【问题标题】:Javascript show prompt until input less than 2Javascript 显示提示直到输入小于 2
【发布时间】: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


    【解决方案1】:

    一个简单的解决方案是让您的readNumberOfEntries 函数返回smaller_than_two 变量,然后让您从readNumberOfEntries 中拉出while 循环并在您的主函数中使用它,这将一直提示直到为真。

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

    【讨论】:

      【解决方案2】:

      只需在else 中再次调用main() 即可重新触发提示

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

      【讨论】:

        【解决方案3】:

        所以您的代码工作正常,但不会被多次调用。您的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();
          }
        }
        

        【讨论】:

          【解决方案4】:

          你可以在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);
          

          【讨论】:

          • 很好的答案,但我应该把option = readNumberOfEntries(option); 放在哪里?
          • 这行之后var option = Number(prompt(messageToDisplay));
          猜你喜欢
          • 1970-01-01
          • 2014-10-17
          • 2019-03-11
          • 2011-07-17
          • 2021-07-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多