Taking user input data

Just finished the 8ball exercise and am wondering how to use the prompt function correctly (this is optional on my part, it wasn’t originally part of the exercise)… how do we extract user input and assign it to a variable, so that it may be returned in a console.log function? Simply put… program asks you to enter a name, you enter a name, and it returns with a greeting Hello, ${userName}!

do you set userName = prompt(“Enter name”); … I keep getting errors trying to use the prompt function… (does prompt only work in a browser environment, therefore i cannot use prompt in the 8ball exercise?)

What kind of errors are you getting?

Yes, I think this is something you need to test in a browser, there might be some text editors that let you do this too but I’m not aware of any for js (I’d be interested to know if there are).

Not sure what the error was exactly, I’d have to go back to the exercise. Just curious what the syntax is for taking user input and assigning that string or number to a variable, to be used in another string, or console.log, etc.

let userName = prompt("Enter name");

// console will log the input: userName

This should work in a test html file.

1 Like

Thank you! :clap:
I thought it would look something like that… I think the prompt function was not working in that 8ball exercise for some reason…