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

why math wont work?

P: n/a
sorry for the simple question, haven't done this in a while. when I use the
following script it keeps displaying the value of "x" like a string. for
example, if I type the number 7 in the prompt, it displays the result as 721
instead of the answer I want which is 28. what am I doing wrong.
hanks -Allen Thompson

<html>
<body>
<script language="javascript">
var y=window.prompt();
var x=y+3*7;
document.write(x);
</script>
</body>
</html>
Jul 20 '05 #1
Share this Question
Share on Google+
19 Replies


P: n/a
Allen Thompson hu kiteb:
sorry for the simple question, haven't done this in a while. when I
use the following script it keeps displaying the value of "x" like a
string. for example, if I type the number 7 in the prompt, it
displays the result as 721 instead of the answer I want which is 28.
what am I doing wrong.
hanks -Allen Thompson

<html>
<body>
<script language="javascript">
var y=window.prompt();
var x=y+3*7;


var x = (y * 1) + (3*7);

Unless you force js to recognise a variable as a number, half teh time
it assumes it is text, and teh + operator is both addition and
concatenation.

If I had my way, + would be reserved as addition only, and a separate
operator created for concatenation.
--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

Jul 20 '05 #2

P: n/a
"Fabian" <la****@hotmail.com> writes:
var x = (y * 1) + (3*7);
Or
var x = Number(y)+3*7
or one of the other ways to convert a string into a number.

The important point is that y contains a string (prompt returns a
string), not a number, and that adding a number to a string will
always convert the number to a second string and concatenate the
strings.

<URL:http://jibbering.com/faq/#FAQ4_21>
Unless you force js to recognise a variable as a number, half teh time
it assumes it is text, and teh + operator is both addition and
concatenation.
It's not *that* bad. Javascript won't assume something half of the time.
It consistently treats strings as strings and numbers as numbers, and
when adding something to a string, it converts the other part to a string
and concatenates.

I would rather say that half of the time, you get away with forgetting
that you have a string containing a numeral, because Javascript converts
it to a number before doing arithmetic operations on it. E.g.,
var y = "42";
var x1 = y * 2; // 84
var x2 = y / 2; // 21
var x3 = y - 37; // 5
var x4 = y + 10; // 4210 - addition doesn't convert arguments to numbers.
In the first four cases, you can safely forget that y contains a string.
It's just that you can't always, so you shouldn't.
If I had my way, + would be reserved as addition only, and a separate
operator created for concatenation.


I wouldn't complain about that. I guess the behavior is stolen from Java,
where + also does concatenation on strings.
/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.'
Jul 20 '05 #3

P: n/a
> when I use the
following script it keeps displaying the value of "x" like a string. for
example, if I type the number 7 in the prompt, it displays the result as 721
instead of the answer I want which is 28. what am I doing wrong.
hanks -Allen Thompson

<html>
<body>
<script>
var y=window.prompt();
var x=y+3*7;
document.write(x);
</script>
</body>
</html>


prompt() returns a string. Before using the '+' operator, you need to convert
the string to a number.

var x = (+y) + 3 * 7;

http://www.crockford.com/javascript/survey.html

Jul 20 '05 #4

P: n/a
In article <br************@ID-174912.news.uni-berlin.de>, Fabian
<la****@hotmail.com> wrote:
Allen Thompson hu kiteb:
sorry for the simple question, haven't done this in a while. when I
use the following script it keeps displaying the value of "x" like a
string. for example, if I type the number 7 in the prompt, it
displays the result as 721 instead of the answer I want which is 28.
what am I doing wrong.
hanks -Allen Thompson

<html>
<body>
<script language="javascript">
var y=window.prompt();
var x=y+3*7;


var x = (y * 1) + (3*7);

<snip>

Just use var x=+y+3*7;

If the y was at the other end of the formula then
var x=3*7+(+y)

--
Dennis M. Marks
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 20 '05 #5

P: n/a
JRS: In article <es************@newsread2.news.atl.earthlink.net >, seen
in news:comp.lang.javascript, Allen Thompson <ge********@mindspring.com>
posted at Sat, 13 Dec 2003 03:12:10 :-
sorry for the simple question, haven't done this in a while. when I use the
following script it keeps displaying the value of "x" like a string. for
example, if I type the number 7 in the prompt, it displays the result as 721
instead of the answer I want which is 28. what am I doing wrong.


You basic error lies in omitting to read the newsgroup FAQ.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> Jsc maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/Jsc/&c, FAQ topics, links.
Jul 20 '05 #6

P: n/a
Dennis M. Marks wrote:
If the y was at the other end of the formula then
var x=3*7+(+y)


JFTR:

var x = 3*7+ +y;

is also possible but more error-catching.
PointedEars
Jul 20 '05 #7

P: n/a
Douglas Crockford wrote:
prompt() returns a string.


It returns a string if the user confirms. Otherwise
it returns `null', no matter what has been typed.
PointedEars
Jul 20 '05 #8

P: n/a
Thomas 'PointedEars' Lahn <Po*********@web.de> writes:
Douglas Crockford wrote:
prompt() returns a string.


It returns a string if the user confirms. Otherwise
it returns `null', no matter what has been typed.


