1.4 - User input
Our programs have been all well and good so far, but they have been quite limited because the user doesn’t really interact with them in any way. Most real programs require some action from the user. It’s time to start asking our user to answer questions!
The way we will take user inputs for this part of the book is by using the
prompt() function. When we get to part two and start building web-forms in HTML, we’ll take user input through the usual text box in the browser as well as outputting back to the browser.
For now, be patient though as you’ll have to flick between the “empty” browser which is where the prompt box will appear, and the console in DevTools which is where your output will appear.
let person = prompt("What is your name?"); console.log("Hello, "+person);
How do we combine the above last few features to automatically ask what the persons name is, find the space, and use it’s value to split the name into firstname and familyname?
let person = prompt( "Type your full name:" ); let space = person.indexOf(" "); let given_name = person.slice(0, space); let family_name = person.slice(space+1); console.log( "Your first name is: "+given_name ) console.log( "Your family name is: "+family_name )