nisargshah95
Your Ad here
Hi, I am a beginner in programming. I just made a calculator in JavaScript but could find a way to add the integers. Here's my code -
But when during its execution while adding, it adds 10+20 as 1020 and 1+1 as 11. Please help me how to solve this. All other operations are working fine.
Thanks in advance.
Nisarg Shah
Code:
<html>
<head>
<title>JavaScript Calculator</title>
<script type="text/javascript">
function calculator(p,q)
{
var r=prompt("Enter '-' sign for subtraction, '*' for multiplication, '/' for division, '^' for modulus and '+' for addition")
var p=prompt("Enter a value for 1st number");
var q=prompt("Enter a value for 2nd number");
if (r=="+")
{
return (p+q);
}
else if (r=="-")
{
return p-q;
}
else if (r=="*")
{
return p*q;
}
else if (r=="/")
{
return p/q;
}
else if (r=="^")
{
return p^q
}
}
</script>
</head>
<body>
<center><b>
<script type="text/javascript">
document.write(calculator(56,56));
</script>
</b></center>
</body>
</html>
But when during its execution while adding, it adds 10+20 as 1020 and 1+1 as 11. Please help me how to solve this. All other operations are working fine.
Thanks in advance.
Nisarg Shah
Last edited by a moderator: