PDA

View Full Version : FF1 and IE6 JavaScript parsers/readers/w-e are screwed up...



Jesdisciple
07-25-2006, 08:10 PM
I was writing a code to calculate any three numbers with each of the four arithmetic signs (+,-,*,/) alternating positions in the two slots. A simplified version of the troublesome code is below. In both FF1 and IE6, it comes up with the number 11 for (x+x)/x and 21 for (10+5)/5, and FF1 then deletes the text. (I thought maybe it was just the IE6 parser, so I tried FF1. I'm assuming FF1 has a completely separate problem.) I find that if I feed the number directly into the JS -(5+5)/5-, it comes up with 2, the correct answer, but can't handle input variables. It's apparently calculating y/x and z/x and adding the quotients as strings, so (10+5)/5="2"+"1" and (5+5)/5="1"+"1".

<html>
<head>
<title>
Oddness...
</title>
<script type="text/javascript">
function calc(x,y,z){
var problemChild=y+z
var yzxaddidiv=problemChild/x
ABC=Math.round(yzxaddidiv)
if(yzxaddidiv!=ABC){
yzxaddidiv="N/A"
}
document.getElementById("output").value=yzxaddidiv
}
</script>
</head>
<body>
<form id="calc">
<input type=text id="x" />
<input type=text id="y" />
<input type=text id="z" />
<button onClick=calc(x.value,y.value,z.value)>Calculate</button>
</form>
<form id="result">
<textarea id="output" rows="10" cols="30"></textarea>
</form>
</body>
</html>Is this everyone's computer or just mine? Anyone know how to get around it?

Twey
07-25-2006, 08:40 PM
Not at all surprising. Use parseInt(y) to convert y to integer form.

Jesdisciple
07-25-2006, 09:02 PM
Thanks. I appreciate that very much. I've never needed that function before and some things I thought were legal are now illegal in JavaScript. Did Netscape do an online update of JavaScript? I know I never approved one...

Twey
07-25-2006, 09:47 PM
Heh, what century are you from?
So-called "Javascript" these days is a variety of implementations of ECMA-262, and I'm afraid strings take precedence in a + operation, no matter what you prefer. :p

We have things called HTML validators (http://validator.w3.org/) these days too. :)

Jesdisciple
07-25-2006, 10:01 PM
I only recently resumed coding, after a hiatus of about a year, probably. I learned my JS, XML, HTML, [insert client-side language here] at W3Schools and high school WebMastering class, and I was told a string required quotes. And what in the world is ECMA? In answer to your question, I'm from the 20th century, lol.

Twey
07-25-2006, 10:10 PM
And what in the world is ECMA?ECMA (http://www.ecma-international.org/) is an international standards regulation body, much like ISO, IETF, and ANSI. This (http://www.ecma-international.org/publications/standards/Ecma-262.htm) formidable beastie is ECMA-262. It's the accepted standard to which the various forms of "Javascript" (JScript, JavaScript, &c.) are meant to conform. Mike (mwinter) has explained it several times, and probably a lot better than I could, if you search.
I was told a string required quotes.Only when creating a new one. Form element values, among other things, are returned as strings.

W3Schools has obtained quite a reputation recently for "errors, omissions and deceit!" as frequently announced by the resident #javascript bot. However, I continue to link to it as reference material because the URLs are easy to remember. :p