471,338 Members | 971 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

'Number Expected' problems

Hi,

I'm having some trouble with javascript code of mine:

When the script runs, I vget an error in line 119: Number Expected.
I'm not sure as to why this is happening. Any advice would be helpful.

The source code is at the bottom of the post.

Thankyou very much,
B. Fletcher
http://themaster5000.netfirms.com

Source code (Copyright (c) 2005 Brenton Fletcher):
<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
<script language="JavaScript1.5">
<!--
var terms = new Array();
var correct = new Array();
var positionAnswer = 0;
var count = 0;
var total = 0;

var usedCorrect = new Array();
var usedTerms = new Array();

terms[0] = "largamente";
correct[0] = "broadly";

terms[1] = "larghetto";
correct[1] = "rather broadly";

terms[2] = "prestissimo";
correct[2] = "extremely fast or as fast as possible";

terms[3] = "con moto";
correct[3] = "with movement";

// more elements follow, removed because they are unimportant I think


var randoms = new Array();
function sortB(a, b)
{
return parseInt(String.valueOf(randoms[r++]));
}

var r = 0;
function initialise()
{
for(var z = 0; z < terms.length; z++)
{
randoms[z] = Math.round(Math.random()*2) - 1;
}
alert(randoms.toString());

terms = terms.sort(sortB);

r = 0;

correct = correct.sort(sortB);

question();
}

var usedCorrect = new Array();
var chosenTerm;
var chosenCorrect;
function question()
{
if(terms.length == 0)
{
result();
return;
}

while(usedCorrect.length > 0)
{
usedCorrect.pop();
}

chosenTerm = terms.pop();
chosenCorrect = correct.pop();

termCell.innerHTML = "<font size='5'>" + chosenTerm + "</font>";

positionAnswer = Math.round(Math.random()*4);

a.innerHTML = correct[getRandomCorrect()];
b.innerHTML = correct[getRandomCorrect()];
c.innerHTML = correct[getRandomCorrect()];
d.innerHTML = correct[getRandomCorrect()];
e.innerHTML = correct[getRandomCorrect()];

switch(positionAnswer)
{
case 0: a.innerHTML = chosenCorrect; break;
case 1: b.innerHTML = chosenCorrect; break;
case 2: c.innerHTML = chosenCorrect; break;
case 3: d.innerHTML = chosenCorrect; break;
default: e.innerHTML = chosenCorrect;
}
}

function answer()
{
var temp;
if(R1[positionAnswer].checked)
{
count++;
feedback.innerHTML = "<font color='#00ff00'>Correct!</font>";
}
else
{
feedback.innerHTML = "<font color='#ff0000'>Incorrect:<br>The correct
translation of \"" + chosenTerm + "\" is \"" + chosenCorrect + "\".</font>";
}
total++;

for(var i = 0; i < 5; i++)
{
R1[i].checked = false;
}

question();
}

function result()
{
feedback.innerHTML = "Percentage correct: " + (total/count) + "%";
count = 0;
total = 0;
}

function getRandomCorrect()
{
var input = Math.round(Math.random()*(correct.length - 1));

while(searchCorrect(input))
{
input = Math.round(Math.random()*(correct.length - 1));
}

usedCorrect.push(input);
return input;
}

function searchCorrect(a)
{
for(var i = 0; i < usedCorrect.length; i++)
{
if(usedCorrect[i] == a)
{
return true;
}
}
return false;
}
//-->
</script>
</head>

<body onload="">

<span name="termCell" id="termCell"></span>
<p>
Left-Click on the English meaning:</p>
<p>
<input type="radio" value="0" checked name="R1"><span name="a"
id="a"></span><br>
<input type="radio" name="R1" value="1"><span name="b" id="b"></span><br>
<input type="radio" name="R1" value="2"><span name="c" id="c"></span><br>
<input type="radio" name="R1" value="3"><span name="d" id="d"></span><br>
<input type="radio" name="R1" value="4"><span name="e" id="e"></span></p>
Last question was: <span name="feedback" id="feedback"></span>
<p><input type="button" value="Next Question" name="B1" onclick=""></p>

</body>

</html>

Jul 23 '05 #1
4 1902
Lee
B. Fletcher said:

