1

I'm trying to make a simple project where i can use addition in a function and where the user has to interact with it. It works with subtraction, multiplication and division but it does not seem to work with addition. Here is my code:

function add(first, second) {
    "use strict";
    first = prompt();
    second = prompt();
    return first + second;
}
var sum = add(sum);
alert(sum);
  • Why do you define `first` and `second` as parameters if you are overwriting their values with the return value of `prompt` anyway? And why are you passing `sum` to `add`? At that moment, `sum` doesn't have a value yet and you defined the function to accept two arguments, not just one (even though you are not doing anything with them, as already mentioned). – Felix Kling Mar 02 '17 at 15:52
  • FWIW, *"but it does not seem to work"* is not a useful problem description. And should provide the input you are giving, the result you get and the result you expect. – Felix Kling Mar 02 '17 at 15:53
  • I'm so sorry! I'm still new to this and especially javascript so as a beginner i got a task where i should think of ways of using a function. I did search for problems related to mine but i guess i didn't do it hard enough I'll keep what you wrote to on here in mind, and thanks for the information provided! – Rasmuz Nordqvist Mar 02 '17 at 18:35

2 Answers2

1

You get strings a type for the input. You clould convert the strings to number with an unary +.

The unary plus operator precedes its operand and evaluates to its operand but attempts to convert it into a number, if it isn't already. Although unary negation (-) also can convert non-numbers, unary plus is the fastest and preferred way of converting something into a number, because it does not perform any other operations on the number. It can convert string representations of integers and floats, as well as the non-string values true,false, and null. Integers in both decimal and hexadecimal ("0x"-prefixed) formats are supported. Negative numbers are supported (though not for hex). If it cannot parse a particular value, it will evaluate to NaN.

BTW, you need no paramter for calling the function add, because you do not use the value.

"use strict";

function add() {
    var first = prompt(),
        second = prompt();
    return +first + +second;
    //     ^        ^
}

var sum = add();

console.log(sum);

For a safe addition, you could use a default value for NaN values.

"use strict";

function add() {
    var first = prompt(),
        second = prompt();
    return (+first || 0) + (+second || 0);
}

var sum = add();

console.log(sum);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

Because prompt() returns a string and you need to convert it to integer (or float if you want)

// Totally missed that, indeed you shouldn't be passing args if you're going
// to overwrite them
function add() {
    "use strict";
    var first  = prompt(),
        second = prompt();

    return parseInt(first) + parseInt(second);
}
var sum = add();
alert(sum);
smarber
  • 4,829
  • 7
  • 37
  • 78