-1

Good afternoon!

I have found this code and I am surprised that "backspace" on a keypad converts it to a NUMBER:

     let x = null;              
     x = prompt("Write something: ", ""); 
    if((x == null) || (x == ""))   {
    alert("Nothing was written");
    } else {
    if("NaN" == 1*x+"")   {
    alert("A string was written");
    }  else    {
    alert("A number was written");
    }
    } 

Why?? And - how to treat it?

And, please, my second question:
there was written let x = null . May I write let x = "" instead? Or only let x?

Many thanks in advance!

Nikos M.
  • 8,033
  • 4
  • 36
  • 43
Joan
  • 5
  • 3
  • 1
    https://stackoverflow.com/questions/9307603/is-whitespace-equals-to-integer-0-in-javascript – epascarello Nov 15 '18 at 13:58
  • What is this supposed to be doing? `"NaN" == 1 * x + ""` – Katie.Sun Nov 15 '18 at 13:58
  • "And, please, my second question" — Don't ask multiple questions which are unrelated or only tangentially related in a single question. It makes it harder to answer / close as duplicates. – Quentin Nov 15 '18 at 13:58
  • You can set x to "81278127398127312uihkhdkjahdiyqweyqiuyqwkjehqwkjeqwkjeh" and it would not matter. – epascarello Nov 15 '18 at 13:58
  • Your question is based on a false premise. Your test to see if the value is a number just does a string comparsion to the string "NaN"` – Quentin Nov 15 '18 at 14:01
  • Also hitting backspace does in fact return the first result. – Katie.Sun Nov 15 '18 at 14:03
  • 2
    Have you tried debugging your code and see value of x after prompt? – barbsan Nov 15 '18 at 14:06
  • My code I had read, it is not from my head. And "NaN" == 1 * x + "" there was explained that 1*num tries convert num to a number and if it failes so adding "" NaN is converted to a string. – Joan Nov 16 '18 at 08:39

2 Answers2

-1
let x;
x = prompt("Write something: ", "");

console.log("NaN" == 1); //this will always be false
console.log(1 * x); //x is multiplied by 1 if it is a number
                    //if x is a letter prints 'NaN'
                    //if x is null prints 0 because x is converted to a number and when we do this conversion null becomes 0
                    //if x is empty space also prints zero because when javascript converts " " to a number the result is 0
console.log(x + ""); //x is converted to a string if it is a number, but this essentially just prints the value of x
console.log("NaN" == 1 * x + "");
//because of the order of operations, our first step is to multiply 1 by x
    //if x is a space, the result is 0 | 1 * " " = 0
    //if x is null, the result is 0 | 1 * null = 0
    //if x is a letter, returns NaN | 1 * A = NaN
    //if x is a number, returns the number | 1 * 2 = 2
//next we add "" to whatever value we got in the first step
    //if the current value of x is 0 we are left with "NaN" == 0, which is false
    //if the current value of x is a letter, we are left with "NaN" == "NaN", which is true
    //if the current value of x is a number, we are left with "NaN" == *a number* which is false


if ((x === null) || ( x.trim() === "")) { //check if x is empty space
    alert("Nothing was written");
} else {
    if (Number(x) === "NaN") { //will return true if x contains letters
        alert(Number(x));
    } else {
        alert(Number(x)); //we end up here if x is a number
    }
}

Ok, so the above should answer your first question. As for the second, it doesn't matter how you initialize x because on the next line you assign it a value. This value is a String, and JavaScript only tries to convert it to a number once you start doing math with it.

Katie.Sun
  • 711
  • 3
  • 15
  • Who is down voting all of the answers on this page? – Katie.Sun Nov 15 '18 at 15:02
  • Great! This is the only code which does not convert clicking on space bar on keypad (how is it in English: space? blank space? white space?) to a number. And, please, is it possible to alert my own text instead of "NaN"? Because this word never understand a non-programmer and my ideas goes to future. Many thanks for your time :-) – Joan Nov 16 '18 at 09:14
  • @Joan to change the alert text all you would have to do is replace the `Number(x)` after `Number(x) === "NaN"` with the text you would want to appear. and yes, in English it's just space. – Katie.Sun Nov 16 '18 at 13:40
  • @Katie.Sun Thanks! Unfortunately the code works curiously: I have only changed `alert("A number " +x+ " entered");` in the last `else` and since I have the phrase `A number...entered` in both of the alerts! Regardless I have entered number or string. For a small explanation: In future, learning much more of Javascript I want to write some simply tasks for my handicaped niece therefore I need to know to write very comprehesible alerts – Joan Nov 19 '18 at 08:43
  • @Katie.Sun Hurrah, I have solved :-) I have only replaced my `(x=="")` with your `(x.trim() === "")` and the code works excellently. Bye! – Joan Nov 19 '18 at 09:03
-2

In JS, " " == 0 equals true, so when trying to execute + " " end up as + 0

AleksW
  • 703
  • 3
  • 12