That is browser dependent. Opera returns "undefined", not "null".
Bot IE and Mozilla do return "null".

/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.'
Jul 20 '05 #9

P: n/a
Waaaaay back on 13-Dec-03 11:26:28, Douglas Crockford said this about Re: why math wont work?:
following script it keeps displaying the value of "x" like a string. for
example, if I type the number 7 in the prompt, it displays the result as
721 instead of the answer I want which is 28. what am I doing wrong. hanks
-Allen Thompson

<html>
<body>
<script>
var y=window.prompt();
var x=y+3*7;
document.write(x);
prompt() returns a string. Before using the '+' operator, you need to convert
the string to a number. var x = (+y) + 3 * 7;


That'll still give the same result, unfortunately. Whatcha need to do is this;

var x = parseInt(y)+3*7;

That should work.

--
da****@banana-and-louie.org * dauber.50megs.com
* ICQ: 28677921 * YIM: dau_ber * AIM: ddaauubbeerr

Jul 20 '05 #10

P: n/a
Lasse Reichstein Nielsen wrote:
Thomas 'PointedEars' Lahn <Po*********@web.de> writes:
Douglas Crockford wrote:
prompt() returns a string.


It returns a string if the user confirms. Otherwise
it returns `null', no matter what has been typed.


That is browser dependent. Opera returns "undefined", not "null".


*Are* *you* *really* *really* *sure*? [psf 1.1]

My Opera/7.11 (Windows NT 5.0; U) [en] returns `null', too.

Done the following tests in the Location Bar:

javascript:alert(prompt("bla")) // `null' if canceled
javascript:alert(typeof prompt("bla")) // `object' if canceled
PointedEars
Jul 20 '05 #11

P: n/a
Dauber! wrote:
Waaaaay back on 13-Dec-03 11:26:28, Douglas Crockford said this about Re: why math wont work?:
The recommended, if not even standardized, maximum is 78 characters
per line. Besides, about 74.7% of your attribution is composed of
superfluous information.
prompt() returns a string. Before using the '+' operator, you need to convert
the string to a number.

var x = (+y) + 3 * 7;


That'll still give the same result, unfortunately.


Not in my UAs. But I think you do not
want to be taken seriously, so go away.
Whatcha need to do is this;

var x = parseInt(y)+3*7;

That should work.
See the FAQ about parseInt(...).

And of course it will work, but the above
is about 6 times faster and more reliable.
--


The trailing space is missing.
PointedEars
Jul 20 '05 #12

P: n/a
In article <80**************************@verizon.SPAMISFOREAT ING.net.invalid>,
"Dauber!" <da*****@verizon.SPAMISFOREATING.net.invalid> writes:
var x = (+y) + 3 * 7;
That'll still give the same result, unfortunately. Whatcha need to do is
this;


Did you bother testing that before spouting off that it would give the same
results?
It is *well* known in this group that +y where y is a number in a string format
is quicker at converting it to a number than parseInt is, which you use
incorrectly.

var x = parseInt(y)+3*7;

That should work.


As long as y doesn't start with a 0 or 0x format.

http://www.jibbering.com/faq/#FAQ4_12

--
Randy
Jul 20 '05 #13

P: n/a
Thomas 'PointedEars' Lahn <Po*********@web.de> writes:
*Are* *you* *really* *really* *sure*? [psf 1.1]
*Yes* *I* *am*!
My Opera/7.11 (Windows NT 5.0; U) [en] returns `null', too.
My Opera/7.23 (Windows NT 5.1; U) [en] returns 'undefined'
javascript:alert(typeof prompt("bla")) // `object' if canceled


on this exact test.

/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.'
Jul 20 '05 #14

P: n/a
Waaaaay back on 14-Dec-03 19:16:53, HikksNotAtHome said this about Re: why math wont work?:
var x = (+y) + 3 * 7;
That'll still give the same result, unfortunately. Whatcha need to do is
this;

Did you bother testing that before spouting off that it would give the same
results?
Uhhh....yes. Believe it or not, despite popular belief, not all browsers
interpret JavaScript the same way, including two of the ones I use.
It is *well* known in this group that +y where y is a number in a string
format is quicker at converting it to a number than parseInt is, which you
use incorrectly.


Funny...two references I checked, including one that helped me get an A in
my JavaScript programming class that I took for my webmaster admin
certification, indicates that it's perfectly valid; in fact, I'm given
several overloaded versions tht account for "0x" format as well as binary
and octal...

--
da****@banana-and-louie.org
* ICQ: 28677921 * YIM: dau_ber * AIM: ddaauubbeerr

Jul 20 '05 #15

P: n/a
"Dauber!" <da*****@verizon.SPAMISFOREATING.net.invalid> writes:
Uhhh....yes. Believe it or not, despite popular belief, not all browsers
interpret JavaScript the same way, including two of the ones I use.
Could you be persuaded to tell us what browsers those are?

