0

The following code should print the SUM of x and y. It works on the W3 editor, but not my Sublime? I don't understand why.

function add () {
    var x = prompt("Enter a number.");
    var y = prompt("Enter another number.");
    var a = x + y;
    alert(a);
    }
<script>
  add();
  </script>

The output I get is literally xy. If I enter x=1 and y=2, I get 12.
I also run into the same problem if I just hit the js code into the body without the function. I can't find my mistake. Could it be my editor?

5
  • 8
    prompt always returns a string. You've to convert the return values to numbers before the math. JS doesn't do automatic type coercion in this case, since + operator is used to concatenate strings too. Commented Jan 26, 2016 at 18:25
  • @teemu ParseInt method? Commented Jan 26, 2016 at 18:27
  • 1
    That, or parseFloat or unary + or Number constructor. Commented Jan 26, 2016 at 18:28
  • can you explain the difference, please? All 4 work. Under the hood there must differences (bytes used, etc?). Commented Jan 26, 2016 at 18:34
  • @chignon Here's a full reference to the JavaScript Number wrapper object, its properties and its methods: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Jan 26, 2016 at 18:38

4 Answers 4

4

make it

function add () {
    var x =parseInt(prompt("Enter a number."),10);
    var y = parseInt(prompt("Enter another number."),10);
    if ( !isNaN(x) && !isNaN(y) )
    {
      var a = x + y;
      alert(a);
    }
    else
    {
      alert("One of the numbers is not valid");
    }
}

you need to parse the string received from the prompt to a integer first.

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

8 Comments

Don't forget the radix.
Also don't forget to check for NaN.
@j08691 and War10ck, thanks for pointing out, made the changes
checking for NaN I have difficulty with...example please?
@chignon NaN stands for Not a number. isNaN checks if the passed argument is a number or not.
|
1

The values are strings so the + operator is treating it as string concatenation. Use parseInt() to generate integers, then do the addition:

function add () {
    // Best to include radix with parseInt
    var x = parseInt(prompt("Enter a number."), 10);
    var y = parseInt(prompt("Enter another number."), 10);
    // Quick and dirty check for NaN
    if(!(x != x) && !(y != y)) {
        var a = x + y;
        alert(a);
    } else {
        alert("Both inputs must be valid integers!");
    }
}

add();

Just for fun, here's an even shorter method:

function add () {
    var x = +prompt("Enter a number."),
        y = +prompt("Enter another number.");
    alert((!(x != x) && !(y != y)) ? x + y : "Both values must be integers!");
}

add();

Comments

0

You can use use:

function add () {
    var x = Number(prompt("Enter a number."));
    var y = Number(prompt("Enter another number."));
    var a = x + y;
    alert(a);
}

Comments

0

I think it is because prompt returns type string. Try using the

parseInt()

function. So

var x = parseInt(prompt("Enter a number."));

Comments

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.