Prompt undefined/number issues

Why is
the prompt in the first paragraph code showing as undefined?
the prompt in the second paragraph showing as number?

const fav1 = Number(prompt ("Fav Num?"));
console.log (fav1);
console.log (typeof Fav1);


    
const favourite = Number(prompt("What's your favourite number?"));
console.log (favourite);
console.log (typeof favourite);

Function call:

myFunction()

Variable name then some brackets:

myFunction ()

I’m don’t understand what you mean. I’m a beginner.
I’m trying to work out the difference between

The top paragraph which is code I have typed. If I type a number in the prompt it returns it as undefined. (I’m trying to make it return a number)
and
The code I have pasted from a tutorial which is returning a number.

The code you posted had a gap between the name of the function (prompt) and the brackets used to call it: that would have caused the result to be undefined.

That isn’t the case in the screenshot, where the reason it’s doing it is to do with the thing highlighted as an error with a red squiggle under it in the code

Sorry - The number appeared because I keep switching between the code and prompt box!

See new screenshot. It’s still the same.

Thanks

Matt

Also revised code from latest screenshot added:

const fav1 = Number(prompt("Fav Num?"));
console.log (fav1);
console.log (typeof Fav1);


    
const favourite = Number(prompt("What's your favourite number?"));
console.log (favourite);
console.log (typeof favourite);

Look carefully at the third line of that code, there is something different about the variable you’re checking the type of.

1 Like

Thanks . Damn it! Got it now. It’s difficult for me to see. I need to make sure I use the code editor to write for me when offered suggestions.

1 Like

I didn’t notice at all either, it took until I’d actually put the code into a console and gone back and forth over it several times before I saw it!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.