-3

in this code snippet i want to sum 2 value that is inserted into prpmpts(values that are numbers other wise alert wrong input). but my problem is when i even insert numbers it alert wrong input and not sum values

function sumation(a,b){
if (typeof a == "number" && typeof b == "number")
return (a + b);
else
alert("wrong input")
}

window.onload = function(){
  var val1 = prompt("insert first number","");
  var val2 = prompt("insert second number","");
  var result = sumation(val1,val2);
  alert(result);
};
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        
    </head>
    <body>
        <script src="newjavascript.js"></script>
    </body>
</html>
sina
  • 9
  • 1
  • 2
  • 7

6 Answers6

2
function sumation(a,b){
    a = parseInt(a);
    b = parseInt(b);
    if (isNaN(a) || isNaN(b)) alert("wrong input")
    else return a + b;
}
Noitidart
  • 35,443
  • 37
  • 154
  • 323
0

To understand the above behavior, try to go through return type of prompt:

A String. If the user clicks "OK", the input value is returned. If the user clicks "cancel", null is returned. If the user clicks OK without entering any text, an empty string is returned.

learner
  • 4,614
  • 7
  • 54
  • 98
0

You can use isNaN(a)

function sumation(a,b){
if (isNaN(a) || isNaN(b))
  alert("wrong input");
else
return (+a + +b);
}

window.onload = function(){
  var val1 = prompt("insert first number","");
  var val2 = prompt("insert second number","");
  var result = sumation(val1,val2);
  alert(result);
};
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        
    </head>
    <body>
        <script src="newjavascript.js"></script>
    </body>
</html>
user3621898
  • 589
  • 5
  • 24
0

Use parseInt() to parse an integer from of the inputs, or parseFloat() to parse a floating point number. If you use parseInt(), it is a good practice to pass the radix (base) as 10 for decimal numbers.

If radix is undefined or 0 (or absent), JavaScript assumes the following:

  • If the input string begins with "0x" or "0X", radix is 16 (hexadecimal) and the remainder of the string is parsed.
  • If the input string begins with "0", radix is eight (octal) or 10 (decimal). Exactly which radix is chosen is implementation-dependent. ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet. For this reason always specify a radix when using parseInt.
  • If the input string begins with any other value, the radix is 10 (decimal).1

Also, isNaN() is another simple way to check if a value is Not a Number (i.e. NaN).

function sumation(a,b){
if (!isNaN(a) && !isNaN(b))
return (a + b);
else
alert("wrong input")
}

window.onload = function(){
  var val1 = parseFloat(prompt("insert first number",""),10);
  var val2 = parseFloat(prompt("insert second number",""),10);
  var result = sumation(val1,val2);
  alert(result);
};
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        
    </head>
    <body>
        <script src="newjavascript.js"></script>
    </body>
</html>
Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
0

You can change your function become this:

window.onload = function(){
  var val1 = parseInt( prompt("insert first number","") );
  var val2 = parseInt( prompt("insert second number","") );
  var result = sumation(val1,val2);
  alert(result);
};
Yusuf Ibrahim
  • 1,591
  • 5
  • 21
  • 46
-1

use parseInt().

function sumation(a,b){
if (typeof a == "number" && typeof b == "number")
return (a + b);
else
alert("wrong input")
}

window.onload = function(){
  var val1 = parseInt(prompt("insert first number",""));
  var val2 = parseInt(prompt("insert second number",""));
  var result = sumation(val1,val2);
  alert(result);
};
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        
    </head>
    <body>
        <script src="newjavascript.js"></script>
    </body>
</html>
Shivkumar kondi
  • 6,458
  • 9
  • 31
  • 58