我正在尝试使页面提示用户输入。我希望它会一直显示提示,直到输入的数字小于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>
伟大的答案,但我在哪里把'option = readNumberOfEntries(选项);'? – Benza
在这行之后'var option = Number(prompt(messageToDisplay));' –