prompt() command isn't executing

Asked by At

I'm trying to learn javascript. Right now I am at a lesson on prompts.

Example 1 works as intended.

Example 2 does not, and there is no error message. An empty window opens and nothing happens.

Both examples are from a book on javascript. Both seem fine in Notepadqq

I'm running them on the same OS and browser(Ubuntu, Firefox)

Since I assume I typed something wrong I already compared it to the source but I can't find my mistake.

Example 1:


var yourName = prompt("Please enter your name\nenter:","name");

alert("You entered:\n" + yourName + "\nThank you!");

document.write(yourName);

Example 2:


var number = prompt("Enter first number:","0");

var n1 = parseFloat(number);

var n2 = parseFloat(prompt("Enter a second number","0");

var sum = n1 + n2;

alert (n1 + " + " + n2 + " = " + sum);

3 Answers

0
Hasan On Best Solutions

Line 3 was wrong

var n2 = parseFloat(prompt("Enter a second number","0");

corrected:

var n2 = parseFloat(prompt("Enter a second number","0"));
0
Rock K On

youre missing a parenthesis after n2 variable should be, parseFloat(prompt("Enter a second number","0"));

0
alt-rock On

You have a syntax error and were missing a parenthesis in n2. Here is the correction.

var number = prompt("Enter first number:","0");

var n1 = parseFloat(number);

var n2 = parseFloat(prompt("Enter a second number","0"));

var sum = n1 + n2;

alert (n1 + " + " + n2 + " = " + sum);