简体   繁体   English

为什么这个基本的if语句不起作用? JavaScript的

[英]Why does this basic if-statement fail to work? JavaScript

var userinput = prompt("Hello. Please enter a statement and I will repeat the last word");
if(typeof userinput !== "string"){
console.log("This is not a string.");
}

I am a complete newb and I was just trying to make a simple script that repeats the last word of a statement that you enter. 我是一个完整的新手,我只是想创建一个简单的脚本,重复你输入的语句的最后一个字。 I added this safeguard so that if you don't enter a string, it will display a message, but whenever I test the code with a number, it just ignores this part and still executes the else statement which is the code that finds the last word. 我添加了这个安全措施,这样如果你没有输入一个字符串,它会显示一条消息,但每当我用一个数字测试代码时,它只是忽略了这个部分并仍然执行else语句,这是找到最后一个的代码。字。 I don't understand why this safeguard won't work. 我不明白为什么这个保障不起作用。

Would really appreciate some help. 非常感谢一些帮助。 Thanks 谢谢

Because prompt() returns a string unconditionally (or null). 因为prompt()无条件地返回一个字符串(或null)。 It does not try to parse what was entered and go "oh, hey, it's all digits. I'll just return an int instead". 它没有尝试解析输入的内容并且去“哦,嘿,它是所有数字。我只会返回一个int ”。 That means your test is basically meaningless... you'll never get anything OTHER than a string back, so your code will always take the 'else' path. 这意味着你的测试基本上没有意义......你永远不会得到任何东西而不是字符串,所以你的代码总是采用'else'路径。

relevant doc: https://developer.mozilla.org/en-US/docs/Web/API/window.prompt 相关文档: https//developer.mozilla.org/en-US/docs/Web/API/window.prompt

"123" is still a string. "123"仍然是一个字符串。

prompt returns a string or null. prompt返回一个字符串或null。 It doesn't matter what characters are entered. 输入的字符无关紧要。

Prompt returns a string. 提示返回一个字符串。 That string may be "2" but it is still a string. 该字符串可能是"2"但它仍然是一个字符串。

you can test with a regex like this: 你可以使用这样的正则表达式进行测试:

var reg = /^\d+$/;
if(reg.test(userinput)){
    console.log("This is a number.");
 }

to see if it is a number 看看它是否是一个数字

Everything received from a user-input is a string at first. 从用户输入接收的所有内容最初都是一个字符串。 What you want to do is test if the input can be converted to a number of some sort. 你想要做的是测试输入是否可以转换为某种数字。

What are you expecting it to do? 你有什么期望呢?

prompt() always returns a string, so userinput will always be a string, no matter what the user enters. prompt()始终返回一个字符串,因此无论用户输入什么, userinput将始终是一个字符串。 If they don't enter anything, it will be a blank string, but still a string. 如果他们没有输入任何内容,它将是一个空白字符串,但仍然是一个字符串。 If they enter a number, it will be a string that happens to contain digit characters. 如果他们输入一个数字,它将是一个恰好包含数字字符的字符串。

So your typeof check will always fail. 所以,你typeof检查总会失败。

If you want to check whether they entered anything, you could just check if it's got a non-blank value: 如果你想检查他们是否输入了任何内容,你可以检查它是否有非空值:

if(!userinput) {
     //user didn't enter anything.
}

The use of prompt() would always return a string. 使用prompt()总是会返回一个字符串。 So, what you need is to test if it's also a number. 所以,你需要的是测试它是否也是一个数字。

if(typeof userinput === "number") {
    console.log("This is also a number.");
} else {
    // only string
}

Alternatively, you could also use isNan() to check for "Not a Number". 或者,您也可以使用isNan()来检查“非数字”。

Five people and counting have explained that "1" is a string... (as is "" , an empty string), and prompt() returns strings... 五个人和计数已经解释说"1"是一个字符串......(就像"" ,一个空字符串),并且prompt()返回字符串......


... but , to achieve what it sounds like you're trying to do - give a different result if the string is a string that can be understood as representing a number - you could try this: ... 但是为了实现你想要做的事情 - 如果字符串是一个可以理解为代表数字的字符串,则给出不同的结果 - 你可以试试这个:

if( parseFloat(userinput) !== NaN ){

parseFloat() tries to translate the string into a number that may have a decimal place (a 'float'). parseFloat()尝试将字符串转换为可能具有小数位的数字('float')。 If it can't, it returns NaN (Not a Number), a special entity type that evaluates as false (but so does 0 or -1, which are numbers, so we test !== NaN rather than just using if( parseFloat(userinput)){ which would test if it's something that evaluates as true ). 如果它不能,它返回NaN (非数字),一个特殊实体类型,其值为false (但0或-1也是数字,所以我们测试!== NaN而不是仅使用if( parseFloat(userinput)){它将测试它是否为评估为true东西)。

As those docs explain, it's quite permissive, eg it will interpret "5 words in this string" as 5.0 . 正如那些文档所解释的那样,它非常宽松,例如它会将"5 words in this string"解释为5.0

Or parseInt() if you only care about integers. 或者parseInt()如果你只关心整数。 Or, read up on regular expressions if you want to be strict about what type of input you allow in your strings. 或者,如果您想严格控制字符串中允许的输入类型,请阅读正则表达式

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

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