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?
promptalways 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.parseFloator unary+orNumberconstructor.Numberwrapper object, its properties and its methods: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…