If prefix plus isn't working consistently accross browsers, we should
ofcourse make a note of it.
The expected behavior (from the ECMAScript standard) is to be equivalent
to calling the Number function, but without the overhead of a function call.
Funny...two references I checked, including one that helped me get an A in
my JavaScript programming class that I took for my webmaster admin
certification, indicates that it's perfectly valid;
It is valid.
in fact, I'm given several overloaded versions tht account for "0x"
format as well as binary and octal...


Overloaded?

Anyway, it is the collective experience in this group, that people
using parseInt most likely expect it to convert from base 10. That is
why we recommend also passing the radix to the function, to avoid
surprises when a user entered number starts with zero.

If you know what you are doing, using parseInt without the radix is
correct. It's just not good advice to a beginner without telling why
"089" converts to 0 in some browsers and 89 in others.

Not all browsers accept octal numbers as arguments to parseInt (as,
indeed, they shouldn't according to ECMA 262). But IE does. So any way
you put it, omitting the radix will make your page behave inconsistently
across browsers.

/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.'
Jul 20 '05 #16

P: n/a
In article <99**************************@verizon.SPAMISFOREAT ING.net.invalid>,
"Dauber!" <da*****@verizon.SPAMISFOREATING.net.invalid> writes:
var x = (+y) + 3 * 7;

That'll still give the same result, unfortunately. Whatcha need to do is
this;
Did you bother testing that before spouting off that it would give the same
results?


Uhhh....yes. Believe it or not, despite popular belief, not all browsers
interpret JavaScript the same way, including two of the ones I use.


So you have a browser that doesn't convert a string to a number using + ?
Ex:

x = "3";
y = +x;
alert(typeof(y)); //browser that alerts string instead of number??

I am not in a position to doubt you about it, would be curious to know of it
though. I know of at least 130 browsers so one might not convert it to number.
If it doesn't, then it would definitely be worth knowing about.
It is *well* known in this group that +y where y is a number in a string
format is quicker at converting it to a number than parseInt is, which you
use incorrectly.


Funny...two references I checked, including one that helped me get an A in
my JavaScript programming class that I took for my webmaster admin
certification, indicates that it's perfectly valid; in fact, I'm given
several overloaded versions tht account for "0x" format as well as binary
and octal...


valid in the sense of a limited subset of numbers, it works, then yes its
valid.

alert(parseInt('08'))
And you get 0, not 8.
The incorrectness of your use was the lack of a radix (base 10).

<URL:
http://msdn.microsoft.com/library/de...n-us/jscript7/
html/jsmthparseint.asp />
--
Randy
Jul 20 '05 #17

P: n/a
Waaaaay back on 15-Dec-03 07:31:06, HikksNotAtHome said this about Re: why math wont work?:
Uhhh....yes. Believe it or not, despite popular belief, not all browsers
interpret JavaScript the same way, including two of the ones I use.
So you have a browser that doesn't convert a string to a number using + ?
Ex: x = "3";
y = +x;
alert(typeof(y)); //browser that alerts string instead of number??
The latest Voyager for Amiga. Yeah, I know, the vast majority of users don't
use Amiga, but I'm a very stubborn advocate for universality. :) Not the
greatest browser in the world, either...fast, but not great.
Funny...two references I checked, including one that helped me get an A in
my JavaScript programming class that I took for my webmaster admin
certification, indicates that it's perfectly valid; in fact, I'm given
several overloaded versions tht account for "0x" format as well as binary
and octal...

valid in the sense of a limited subset of numbers, it works, then yes its
valid.


DUH!....forgot the "10" parameter. <blush>

--
da****@banana-and-louie.oops.org
* ICQ: 28677921 * YIM: dau_ber * AIM: ddaauubbeerr

Jul 20 '05 #18

P: n/a
"Dauber!" <da*****@verizon.SPAMISFOREATING.net.invalid> wrote in message
news:1147.479T1499T6255001da*****@verizon.SPAMISFO REATING.net.invalid...
<snip>
So you have a browser that doesn't convert a string to
a number using + ?
Ex:
x = "3";
y = +x;
alert(typeof(y)); //browser that alerts string instead of number??


The latest Voyager for Amiga.


Does it claim its script language implementation is ECMA 262 compliant?
If not why not (the current version has been around since 1999) and if
they do, have they been told they have a bug to fix?

How do the other type-converting math operators perform? Does y = -x;
also return a string?
Yeah, I know, the vast majority of users don't
use Amiga, but I'm a very stubborn advocate for
universality. :) ...

<snip>

You will find that the majority here are also advocates of universality,
but if a browser manufactures decides to implement there own scripting
language without regard for either the published standard or precedence
and then tries to execute it when provided with JavaScript/ECMA Script
source code there is only so much that can be done.

Richard.
Jul 20 '05 #19

P: n/a
Lasse Reichstein Nielsen wrote:
Thomas 'PointedEars' Lahn <Po*********@web.de> writes:
My Opera/7.11 (Windows NT 5.0; U) [en] returns `null', too.


My Opera/7.23 (Windows NT 5.1; U) [en] returns 'undefined'
javascript:alert(typeof prompt("bla")) // `object' if canceled


on this exact test.


Straynge. [psf 4.15]
PointedEars :)
Jul 20 '05 #20

This discussion thread is closed

Replies have been disabled for this discussion.