473,382 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Expressions help

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 ||
- ||----||

Jul 23 '05 #1
5 1191
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

"mwh" <a_********@yahoo.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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 ||
- ||----||

Jul 23 '05 #2

"mwh" <a_********@yahoo.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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;
}
Jul 23 '05 #3
mwh wrote:
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>

--
Zif
Jul 23 '05 #4
JRS: In article <wZeee.1648$Vu.1318@trnddc07>, dated Thu, 5 May 2005
01:50:20, seen in news:comp.lang.javascript, David <ri***@dd.com> posted
:
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.
"mwh" <a_********@yahoo.com> wrote in message
news:11**********************@g14g2000cwa.googleg roups.com...
Hi.
...


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.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #5
"Dr John Stockton" <sp**@merlyn.demon.co.uk> wrote in message
news:bR**************@merlyn.demon.co.uk...
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
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Martin Robins | last post by:
I am trying to parse a string that is similar in form to an OLEDB connection string using regular expressions; in principle it is working, but certain character combinations in the string being...
7
by: mark | last post by:
Access 2000: I creating a report that has a record source built by the user who selects the WHERE values. An example is: SELECT * FROM CHARGELOG WHERE STDATE Between #10/27/2003# And...
4
by: GenoJoe | last post by:
If you are not new to VB.NET but are new to regular expressions, you need to get a free copy of "Pragmatic Guide to Regular Expressions for VB.NET Programmers". I wrote this guide because all of the...
2
by: cleo | last post by:
I'm experimenting with Regular Expressions and Windows Forms. Frequently I want a value to be either a valid pattern or empty. For example a Zip code must be 5 digits or may be empty. I know that...
7
by: norton | last post by:
Hello, Does any one know how to extact the following text into 4 different groups(namely Date, Artist, Album and Quality)? - Artist - Album Artist - Album - Artist - Album - Artist -...
4
by: Együd Csaba | last post by:
Hi All, I'd like to "compress" the following two filter expressions into one - assuming that it makes sense regarding query execution performance. .... where (adate LIKE "2004.01.10 __:30" or...
3
by: a | last post by:
I'm a newbie needing to use some Regular Expressions in PHP. Can I safely use the results of my tests using 'The Regex Coach' (http://www.weitz.de/regex-coach/index.html) Are the Regular...
9
by: G .Net | last post by:
Hi Can anybody help with a problem with Expressions for DataColumns Suppose I have a table with three fields A, B and C. I want an expression in column C so that C = 2 * A if A is not...
3
by: Zeba | last post by:
Hi guys, I need some help regarding regular expressions. Consider the following statement : System.Text.RegularExpressions.Match match =...
0
by: peridian | last post by:
Hi, I wanted a web page where I could post code to, and have it appear in coloured formatting based on the context of the code. Most of the techniques I have seen for this involve complex use...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.