【问题标题】:Having trouble displaying my number using javascript无法使用 javascript 显示我的号码
【发布时间】:2020-05-29 21:08:40
【问题描述】:

我必须将十进制转换为二进制,它至少需要 3 个函数,并且必须以 HTML 格式显示。这是我到目前为止所得到的,不知道如何让它正确显示?

// Prompt user for number between 1-1000
let input = parseInt(prompt("Enter a number between 1 and 1000", "50"));

function store() {
let quotient = [];
let answer = quotient.reverse();
return answer;
}
function check() {
    while (input != 0) {
        if (input < 1000 && input % 2 != 0) {
            return quotient.push("1");
            input = input / 2;
        }
        else if (input < 1000 && input % 2 == 0) {
            return quotient.push("0");
            input = input / 2;
        }
    }
}
function display() {
    document.getElementById("displayNumber").innerHTML = answer;
}
display();
&lt;h1 id="displayNumber"&gt;&lt;/h1&gt;

【问题讨论】:

    标签: javascript html arrays function


    【解决方案1】:

    在显示功能中,您将 answer 分配给 innerHTML 但没有调用您的 check 功能。

    <html> 
        <h1 id="displayNumber"></h1>
        <script>
            let input = parseInt(prompt('Enter a number between 1 and 1000', '50'));
            function check(input) {
                let quotient = [];
                while (input > 0) {
                    quotient.push(input % 2);
                    input = parseInt(input / 2);
                }
                return quotient.reverse().join('');
            }
    
            function display() {
                if (input >= 1 && input <= 1000) {
                    document.getElementById("displayNumber").innerHTML = check(input);
                }
            }
    
        display();
    
    </script>
    </html>
    

    【讨论】:

      【解决方案2】:

      这是固定的脚本,希望对你有帮助。

         function check(input, quotient) {
             while (input != 0) {
              if (input < 1000 && input % 2 != 0) {
                  quotient.push("1");
                  input = parseInt(input / 2);
              }else if (input < 1000 && input % 2 == 0) {
                  quotient.push("0");
                  input = parseInt(input / 2);
              }
          }
      }
      
      function display() {
          let input = parseInt(prompt("Enter a number between 1 and 1000", "50"));
          let quotient = [];
          check(input, quotient);
          let answer = quotient.reverse().join('');
          document.getElementById("displayNumber").innerHTML = answer;
      }
      display();
      

      【讨论】:

        【解决方案3】:

        这里更新了sn-p:

        		function check(input) {
          	let quotient = [];
                     while (input != 0) {
                         if (input < 1000 && input % 2 != 0) {
                             input = input / 2;
                         }
                         else if (input < 1000 && input % 2 == 0) {
                             input = input / 2;
                         }
        
                           quotient.push(input);
                     }
                     return quotient;
        
                 }
         
                 function display() {
                     let input = parseInt(prompt("Enter a number between 1 and 1000", "50"));
                     var answer = check(input);
                     document.getElementById("displayNumber").innerHTML = answer;         
        
                }          
                display();
         &lt;h1 id="displayNumber"&gt;&lt;/h1&gt;

        【讨论】:

        • 如果我想再添加一个函数,我可以做到这一点,以便我有一个数组函数吗?不过,我该怎么称呼它呢?
        • 如果可以的话,你可以制作一些功能,并根据需要添加
        【解决方案4】:

        目前,您只是在创建函数 storecheck,但并未实际调用它们。

        但是,如果您只想将输入显示为二进制,则可以使用 toString 函数,传入您想要的基数。

        如果数字超出 1-1000 范围,我不确定要显示什么。所以我只是输入“无效输入”。您可以添加更多检查是否为 NAN 等

         <!DOCTYPE html>
         <html>
        
           <h1 id="displayNumber"></h1>
           <script>
             // Prompt user for number between 1-1000
             let input = parseInt(prompt("Enter a number between 1 and 1000", "50"));
             
             function display() {
               if (input < 1 || input > 1000) {
                 document.getElementById("displayNumber").innerHTML = "Invalid input";
               } else {
                 document.getElementById("displayNumber").innerHTML = input.toString(2);
               }
             }
        
             display();
        
           </script>
         </html>

        【讨论】:

        • 如果我想使用其他功能,应该怎么做?
        猜你喜欢
        • 1970-01-01
        • 2012-04-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多