我正在嘗試使頁面提示用戶輸入。我希望它會一直顯示提示,直到輸入的數字小於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));' –