Hi,

I'm having some trouble with javascript code of mine:

When the script runs, I vget an error in line 119: Number Expected.
I'm not sure as to why this is happening. Any advice would be helpful.
I don't see that error and don't have any way of knowing which line is 119.
However, if you read the FAQ for this newsgroup, you'll learn why you should
[almost] never use Math.round() while generating random numbers, find a better
way to shuffle an array into random order, etc.

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

Also, this line:
return parseInt(String.valueOf(randoms[r++]));


seems to be explicitely converting a number to a string, and then converting it
back to a number again. I'm curious as to why that seems like a good idea.

Jul 23 '05 #2
Lee wrote:
B. Fletcher said: <snip> Also, this line:
return parseInt(String.valueOf(randoms[r++]));


seems to be explicitely converting a number to a string,
and then converting it back to a number again. I'm
curious as to why that seems like a good idea.


I am not so sure that is what it is doing. The - valueOf - method that
is being called is the method of the String function (the String object
constructor) and while the - valueOf - method of a String object
instance should return the string primitive value of that string, surly
the - valueOf - method of the - String - constructor is the one
inherited through - Function.prototype -. And as - Function.prototype -
has no - valueOf - property of its own the method called will be the one
from - Object.prototype -, which is specified as returning - this -; the
String constructor function in this case.

However, none of those - valueOf - methods take an argument anyway.

If - String.valueOf( whatever ) - returns a reference to a function
then - parseInt - will type-convert its argument to a string, which will
not resemble a number. And so - parseInt - will return - NaN -, which is
a number (sort of) but maybe not in the context of the
Array.prototype.sort method.

I recall a thread about 6 months ago in which IE producing a "Number
expected" error from Array.prototype.sort was discussed, but I cannot
remember the conclusion. Groups.google.com might be able to turn
something up (assuming they haven't completely messes google groups up
by now).

Richard.
Jul 23 '05 #3
JRS: In article <11***************@stitch.ispdr.net.au>, dated Thu, 6
Jan 2005 09:22:57, seen in news:comp.lang.javascript, B. Fletcher
<br****************@hotmail.com> posted :
I'm having some trouble with javascript code of mine:

When the script runs, I vget an error in line 119: Number Expected.
So which is line 119?
I'm not sure as to why this is happening. Any advice would be helpful.
Test smaller pieces at once.

<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
That seems a mistake.

<script language="JavaScript1.5">
Deprecated; see FAQ or Notes, and complain if it is not there :
use <script type="text/javascript">

// more elements follow, removed because they are unimportant I think
What you think is unimportant; you would not be asking if you knew
everything. You should have tested for the problem with the code as
posted; errors in code not shown are only of future interest.

return parseInt(String.valueOf(randoms[r++]));
That seems to be an obscure way of doing return NaN .
For me, the value of String.valueOf() is

function String() {
[native code]
}

for any parameter I've tried. BTW, parseInt is rarely justified, except
if the first argument contains more than just a number or the second is
a number other than 10.

randoms[z] = Math.round(Math.random()*2) - 1;
I think that generates -1 (25%), 0 (50%), +1 (25%). Maybe you want
that; otherwise, see FAQ 4.22.
alert(randoms.toString()); toString() seems superfluous.

termCell.innerHTML = "<font size='5'>" + chosenTerm + "</font>";
Presumably you want to go up two sizes? But ISTM possible that the user
does not have default=3. size='+2' or <big><big> might be safer; check.

positionAnswer = Math.round(Math.random()*4);
Gives 0 (12.5%), 1 2 3 (25%), 4 (12.5%). As before.

a.innerHTML = correct[getRandomCorrect()];
b.innerHTML = correct[getRandomCorrect()];
c.innerHTML = correct[getRandomCorrect()];
d.innerHTML = correct[getRandomCorrect()];
e.innerHTML = correct[getRandomCorrect()];
Sub-maximal browser compatibility there. FAQ 4.15.

switch(positionAnswer)
{
case 0: a.innerHTML = chosenCorrect; break;
case 1: b.innerHTML = chosenCorrect; break;
case 2: c.innerHTML = chosenCorrect; break;
case 3: d.innerHTML = chosenCorrect; break;
default: e.innerHTML = chosenCorrect;
}
There must be a better way of doing that, in one statement, with a
little redesign. Maybe call them S1 to S4, use getElementByID('S'+n),
and provide getElementByID for those who lack it.

