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();
1

5 Answers 5

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();

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

2 Comments

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.
@KyleRichardson Yup having the + will do the job through coercion, but thanks for the input mate!
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();

Comments

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();

1 Comment

up for unary + operator. I dont see many people using it these days. :)
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();

Comments

0
const add = function(a, b, c) {
    return a + b + c;
}

const result = add(4,5,6);
console.log(result);

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.