简体   繁体   English

Javascript if-else解释

[英]Javascript if-else interpretation

This little program is supposed to have the browser guess at what the user inputs. 这个小程序应该让浏览器猜测用户输入了什么。 It's "how many fingers". 是“多少根手指”。 So i want it to force the user to use a number 5 or less. 因此,我希望它强制用户使用5或更少的数字。

If the input is larger than the range of options available to the program, the program will error out and break the browser. 如果输入大于该程序可用的选项范围,则该程序将出错并中断浏览器。 I've tried rearranging the nesting of the scripts in various ways to get a feel for how JS "thinks". 我尝试过以各种方式重新安排脚本的嵌套,以了解JS的“思维方式”。 Unfortunately, I still don't have a grasp of how Javascript is interpreting the flow of the code. 不幸的是,我仍然不了解Javascript如何解释代码流。

I use SQL at work and am familiar with the logic of PEMDAS in math. 我在工作中使用SQL,并且熟悉数学中PEMDAS的逻辑。 I think this is a similar situation where i'm missing something about the logical implications of the syntax. 我认为这是类似的情况,其中我缺少有关语法的逻辑含义的信息。 It seems to me like this is a very straightforward program. 在我看来,这是一个非常简单的程序。

I don't understand why the program doesn't execute the alert when my input meets the criteria for executing the IF clause. 我不明白为什么当我的输入满足执行IF子句的条件时程序不执行警报。 It works fine on the Else clause. 在Else子句上可以正常工作。

 document.getElementById("guess").onclick = function() { var a = document.getElementById("myNumber").value; var b = 6; var x = Math.random(); x = x * b; x = Math.floor(x); if (a > b) { alert("you don't have that many fingers"); } else { var gotit = false; var guesses = 1; while (gotit == false) { if (a == x) { gotit = true; alert("Success! the answer is" + x + " or " + a + " it took me " + guesses + " guesses!"); } else { guesses++; var x = Math.random(); x = x * b; x = Math.floor(x); } } } } 
 <p>How many fingers are you holding up?</p> <input id="myNumber" /> <button id="guess">Guess!</button> 

All help is greatly appreciated. 非常感谢所有帮助。

You need to change the test to if (a >= b) . 您需要将测试更改为if (a >= b) The code guesses numbers from 0 to 5 , so if the user enters 6 it will never match. 该代码猜测05数字,因此,如果用户输入6 ,它将永远不会匹配。 But a > b only succeeds when they enter 7 or higher -- 6 is not greater than b . 但是a > b仅在输入7或更高时成功6不大于b

 document.getElementById("guess").onclick = function() { var a = document.getElementById("myNumber").value; var b = 6; var x = Math.random(); x = x * b; x = Math.floor(x); if (a >= b) { alert("you don't have that many fingers"); } else { var gotit = false; var guesses = 1; while (gotit == false) { if (a == x) { gotit = true; alert("Success! the answer is " + x + " or " + a + " it took me " + guesses + " guesses!"); } else { guesses++; var x = Math.random(); x = x * b; x = Math.floor(x); } } } } 
 <p>How many fingers are you holding up?</p> <input id="myNumber" /> <button id="guess">Guess!</button> 

First off, you're repeating your random generation, so I'd break that out to a function. 首先,您要重复随机生成,所以我将其分解为一个函数。 Second, your logic is wrong (has a chance of the guess being 0 fingers as well as a very small chance of it being 6 fingers). 其次,您的逻辑是错误的(猜测是0指的机会,也有很小的几率是6指)。

What Math.random actually does is returns a value between 0 and 1 , so I think you can see the logic error there (0 to 1)*6 = possibly 0 or 6 . Math.random实际作用是返回01之间的值,因此我认为您可以看到那里的逻辑错误(0 to 1)*6 = possibly 0 or 6

So if you start off with a function for the calculation 因此,如果您从计算功能开始

  function getRandomFinger(amountOfFingers) {
    var x = Math.random();
    x = (x * (amountOfFingers-1))+1; // ((0 to 1)*4)+1 because always at least one finger but possibly 4 more
    return Math.round(x);
  }

and you can call that when needed. 您可以在需要时致电给您。 And then actually defining the amount of fingers makes more sense right? 然后实际定义手指的数量更有意义吗? More than some seemingly random number. 不止是一些看似随机的数字。

  var b = 5; // 5 fingers on the hand

Add those few changes and your code should work. 添加这些更改,您的代码应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM