Expressions help

M

mwh

Hi.

I am making a calculator kind of calculator using javascript, and I am
trying to add numbers together. Example:

function additionCalculator(num1, num2){
var total = num1+num2
return total
}

I use this script to add num1 and num2 together. But, it just puts them
together, it does not add them together.

I also want to subtract and divide in my calculator, and I don't know
how to use these in JavaScript.

Can anyone help?

(____)
(\/)
/-------\/
/ | Mwh ||
- ||----||
 
D

David

It depends on how your calling the args. It will add the numbers correctly
if they are actual numbers so in your function call do not use any
quotations..

additionCalculator(10,20)

If you need to use quotations you'll have to parse the numbers because they
are considered as "text" and not actual numbers.

var total = parseInt(num1)+parseInt(num2);

additionCalculator('10','20')

David
 
V

Vic Sowers

mwh said:
Hi.

I am making a calculator kind of calculator using javascript, and I am
trying to add numbers together. Example:

function additionCalculator(num1, num2){
var total = num1+num2
return total
}

I use this script to add num1 and num2 together. But, it just puts them
together, it does not add them together.

I also want to subtract and divide in my calculator, and I don't know
how to use these in JavaScript.

function additionCalculator(num1, num2) {
var total = Number(num1)+Number(num2);
return total;
}
 
Z

Zifud

mwh said:
Hi.

I am making a calculator kind of calculator using javascript, and I am
trying to add numbers together. Example:

function additionCalculator(num1, num2){
var total = num1+num2
return total
}

I use this script to add num1 and num2 together. But, it just puts them
together, it does not add them together.

I also want to subtract and divide in my calculator, and I don't know
how to use these in JavaScript.

<URL:http://www.jibbering.com/faq/#FAQ4_21>
 
D

Dr John Stockton

JRS: In article <wZeee.1648$Vu.1318@trnddc07>, dated Thu, 5 May 2005
It depends on how your calling the args. It will add the numbers correctly
if they are actual numbers so in your function call do not use any
quotations..

additionCalculator(10,20)

If you need to use quotations you'll have to parse the numbers because they
are considered as "text" and not actual numbers.

var total = parseInt(num1)+parseInt(num2);

additionCalculator('10','20')

You've only just arrived here; you need to look around a bit in order to
be sure of giving sound advice. Read the newsgroup FAQ, learn how to
format a news reply (Sec 2.3), learn why parseInt should only rarely be
used without a radix (4.12), and why it is not needed (4.21). See
signature.

var total = +num1 + +num2 // or = + num1 + + num2
will suffice. The + which are adjacent to the letter n can only be
unary-plus, the output of which is always a number.

Given that the inputs probably cone from a control, it's probably best
to "capture" them with lines like

var Num1 = + document ... Num1Input.value

and then work with Num1 - after reading the FAQ notes on Form Access
and/or Square Brackets.
 
D

David

You've only just arrived here; you need to look around a bit in order to
be sure of giving sound advice. Read the newsgroup FAQ, learn how to
format a news reply (Sec 2.3), learn why parseInt should only rarely be
used without a radix (4.12), and why it is not needed (4.21). See
signature.



Points taken .. David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top