435,395 Members | 2,524 Online
+ 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 Jul 20 '05 #1
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

 P: n/a "Fabian" 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. 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: '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 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 , Fabian 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

 P: n/a JRS: In article , seen in news:comp.lang.javascript, Allen Thompson posted at Sat, 13 Dec 2003 03:12:10 :-sorry for the simple question, haven't done this in a while. when I use thefollowing script it keeps displaying the value of "x" like a string. forexample, if I type the number 7 in the prompt, it displays the result as 721instead 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 © Jim Ley's FAQ for news:comp.lang.javascript Jsc maths, dates, sources. 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 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: '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

 P: n/a Lasse Reichstein Nielsen wrote: Thomas 'PointedEars' Lahn 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 convertthe 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!" writes: var x = (+y) + 3 * 7;That'll still give the same result, unfortunately. Whatcha need to do isthis; 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 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: '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 isthis; Did you bother testing that before spouting off that it would give the sameresults? 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 stringformat is quicker at converting it to a number than parseInt is, which youuse 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