473,586 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

'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="Micros oft FrontPage 5.0">
<meta name="ProgId" content="FrontP age.Editor.Docu ment">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
<script language="JavaS cript1.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] = "prestissim o";
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(random s[r++]));
}

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

terms = terms.sort(sort B);

r = 0;

correct = correct.sort(so rtB);

question();
}

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

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

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

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

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

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

switch(position Answer)
{
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.innerH TML = "<font color='#00ff00' >Correct!</font>";
}
else
{
feedback.innerH TML = "<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.innerH TML = "Percentage correct: " + (total/count) + "%";
count = 0;
total = 0;
}

function getRandomCorrec t()
{
var input = Math.round(Math .random()*(corr ect.length - 1));

while(searchCor rect(input))
{
input = Math.round(Math .random()*(corr ect.length - 1));
}

usedCorrect.pus h(input);
return input;
}

function searchCorrect(a )
{
for(var i = 0; i < usedCorrect.len gth; 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 2004
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(random s[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(random s[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.protot ype -. And as - Function.protot ype -
has no - valueOf - property of its own the method called will be the one
from - Object.prototyp e -, 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.c om 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.ispd r.net.au>, dated Thu, 6
Jan 2005 09:22:57, seen in news:comp.lang. javascript, B. Fletcher
<br************ ****@hotmail.co m> 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="Micros oft FrontPage 5.0">
That seems a mistake.

<script language="JavaS cript1.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(random s[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.t oString()); toString() seems superfluous.

termCell.innerH TML = "<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[getRandomCorrec t()];
b.innerHTML = correct[getRandomCorrec t()];
c.innerHTML = correct[getRandomCorrec t()];
d.innerHTML = correct[getRandomCorrec t()];
e.innerHTML = correct[getRandomCorrec t()];
Sub-maximal browser compatibility there. FAQ 4.15.

switch(position Answer)
{
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.innerH TML = "<font color='#ff0000' >Incorrect:<br> The correct
translation of \"" + chosenTerm + "\" is \"" + chosenCorrect + "\".</font>";
Don't let your software wrap lines when posting.

feedback.innerH TML = "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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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.de mon.co.uk> wrote in message
news:oL******** ******@merlyn.d emon.co.uk...
JRS: In article <11************ ***@stitch.ispd r.net.au>, dated Thu, 6
Jan 2005 09:22:57, seen in news:comp.lang. javascript, B. Fletcher
<br************ ****@hotmail.co m> 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="Micros oft FrontPage 5.0">


That seems a mistake.

<script language="JavaS cript1.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(random s[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.t oString());

toString() seems superfluous.

termCell.innerH TML = "<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[getRandomCorrec t()];
b.innerHTML = correct[getRandomCorrec t()];
c.innerHTML = correct[getRandomCorrec t()];
d.innerHTML = correct[getRandomCorrec t()];
e.innerHTML = correct[getRandomCorrec t()];


Sub-maximal browser compatibility there. FAQ 4.15.

switch(position Answer)
{
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.innerH TML = "<font color='#ff0000' >Incorrect:<br> The correcttranslation of \"" + chosenTerm + "\" is \"" + chosenCorrect +

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

feedback.innerH TML = "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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources. <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items,

links.
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2085
by: Edvard Majakari | last post by:
Hi all ya unit-testing experts there :) Code I'm working on has to parse large and complex files and detect equally complex and large amount of errors before the contents of the file is fed to module interpreting it. First I created a unit-test class named TestLoad which loaded, say, 40 files of which about 10 are correct and other 30 files...
6
4219
by: Willem-Jan Selen | last post by:
Hie everybody, I have a problem with the following, I have a table with a row containing numbers with 2 decimal places. I want to count the sum of these numbers but my valuw returned is far to high. I think the SUM string doesn't look at the decimal place. The same is with the AVG string
10
2887
by: Sonoman | last post by:
Hi all: I am trying to write a simple program that simulates asking several persons their birth day and it counts how many persons are asked until two have the same birth day. The problem that I have is that the first loop I get a sequence of random numbers untuil I get a match, BUT then on the following loops I get the SAME random(?)...
6
6777
by: David Cook | last post by:
The html file below gets intermittent errors 'error on page' ('number expected') when clicking on column-headings to sort. Yet, this same file works flawlessly in other browsers (i.e. Opera, Mozilla, Netscape, etc.). Can anyone suggest a fix/workaround? Cheers...
11
21912
by: Ken Varn | last post by:
I want to be able to determine my current line, file, and function in my C# application. I know that C++ has the __LINE__, __FUNCTION__, and __FILE___ macros for getting this, but I cannot find a C# equivalent. Any ideas? -- ----------------------------------- Ken Varn Senior Software Engineer Diebold Inc. varnk@diebold.com
27
9779
by: eksamor | last post by:
I can't seem to control the rand() function. I just makes an evil humongeous number. How do I make it generate random numbers from 1 - 4000??
10
6715
by: Robert | last post by:
How do you get an accurate count of the number of records returned from a query when using linked tables. I have an access 2003 database as a front end to another access 2003 database that contains only tables. I have linked the tables for the front end to the back end database. I am trying to set the recordsource of a form to a query...
10
9829
by: Praveen.Kumar.SP | last post by:
Hi Could anyone solve the problem for the code below The Code: #include "stdio.h" #include "iostream.h" void Temp( int a, char* str,...)
19
33350
by: Dancefire | last post by:
Hi, everyone It might be a simple question, but I really don't know the answer. char c = '1'; cout << c; The above code will only output a '1' rather than 0x31; If I use int cast, it can show the number:
0
7839
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8202
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
6614
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3837
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.