By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,987 Members | 1,607 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,987 IT Pros & Developers. It's quick & easy.

Strings and numbers...?

P: n/a
I'm having difficulties arithmetically manipulating form element values.

I've entered data into the form, and I fetch them using a js, as:

p7Left = Number(document.form1.elements["p7_left"].value);
p7Right = Number(document.form1.elements["p7_right"].value);
...
...
scoreLeft = Number(document.form1.elements["left_score"].value);
scoreRight = Number(document.form1.elements["right_score"].value);

Then I add these as follows:

scoreLeft = Number(scoreLeft + p&Left + ...... +);

This does what I want; without the operator 'Number' I get a concatination
of the various variables (as expected). Is there some way of globally
defining all variables as numbers instead of strings?

--
Ed Jay (remove M to respond by email)
Feb 3 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Ed Jay wrote:
This does what I want; without the operator 'Number' I get a
concatination of the various variables (as expected). Is there some
way of globally defining all variables as numbers instead of strings?


No.

But you should see http://www.JavascriptToolbox.com/bestpractices/ for an
entry about this topic and the best way to solve it.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Feb 3 '06 #2

P: n/a
"Matt Kruse" <ne********@mattkruse.com> wrote:
Ed Jay wrote:
This does what I want; without the operator 'Number' I get a
concatination of the various variables (as expected). Is there some
way of globally defining all variables as numbers instead of strings?


No.

But you should see http://www.JavascriptToolbox.com/bestpractices/ for an
entry about this topic and the best way to solve it.


Thanks.

--
Ed Jay (remove M to respond by email)
Feb 3 '06 #3

P: n/a
Ed Jay <ed***@aes-intl.com> writes:
I'm having difficulties arithmetically manipulating form element values. .... scoreRight = Number(document.form1.elements["right_score"].value); .... scoreLeft = Number(scoreLeft + p&Left + ...... +);

This does what I want; without the operator 'Number' I get a concatination
of the various variables (as expected). Is there some way of globally
defining all variables as numbers instead of strings?


No.
In the current version of Javascript used in browsers, variables have no
type. Only values have type. If you put a string into a variable, that
is also what you get out.

You are doing the right thing in converting form control values to
numbers. You should then remember what types your values contain.
The result of adding the numbers from the first variables is already
a number, so you don't need to convert it again before assingning to
scoreLeft.

Good luck
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Feb 4 '06 #4

P: n/a
Lasse Reichstein Nielsen <lr*@hotpop.com> wrote:
Ed Jay <ed***@aes-intl.com> writes:
I'm having difficulties arithmetically manipulating form element values.

...
scoreRight = Number(document.form1.elements["right_score"].value);

...
scoreLeft = Number(scoreLeft + p&Left + ...... +);

This does what I want; without the operator 'Number' I get a concatination
of the various variables (as expected). Is there some way of globally
defining all variables as numbers instead of strings?


No.
In the current version of Javascript used in browsers, variables have no
type. Only values have type. If you put a string into a variable, that
is also what you get out.

You are doing the right thing in converting form control values to
numbers. You should then remember what types your values contain.
The result of adding the numbers from the first variables is already
a number, so you don't need to convert it again before assingning to
scoreLeft.

Good luck

Thanks, Lasse.

--
Ed Jay (remove M to respond by email)
Feb 4 '06 #5

P: n/a
JRS: In article <td********************************@4ax.com>, dated
Fri, 3 Feb 2006 11:48:02 remote, seen in news:comp.lang.javascript, Ed
Jay <ed***@aes-intl.com> posted :
I'm having difficulties arithmetically manipulating form element values.

I've entered data into the form, and I fetch them using a js, as:

p7Left = Number(document.form1.elements["p7_left"].value);
p7Right = Number(document.form1.elements["p7_right"].value);
..
..
scoreLeft = Number(document.form1.elements["left_score"].value);
scoreRight = Number(document.form1.elements["right_score"].value);

Then I add these as follows:

scoreLeft = Number(scoreLeft + p&Left + ...... +);

This does what I want; without the operator 'Number' I get a concatination
of the various variables (as expected). Is there some way of globally
defining all variables as numbers instead of strings?


You should try reading the newsgroup FAQ; the indexing for your question
is obvious, leading to 4.21.

Rather than Number(...) you can best use +... .

BTW, & != 7 .

YSCIB.

--
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.
Feb 4 '06 #6

P: n/a
Dr John Stockton <jr*@merlyn.demon.co.uk> wrote:
JRS: In article <td********************************@4ax.com>, dated
Fri, 3 Feb 2006 11:48:02 remote, seen in news:comp.lang.javascript, Ed
Jay <ed***@aes-intl.com> posted :
I'm having difficulties arithmetically manipulating form element values.

I've entered data into the form, and I fetch them using a js, as:

