0
<script>
        function getSum(a, b) {
            sum = a + b;
            document.write(sum);
        }
        var x = prompt();
        var y = prompt();
        getSum(x, y);
</script>

For example, I input 2 and 2. The output for that will be 22. It's as if it treats the values as if they were characters? But when I skip the whole prompt thing and replace x and y inside the getSum() with actual numbers, the code works. How do I fix this so that it will compute the number values inside variables?

jojo
  • 1
  • "_It's as if it treats the values as if they were characters?_" They are. [`prompt()`](https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt) returns a string, not a number. – Ivar Feb 23 '21 at 13:48
  • change to `sum = +a + +b;` – Reza Feb 23 '21 at 13:48

0 Answers0