2017-07-24 74 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));' –