Well this is what happen's when you add plus before prompt, i.e. as below,
Eg :- 1
var a = prompt("Please enter a number");
console.log(a);
typeof(a);
Now in eg (1) when you enter a number and if you check that in console, it show a number but as that number is in-between double-quote, so in JavaScript it's a string, that's what it will show in typeof too when you console that.
Eg :- 2
var a = +prompt("Please enter a number");
console.log(a);
typeof(a);
Now when you console the var a and typeof a of eg(2) the result differs as we have added + before prompt. So this time we get our prompt input value as number and not string. Try you will understand what I'm saying.
+prompt vs prompt in JavaScript - Stack Overflow
How to store user input using prompt() in JavaScript? - Stack Overflow
prompt function in JS ? (Eloquent JavaScript book)
JavaScript Prompt() method - Stack Overflow
Videos
Well this is what happen's when you add plus before prompt, i.e. as below,
Eg :- 1
var a = prompt("Please enter a number");
console.log(a);
typeof(a);
Now in eg (1) when you enter a number and if you check that in console, it show a number but as that number is in-between double-quote, so in JavaScript it's a string, that's what it will show in typeof too when you console that.
Eg :- 2
var a = +prompt("Please enter a number");
console.log(a);
typeof(a);
Now when you console the var a and typeof a of eg(2) the result differs as we have added + before prompt. So this time we get our prompt input value as number and not string. Try you will understand what I'm saying.
No.
The unary plus operator will convert the response in to a Number, not an integer.
It could give you a floating point value, it could give you NaN.
If you want an integer then you need to check the response and then put in some error recovery for cases where the response is not what you want.
For example: If it is a floating point value, then you might want to just use Math.floor to convert it. If it is NaN then you might want to prompt the user again.
ยป npm install prompt
Hi there,
I started to teach my self JS via the book " Eloquent JavaScript"
I encounter the use of prompt function in the book, which I understand is suppose to take an input from the user (similar to 'scanf' in C (?))
I'm using Visual Studio Code. When I try to run this code:
let theNumber = Number(prompt("Pick a number"));
if (!Number.isNaN(theNumber)) {
console.log("Your number is the square root of " +
theNumber * theNumber);
}I'm getting an error message: "ReferenceError: prompt is not defined"
How can I use to "prompt" function in JS ?
Thanks for helping!
From the sounds of it, the following meets your requirements:
var a, d, t;
while ( ! a ) a = prompt( "Which Artist?" );
while ( ! d ) d = prompt( "How many DVDs?" );
while ( ! t ) t = prompt( "How many tapes?" );
alert( "You want " + t + " Tapes, and " + d + " DVDs, of " + a + "." );
Let's break it down you so have an understanding of what's going on:
var a, d, t;
On the first line, I'm declaring the various variables I plan on using in the code below. This is a common practice, and would be a good habit to develop if you want to maintain clean and manageable code.
while ( ! a )
The while loop is a loop that will run over and over, until a condition is met. In this example, we're telling the loop to run as long as we don't have a value for a. What comes next is our attempt to collect a value of a from the user:
while ( ! a ) a = prompt( "Which Artist?" );
Each time the while loop runs, we will prompt the user to answer the question. We take their answer, and assign it to a. If they entered nothing, our while loop runs again, prompting them again. You can probably make sense of the next two while loops at this point.
Lastly is our alert, which gathers up the various values and shows them to the user:
alert( 'Artist ' + a );
This also presents an example of string concatenation, or joining together of two strings. We have a value stored inside a, and a value written explicitly as text. We use the + operator to join both of these together, like glue tying two ends of a rope together. As we add more strings, and more variables, we use the + operator more and more:
alert( "You want " + t + " Tapes, and " + d + " DVDs, of " + a + "." );
When this code is ran, t, d, and a will all be replaced with the actual values inserted by the end-user.
Note, this is a very basic implementation of what your homework requires. A real solution would test the type of input to make sure it's of the expected format. For instance, when asking how many DVDs the user wants, you may want to restrict 'acceptable' answers to integers only.
Best of luck!
Use a loop over the values object/array. Maybe use a second (nested) loop to prompt again until a value was entered.