p7Left = Number(document.form1.elements["p7_left"].value);
p7Right = Number(document.form1.elements["p7_right"].value);
..
..
scoreLeft = Number(document.form1.elements["left_score"].value);
scoreRight = Number(document.form1.elements["right_score"].value);

Then I add these as follows:

scoreLeft = Number(scoreLeft + p&Left + ...... +);

This does what I want; without the operator 'Number' I get a concatination
of the various variables (as expected). Is there some way of globally
defining all variables as numbers instead of strings?
You should try reading the newsgroup FAQ; the indexing for your question
is obvious, leading to 4.21.

Rather than Number(...) you can best use +... .

BTW, & != 7 .


You have good eyes. :-)
YSCIB.

Thanks. I ended up subtracting '0' from every value.

--
Ed Jay (remove M to respond by email)
Feb 4 '06 #7

P: n/a
Ed Jay said the following on 2/4/2006 6:00 PM:
Dr John Stockton <jr*@merlyn.demon.co.uk> wrote:
JRS: In article <td********************************@4ax.com>, dated
Fri, 3 Feb 2006 11:48:02 remote, seen in news:comp.lang.javascript, Ed
Jay <ed***@aes-intl.com> posted :
I'm having difficulties arithmetically manipulating form element values.

I've entered data into the form, and I fetch them using a js, as:

p7Left = Number(document.form1.elements["p7_left"].value);
p7Right = Number(document.form1.elements["p7_right"].value);
..
..
scoreLeft = Number(document.form1.elements["left_score"].value);
scoreRight = Number(document.form1.elements["right_score"].value);

Then I add these as follows:

scoreLeft = Number(scoreLeft + p&Left + ...... +);

This does what I want; without the operator 'Number' I get a concatination
of the various variables (as expected). Is there some way of globally
defining all variables as numbers instead of strings?

You should try reading the newsgroup FAQ; the indexing for your question
is obvious, leading to 4.21.

Rather than Number(...) you can best use +... .

BTW, & != 7 .


You have good eyes. :-)
YSCIB.

Thanks. I ended up subtracting '0' from every value.


scoreLeft = +document.form1.elements["left_score"].value;

Is more efficient than subtracting zero.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 5 '06 #8

P: n/a
Randy Webb <Hi************@aol.com> wrote:
Ed Jay said the following on 2/4/2006 6:00 PM:
Dr John Stockton <jr*@merlyn.demon.co.uk> wrote:
JRS: In article <td********************************@4ax.com>, dated
Fri, 3 Feb 2006 11:48:02 remote, seen in news:comp.lang.javascript, Ed
Jay <ed***@aes-intl.com> posted :
I'm having difficulties arithmetically manipulating form element values.

I've entered data into the form, and I fetch them using a js, as:

p7Left = Number(document.form1.elements["p7_left"].value);
p7Right = Number(document.form1.elements["p7_right"].value);
..
..
scoreLeft = Number(document.form1.elements["left_score"].value);
scoreRight = Number(document.form1.elements["right_score"].value);

Then I add these as follows:

scoreLeft = Number(scoreLeft + p&Left + ...... +);

This does what I want; without the operator 'Number' I get a concatination
of the various variables (as expected). Is there some way of globally
defining all variables as numbers instead of strings?
You should try reading the newsgroup FAQ; the indexing for your question
is obvious, leading to 4.21.

Rather than Number(...) you can best use +... .

BTW, & != 7 .


You have good eyes. :-)
YSCIB.

Thanks. I ended up subtracting '0' from every value.


scoreLeft = +document.form1.elements["left_score"].value;

Is more efficient than subtracting zero.


Thank you.

--
Ed Jay (remove M to respond by email)
Feb 5 '06 #9

P: n/a
JRS: In article <ee********************************@4ax.com>, dated
Sat, 4 Feb 2006 15:00:00 remote, seen in news:comp.lang.javascript, Ed
Jay <ed***@aes-intl.com> posted :
Thanks. I ended up subtracting '0' from every value.


You should read the FAQ more carefully; there is no need to subtract
zero, and your solution uses twice as many characters as is necessary.
The matter is also dealt with on my Web site.

--
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.
Feb 5 '06 #10

P: n/a
Dr John Stockton <jr*@merlyn.demon.co.uk> wrote:
JRS: In article <ee********************************@4ax.com>, dated
Sat, 4 Feb 2006 15:00:00 remote, seen in news:comp.lang.javascript, Ed
Jay <ed***@aes-intl.com> posted :
Thanks. I ended up subtracting '0' from every value.

You should read the FAQ more carefully;


I agree.
there is no need to subtract
zero, and your solution uses twice as many characters as is necessary.
The matter is also dealt with on my Web site.

I ended up following Randy's suggestion and put + before each value.

--
Ed Jay (remove M to respond by email)
Feb 5 '06 #11

P: n/a
Dr John Stockton wrote:
there is no need to subtract
zero, and your solution uses twice as many characters as is necessary.


Are you really using that as an argument? :)

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Feb 5 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.