feedback.innerHTML = "<font color='#ff0000'>Incorrect:<br>The correct
translation of \"" + chosenTerm + "\" is \"" + chosenCorrect + "\".</font>";
Don't let your software wrap lines when posting.

feedback.innerHTML = "Percentage correct: " + (total/count) + "%";


One wonders whether adding 100* might help.
Further repeated points are not re-commented on.
Read the newsgroup FAQ.

--
© 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 #4
Hello,

Thank you very much for your advice. I am sorryy, I know I should have read
the FAQ.

B. Fletcher

"Dr John Stockton" <sp**@merlyn.demon.co.uk> wrote in message
news:oL**************@merlyn.demon.co.uk...
JRS: In article <11***************@stitch.ispdr.net.au>, dated Thu, 6
Jan 2005 09:22:57, seen in news:comp.lang.javascript, B. Fletcher
<br****************@hotmail.com> posted :
I'm having some trouble with javascript code of mine:

When the script runs, I vget an error in line 119: Number Expected.
So which is line 119?
I'm not sure as to why this is happening. Any advice would be helpful.


Test smaller pieces at once.

<meta name="GENERATOR" content="Microsoft FrontPage 5.0">


That seems a mistake.

<script language="JavaScript1.5">


Deprecated; see FAQ or Notes, and complain if it is not there :
use <script type="text/javascript">

// more elements follow, removed because they are unimportant I think


What you think is unimportant; you would not be asking if you knew
everything. You should have tested for the problem with the code as
posted; errors in code not shown are only of future interest.

return parseInt(String.valueOf(randoms[r++]));


That seems to be an obscure way of doing return NaN .
For me, the value of String.valueOf() is

function String() {
[native code]
}

for any parameter I've tried. BTW, parseInt is rarely justified, except
if the first argument contains more than just a number or the second is
a number other than 10.

randoms[z] = Math.round(Math.random()*2) - 1;


I think that generates -1 (25%), 0 (50%), +1 (25%). Maybe you want
that; otherwise, see FAQ 4.22.
alert(randoms.toString());

toString() seems superfluous.

termCell.innerHTML = "<font size='5'>" + chosenTerm + "</font>";


Presumably you want to go up two sizes? But ISTM possible that the user
does not have default=3. size='+2' or <big><big> might be safer; check.

positionAnswer = Math.round(Math.random()*4);


Gives 0 (12.5%), 1 2 3 (25%), 4 (12.5%). As before.

a.innerHTML = correct[getRandomCorrect()];
b.innerHTML = correct[getRandomCorrect()];
c.innerHTML = correct[getRandomCorrect()];
d.innerHTML = correct[getRandomCorrect()];
e.innerHTML = correct[getRandomCorrect()];


Sub-maximal browser compatibility there. FAQ 4.15.

switch(positionAnswer)
{
case 0: a.innerHTML = chosenCorrect; break;
case 1: b.innerHTML = chosenCorrect; break;
case 2: c.innerHTML = chosenCorrect; break;
case 3: d.innerHTML = chosenCorrect; break;
default: e.innerHTML = chosenCorrect;
}


There must be a better way of doing that, in one statement, with a
little redesign. Maybe call them S1 to S4, use getElementByID('S'+n),
and provide getElementByID for those who lack it.

feedback.innerHTML = "<font color='#ff0000'>Incorrect:<br>The correcttranslation of \"" + chosenTerm + "\" is \"" + chosenCorrect +

"\".</font>";
Don't let your software wrap lines when posting.

feedback.innerHTML = "Percentage correct: " + (total/count) + "%";
One wonders whether adding 100* might help.
Further repeated points are not re-commented on.
Read the newsgroup FAQ.

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

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Willem-Jan Selen | last post: by
10 posts views Thread by Sonoman | last post: by
6 posts views Thread by David Cook | last post: by
27 posts views Thread by eksamor | last post: by
10 posts views Thread by Praveen.Kumar.SP | last post: by
reply views Thread by rosydwin | last post: by

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.