Search code examples
javascriptfunctionaddition

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


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

Solution

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