2017-07-24 75 views
2

我正在嘗試使頁面提示用戶輸入。我希望它會一直顯示提示,直到輸入的數字小於2.Javascript顯示提示,直到輸入小於2

我的代碼沒有正確執行此操作。當我按下我的提醒時,它會關閉。如果輸入的數字低於2,我希望提示重新顯示。

我需要使用名爲readNumberOfEntries的函數編寫此代碼。

我此刻的代碼是:

<script> 
"use strict"; 

main(); 

/* You may not change anything in the mainFuncion */ 
function main() { 

    var messageToDisplay = "Enter 1 to check whether data is sorted\n"; 
    messageToDisplay += "Enter 2 to check whether data represents a palindrome"; 

    var option = Number(prompt(messageToDisplay)); 

    readNumberOfEntries(option); 

    if (option === 1) { 
     if (isSorted()) { 
      alert("Data is sorted"); 
     } else { 
      alert("Data is not sorted"); 
     } 
    } 

    else if (option === 2) { 
     if (isPalindrome()) { 
      alert("Data represents a palindrome"); 
     } else { 
      alert("Data does not represent a palindrome"); 
     } 
    } 

    else { 
     alert("Invalid option provided."); 
    } 

} 

function readNumberOfEntries($value) { 
    /* YOU MUST IMPLEMENT THIS FUNCTION */ 
    var smaller_than_two = false; 

    if($value < 2) { 
     smaller_than_two = true; 
    } else { 
     smaller_than_two = false; 
     //alert('Error: Number must be greater than or equal to 2'); 
    } 

    while(!smaller_than_two) { 
     if($value < 2) { 
      smaller_than_two = true; 
     } 
    } 
} 

function isSorted() { 
    /* YOU MUST IMPLEMENT THIS FUNCTION */ 

} 

function isPalindrome() { 
    /* YOU MUST IMPLEMENT THIS FUNCTION */ 
} 

</script> 

回答

0

一個簡單的解決方法就是讓你的readNumberOfEntries函數返回smaller_than_two變量,然後供您readNumberOfEntries拉出while循環,並用它而不是在你的主要功能,這將繼續提示,直到真實。

function main() { 

    while(!readNumberOfEntries(option)){ 
     // alerts, etc ... 
    } 
} 
0

只需在else再打電話main()重新觸發提示

else { 
    alert("Invalid option provided."); 
    main() 
} 
0

所以你的代碼工作正常,但它沒有被調用一次以上。您的main();呼叫會調用一次工作邏輯,但是如果符合條件,您需要重新呼叫它。我會建議遞歸調用main()

function main() { 

    var messageToDisplay = "Enter 1 to check whether data is sorted\n"; 
    messageToDisplay += "Enter 2 to check whether data represents a palindrome"; 

    var option = Number(prompt(messageToDisplay)); 

    readNumberOfEntries(option); 

    if (option === 1) { 
     if (isSorted()) { 
      alert("Data is sorted"); 
     } else { 
      alert("Data is not sorted"); 
     } 
    } 

    else if (option === 2) { 
     if (isPalindrome()) { 
      alert("Data represents a palindrome"); 
     } else { 
      alert("Data does not represent a palindrome"); 
     } 
    } 

    else { 
    alert("Invalid option provided."); 
    } 

    if(option <2){ 
    main(); 
    } 
} 
0

你可以只運行一個循環內readNumberOfEntries

function readNumberOfEntries($value) { 
    while(true) { 
     if($value>2) { 
     return $value; 
     } else { 
     $value = Number(prompt("Enter a value greater than 2")); 
     } 
    } 
} 

然後通過readNumberOfEntries返回的值賦給option

option = readNumberOfEntries(option); 
+0

偉大的答案,但我在哪裏把'option = readNumberOfEntries(選項);'? – Benza

+0

在這行之後'var option = Number(prompt(messageToDisplay));' –