Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Reply
 
LinkBack Thread Tools Display Modes
Old 20-10-2010, 07:28 PM   #1 (permalink)
Your Ad here
 
nisargshah95's Avatar
 
Join Date: Feb 2010
Location: Mahesana, Gujarat, India
Posts: 326
Exclamation No addition support in calculator in JavaScript.


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 -

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 Liverpool_fan; 20-10-2010 at 07:58 PM. Reason: Added code tags for JavaScript Code
nisargshah95 is offline   Reply With Quote
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 20-10-2010, 07:57 PM   #2 (permalink)
Sami Hyypiä, LFC legend
 
Liverpool_fan's Avatar
 
Join Date: Jun 2007
Location: Нью-Дели
Posts: 2,138
Default Re: No addition support in calculator in JavaScript.

just use parseInt function so that the + sign is used as addition operation instead of concatenation.
Instead of:

Code:
var r=prompt("Enter '-' sign for subtraction, '*' for multiplication, '/' for division, '^' for modulus and '+' for addition")
put

Code:
var r=parseInt(prompt("Enter '-' sign for subtraction, '*' for multiplication, '/' for division, '^' for modulus and '+' for addition"))
do it for each of the variables that take input
__________________
Experience true education in Computer Science - http://www.udacity.com | http://www.coursera.org

Spoiler:
Read before asking / messaging any moderator for any query: FAQ + answers for new members

Read all the sticky threads before asking any type of query. Most basic questions are answered in those.
Don't use forum for chatting. Visit http://webchat.freenode.net/?channels=krow, enter nick and connect.
Liverpool_fan is offline   Reply With Quote
Old 25-10-2010, 11:31 PM   #3 (permalink)
Your Ad here
 
nisargshah95's Avatar
 
Join Date: Feb 2010
Location: Mahesana, Gujarat, India
Posts: 326
Default Re: No addition support in calculator in JavaScript.

Thanks for your reply, but I successfully debugged my code by putting p=p*1 and q=q*1 at the beginning so that JavaScript recognizes it as an integer. It works fine now.
nisargshah95 is offline   Reply With Quote
Old 26-10-2010, 09:10 PM   #4 (permalink)
BIOS Terminator
 
nims11's Avatar
 
Join Date: Apr 2008
Location: Ranchi
Posts: 816
Default Re: No addition support in calculator in JavaScript.

using the parseInt is a better approach to the problem.
alternatively you may also declare an integer as

var i= new Number();
nims11 is offline   Reply With Quote
Old 19-03-2011, 08:25 PM   #5 (permalink)
Your Ad here
 
nisargshah95's Avatar
 
Join Date: Feb 2010
Location: Mahesana, Gujarat, India
Posts: 326
Thumbs up Re: No addition support in calculator in JavaScript.

Quote:
Originally Posted by nims11 View Post
using the parseInt is a better approach to the problem.
alternatively you may also declare an integer as

var i= new Number();
Thanks!
__________________
Quote:
“The Web is like a dominatrix. Everywhere I turn, I see little buttons ordering me to Submit.”
ASUS P5KPL AM/PS | P4 @ 3.07GHz | Ubuntu 11.04 / Win Server 2k8 / XP SP3 | 1GB DDR2 | Samsung HD080HJ 80GB
nisargshah95 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by Charan
- by Charan
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 03:23 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2