'Number Expected' problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • B. Fletcher

    '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


    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>



  • Lee

    #2
    Re: 'Number Expected' problems

    B. Fletcher said:[color=blue]
    >
    >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.[/color]

    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.



    Also, this line:
    [color=blue]
    > return parseInt(String .valueOf(random s[r++]));[/color]

    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.

    Comment

    • Richard Cornford

      #3
      Re: 'Number Expected' problems

      Lee wrote:[color=blue]
      > B. Fletcher said:[/color]
      <snip>[color=blue]
      > Also, this line:
      >[color=green]
      >> return parseInt(String .valueOf(random s[r++]));[/color]
      >
      > 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.[/color]

      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.


      Comment

      • Dr John Stockton

        #4
        Re: 'Number Expected' problems

        JRS: In article <1104963646.669 601@stitch.ispd r.net.au>, dated Thu, 6
        Jan 2005 09:22:57, seen in news:comp.lang. javascript, B. Fletcher
        <brentonfletche r007@hotmail.co m> posted :
        [color=blue]
        >I'm having some trouble with javascript code of mine:
        >
        >When the script runs, I vget an error in line 119: Number Expected.[/color]

        So which is line 119?
        [color=blue]
        >I'm not sure as to why this is happening. Any advice would be helpful.[/color]

        Test smaller pieces at once.

        [color=blue]
        ><meta name="GENERATOR " content="Micros oft FrontPage 5.0">[/color]

        That seems a mistake.

        [color=blue]
        ><script language="JavaS cript1.5">[/color]

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

        [color=blue]
        >// more elements follow, removed because they are unimportant I think[/color]

        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.

        [color=blue]
        > return parseInt(String .valueOf(random s[r++]));[/color]

        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.

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

        I think that generates -1 (25%), 0 (50%), +1 (25%). Maybe you want
        that; otherwise, see FAQ 4.22.
        [color=blue]
        > alert(randoms.t oString());[/color]
        toString() seems superfluous.

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

        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.

        [color=blue]
        > positionAnswer = Math.round(Math .random()*4);[/color]

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

        [color=blue]
        > 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()];[/color]

        Sub-maximal browser compatibility there. FAQ 4.15.

        [color=blue]
        > 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;
        > }[/color]

        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.

        [color=blue]
        > feedback.innerH TML = "<font color='#ff0000' >Incorrect:<br> The correct
        >translation of \"" + chosenTerm + "\" is \"" + chosenCorrect + "\".</font>";[/color]

        Don't let your software wrap lines when posting.

        [color=blue]
        > feedback.innerH TML = "Percentage correct: " + (total/count) + "%";[/color]

        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.

        Comment

        • B. Fletcher

          #5
          Re: 'Number Expected' problems

          Hello,

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

          B. Fletcher

          "Dr John Stockton" <spam@merlyn.de mon.co.uk> wrote in message
          news:oLJGOgfSgV 3BFwTP@merlyn.d emon.co.uk...[color=blue]
          > JRS: In article <1104963646.669 601@stitch.ispd r.net.au>, dated Thu, 6
          > Jan 2005 09:22:57, seen in news:comp.lang. javascript, B. Fletcher
          > <brentonfletche r007@hotmail.co m> posted :
          >[color=green]
          > >I'm having some trouble with javascript code of mine:
          > >
          > >When the script runs, I vget an error in line 119: Number Expected.[/color]
          >
          > So which is line 119?
          >[color=green]
          > >I'm not sure as to why this is happening. Any advice would be helpful.[/color]
          >
          > Test smaller pieces at once.
          >
          >[color=green]
          > ><meta name="GENERATOR " content="Micros oft FrontPage 5.0">[/color]
          >
          > That seems a mistake.
          >
          >[color=green]
          > ><script language="JavaS cript1.5">[/color]
          >
          > Deprecated; see FAQ or Notes, and complain if it is not there :
          > use <script type="text/javascript">
          >
          >[color=green]
          > >// more elements follow, removed because they are unimportant I think[/color]
          >
          > 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.
          >
          >[color=green]
          > > return parseInt(String .valueOf(random s[r++]));[/color]
          >
          > 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.
          >
          >[color=green]
          > > randoms[z] = Math.round(Math .random()*2) - 1;[/color]
          >
          > I think that generates -1 (25%), 0 (50%), +1 (25%). Maybe you want
          > that; otherwise, see FAQ 4.22.
          >[color=green]
          > > alert(randoms.t oString());[/color]
          > toString() seems superfluous.
          >
          >[color=green]
          > > termCell.innerH TML = "<font size='5'>" + chosenTerm + "</font>";[/color]
          >
          > 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.
          >
          >[color=green]
          > > positionAnswer = Math.round(Math .random()*4);[/color]
          >
          > Gives 0 (12.5%), 1 2 3 (25%), 4 (12.5%). As before.
          >
          >[color=green]
          > > 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()];[/color]
          >
          > Sub-maximal browser compatibility there. FAQ 4.15.
          >
          >[color=green]
          > > 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;
          > > }[/color]
          >
          > 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.
          >
          >[color=green]
          > > feedback.innerH TML = "<font color='#ff0000' >Incorrect:<br> The[/color][/color]
          correct[color=blue][color=green]
          > >translation of \"" + chosenTerm + "\" is \"" + chosenCorrect +[/color][/color]
          "\".</font>";[color=blue]
          >
          > Don't let your software wrap lines when posting.
          >
          >[color=green]
          > > feedback.innerH TML = "Percentage correct: " + (total/count) + "%";[/color]
          >
          > 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[/color]
          4 ©[color=blue]
          > <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of[/color]
          news:comp.lang. javascript[color=blue]
          > <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates,[/color]
          sources.[color=blue]
          > <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items,[/color]
          links.


          Comment

          Working...