【发布时间】:2017-12-30 10:39:42
【问题描述】:
我正在尝试使页面提示用户输入。我希望它一直显示提示,直到输入的数字小于 2。
我的代码没有正确执行此操作。当我在警报上按 OK 时,它就会关闭。如果输入的数字低于 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>
【问题讨论】:
标签: javascript loops while-loop