1

I'm a beginner to Javascript and I have a basic question about how to use the prompt method. None of the code seems to process below. Is there some sort of hidden rule about using multiple prompt boxes or does my code just have a syntax error? Any help would be much appreciated. Thanks in advance.

    <html>
    <head> 
    <title> Two Numbers </title>
    <script type="text/javascript">
    var first = prompt("Enter first number:");
    var second = prompt("Enter second number:");

    var sum = (first-0) + (second-0);
    var diff = first - second;
    var divide = first/second;
    var multi = first*second;


    document.write(first + " + " + second " = " + sum + "<br />"); 
    document.write(first + " + " + second " = " + diff + "<br />"); 
    document.write(first + " + " + second " = " + divide + "<br />"); 
    document.write(first + " + " + second " = " + multi + "<br />"); 

    </script>
    </head>

    <body> 
    </body>
    </html>
3
  • 1
    Check the console by pressing F12 in any major browser, and going to console. It will tell you about any syntax errors or errors that cause an end of script execution. Commented Aug 29, 2014 at 0:04
  • 1
    Maybe it's the missing + between second and " = ". This would cause a parse error which would prevent the script from running. Commented Aug 29, 2014 at 0:05
  • jsfiddle.net/pdstu13p/1 Commented Aug 29, 2014 at 0:15

3 Answers 3

2

You're missing a +.

//change this
console.log(first + " + " + second " = " + sum + "<br />"); 
// to this
console.log(first + " + " + second + " = " + sum + "<br />"); 

In the future, please use the console for debugging. There is a great article on everything you can do with the console here > https://developer.chrome.com/devtools/docs/javascript-debugging

Sign up to request clarification or add additional context in comments.

Comments

2

Corrected the syntax error and corrected the operators in the write() function:

<html>
<head>
<title> Two Numbers </title>
<script type="text/javascript">
var first = prompt("Enter first number:");
var second = prompt("Enter second number:");

var sum = (first-0) + (second-0);
var diff = first - second;
var divide = first/second;
var multi = first*second;

document.write(first + " + " + second + " = " + sum + "<br />");
document.write(first + " - " + second + " = " + diff + "<br />");
document.write(first + " / " + second + " = " + divide + "<br />");
document.write(first + " * " + second + " = " + multi + "<br />");

</script>
</head>
<body>
</body>
</html>

use the console to check for errors, as said by James G

Comments

1

var isValid = true;

var first = prompt("Enter first number:");
if (!Number(first)) {
  alert("Please enter numeric value only.");
  isValid = false;
}

if (isValid) {
  var second = prompt("Enter second number:");
  if (!Number(second)) {
    alert("Please enter numeric value only.");
    isValid = false;
  }

  if (isValid) {
    var sum = first + second;
    var diff = first - second;
    var divide = first / second;
    var multi = first * second;

    console.log(first + " + " + second + " = " + sum);
    console.log(first + " - " + second + " = " + diff)
    console.log(first + " / " + second + " = " + divide);
    console.log(first + " * " + second + " = " + multi);
  }
}

1 Comment

When you're using prompt function you must validate the entered value in case of you need to perform mathematics calculation otherwise it will cause an error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.