0

Trying to add three numbers in javascript using functions but its not adding them instead it just writes them as one number

function numinput(a,b,c,res){
a = prompt("Enter first number");
    b = prompt("Enter second number");
    c =  prompt("Enter third number");

    res = a + b + c ;
    alert (res);
}  

numinput();
kamran afzal
  • 19
  • 1
  • 8
  • Possible duplicate of [Javascript concatenating numbers, not adding up](http://stackoverflow.com/questions/14656003/javascript-concatenating-numbers-not-adding-up) – legoscia Apr 22 '17 at 19:28

5 Answers5

1

Convert the values to numbers using

parseInt

. Here's a working solution.

function numinput(a,b,c,res){
        a = parseInt(prompt("Enter first number"), 10);
        b = parseInt(prompt("Enter second number"), 10);
        c = parseInt(prompt("Enter third number"), 10);

        res = a + b + c ;
        alert (res);
    }

    numinput();
HenryDev
  • 4,685
  • 5
  • 27
  • 64
  • This is the correct answer. Although you could use the unary operator + in front of each variable, parseInt(var, radix) is more explicit when it comes to human legibility. – Kyle Richardson Apr 22 '17 at 19:13
  • @KyleRichardson Yup having the + will do the job through coercion, but thanks for the input mate! – HenryDev Apr 22 '17 at 19:13
1

prompt returns a string. You need to first convert the string to number otherwise you are concatenating strings: '5' + '7' === '57'

Here are some ways to achieve this:

1 - Using Number

Number('5');

2 - Using parseInt or parseFloat

parseInt('20', 10);
parseFloat('5.5');

3 - Unary + operator as other answers explained

+'5'

Working demo:

function numinput() {
    var a = prompt("Enter first number"),
        b = prompt("Enter second number"),
        c = prompt("Enter third number"),
        res = Number(a) + Number(b) + Number(c);
      
    alert(res);
}

numinput();
Marcos Casagrande
  • 37,983
  • 8
  • 84
  • 98
0

The each user entry is typeof string, it's being concatenated into one whole string. If you want to add every element as a Math operation, parse the entries into numbers, using + sign in front of variables or parse it using parseInt function.

function numinput(a, b, c, res) {
  a = prompt("Enter first number");
  b = prompt("Enter second number");
  c = prompt("Enter third number");

  res = +a + +b + +c;
  alert(res);
}

numinput();
kind user
  • 40,029
  • 7
  • 67
  • 77
0

You need to convert every value, which is a string, to a number with an unary +.

Then I suggest to move the variables declaration into the function and not inside of the parameters of the function, because you do not need them in this way and you assign the values inside of the function.

function numinput() {
    var a = +prompt("Enter first number"),
        b = +prompt("Enter second number"),
        c = +prompt("Enter third number"),
        res = a + b + c;
      
    alert(res);
}

numinput();
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0
const add = function(a, b, c) {
    return a + b + c;
}

const result = add(4,5,6);
console.log(result);
user16217248
  • 3,119
  • 19
  • 19
  • 37