简体   繁体   English

基本的如果语句脑放屁HTML javascript

[英]basic if statment brain fart HTML javascript

trying to learn javascript for the first time i am stuck on an issue involving an if statement I want the user to be able to put in the text a letter then have the computer say whether what he put in was right or wrong when I run this it usually skips some part of the if statement. 第一次尝试学习javascript时,我陷入了一个涉及if语句的问题,我希望用户能够在文本中输入字母,然后让计算机说出我输入的内容是对还是错。它通常会跳过if语句的某些部分。 this could be because I'm using the != symbol but I don't know below is the code to show you what I am working with I hope I can get you some help! 这可能是因为我正在使用!=符号,但是我不知道下面是显示您正在使用的代码的代码,希望可以为您提供帮助!

 document.getElementById("Submit").onclick = function(){ var a = "'a', 'b', 'c'"; var enter = document.getElementById("text").value; a.toString(); if (a == enter) { alert("congrats"); } else if(enter == null) { alert("you put nothing"); } else if (enter != a) { alert("wrong try again") } else { alert("End of if statment") } } 
 <form> First name:<br> <input type="text" id = "text" name="firstname"> </form> <button type="button" id ="Submit">Submit!</button> 

First, you have to change your a variable. 首先,您必须更改变量。 Now you have an array(3 different values a,b,c), or just weird string( 'a','b','c'), I'm not sure about this. 现在您有了一个数组(3个不同的值a,b,c),或者只是一个奇怪的字符串('a','b','c'),对此我不确定。 It shuold look like this: 它看起来像这样:

var a = "abc"; var a =“ abc”;

Now, your "congrats" alert should work, as it can actually try to weak-compare these two values succesfully. 现在,您的“祝贺”警报应该可以正常工作,因为它实际上可以成功地对这两个值进行微弱比较。

Next step you have to make, is to change null for "". 下一步,您需要将“”更改为null。

else if (enter == "") 否则,如果(输入==“”)

That's because this value(no input) is actually undefined value, not null, so with "null" there is no way to compare this. 这是因为该值(无输入)实际上是未定义的值,而不是null,因此使用“ null”无法进行比较。

These are just basic primitive variables types. 这些只是基本的原始变量类型。 For more information I would reccomend to read this: Read That Book 有关更多信息,我建议阅读: 阅读那本书

You declared a as a string with value 'a', 'b', 'c' so it is a string with some letters and the ' symbol. 您将a声明为具有值'a', 'b', 'c'的字符串,因此它是带有一些字母和'符号的字符串。

If you want to check if what the user entered is one of those letters declared by you (let's say a , b or c )... 如果要检查用户输入的内容是否是您声明的字母之一(假设abc )...

You should (for your attempt) declare it as an array like this: var a = ['a', 'b', 'c'] and use Array.prototype.indexOf in order to see if what the user entered is a letter inside the array. 您应该(尝试)将其声明为如下所示的数组: var a = ['a', 'b', 'c']并使用Array.prototype.indexOf来查看用户输入的内容是否为字母在数组中。

See below snippet 见下面的片段

 document.getElementById("SubmitArrayVersion").onclick = function() { var aArray = ['a', 'b', 'c']; // array version var enter = document.getElementById("text").value; if (aArray.indexOf(enter) !== -1) { alert("congrats"); } else if (!enter) { alert("you put nothing"); } else { alert("wrong try again") } } 
 First name:<br> <input type="text" id="text" name="firstname"> </form> <button type="button" id="SubmitArrayVersion">Submit! (Array version)</button> 

It will be correct when the user enters 'a', 'b', 'c' into the input field. 当用户在输入字段中输入'a', 'b', 'c'时,它将是正确的。

If you try the code below 如果您尝试下面的代码

 document.getElementById("Submit").onclick = function(){ var array = ['a', 'b', 'c']; var enter = document.getElementById("text").value; if (array[0] == enter){ alert("congrats"); } else if(enter == null){ alert("you put nothing"); } else if (enter != array[0]){ alert("wrong try again") } else{ alert("End of if statment") } } 
 <form> First name:<br> <input type="text" id = "text" name="firstname"> </form> <button type="button" id ="Submit">Submit!</button> 

That will alert congrats when a is entered into the input as it is checking the first element of the array (assuming if you have worked with arrays) 当在检查输入数组的第一个元素时,如果输入a,这将引起祝贺(假设您是否使用过数组)

Assuming you want your program to print out success when the user puts in either the single letter a, b, or c this should work. 假设您希望程序在用户输入单个字母a,b或c时成功打印出来,那么这应该可以工作。

 document.getElementById("Submit").onclick = function() { var enter = document.getElementById("text").value; if ('a' === enter || 'b' === enter || 'c' === enter) { alert("congrats"); } else if (enter == "") { alert("you put nothing"); } else if('a' != enter || 'b' != enter || 'c' != enter) { alert("wrong try again"); } } 
 <form> First name:<br> <input type="text" id="text" name="firstname"> </form> <button type="button" id="Submit">Submit!</button> 

In the first if statement I used the || 在第一个if语句中,我使用了||。 operator to distinguish between different conditions, the expression in the if statement will convert to true if enter is either the string a, b, or c. 运算符以区分不同的条件,如果enter是字符串a,b或c,则if语句中的表达式将转换为true

Then the second part checks if enter is empty, the third part is kinds obsolete because we already know that enter is not a, b, or c because we checked it in the first if statement but I left it in for reference. 然后第二部分检查enter是否为空,第三部分已过时,因为我们已经知道enter不是a,b或c,因为我们在第一条if语句中对其进行了检查,但我留作了参考。

So: 所以:

  } else if('a' != enter || 'b' != enter || 'c' != enter) {
    alert("wrong try again");
  }

Could be rewritten to: 可以重写为:

else {
   alert("wrong try again");
}

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

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