weird for loop behaviour

Discussion in 'Javascript' started by Mark Scott, Jan 13, 2008.

  1. Mark Scott

    Mark Scott Guest

    I have successfully written a function that writes out a times table as
    follows:

    var timesTable
    parseFloat(timesTable)
    var upperBound
    parseFloat(upperBound)
    function tableWriter()
    {
    var n
    parseFloat(n)
    for (n = 1; n < (upperBound+1)/10; n = n + 1)
    {
    document.write (n + " times "+ timesTable + " equals " + n * timesTable
    +"<BR>")
    }
    }
    timesTable = window.prompt ("What times table do you wish to see?")
    upperBound = window.prompt ("how many entries do you need?")
    tableWriter(timesTable);

    However looking at the follwoing line:

    for (n = 1; n < (upperBound+1)/10; n = n + 1)

    if I had n < upperBound then I get 11 entries when I ask for 12 (fair
    enough, zero based indexing)
    If I had n < upperBound+1 then I get 120 entries (12 * 10? where is the *10
    coming from?!)
    I have had to bodge it using the code I have in the line now.

    Any ideas why this is happening?

    Regards

    Mark
     
    Mark Scott, Jan 13, 2008
    #1
    1. Advertising

  2. Mark Scott

    VK Guest

    On Jan 13, 7:31 pm, "Mark Scott" <> wrote:
    > I have successfully written a function that writes out a times table as
    > follows:
    >
    > var timesTable
    > parseFloat(timesTable)
    > var upperBound
    > parseFloat(upperBound)
    > function tableWriter()
    > {
    > var n
    > parseFloat(n)
    > for (n = 1; n < (upperBound+1)/10; n = n + 1)
    > {
    > document.write (n + " times "+ timesTable + " equals " + n * timesTable
    > +"<BR>")}
    > }
    >
    > timesTable = window.prompt ("What times table do you wish to see?")
    > upperBound = window.prompt ("how many entries do you need?")
    > tableWriter(timesTable);
    >
    > However looking at the follwoing line:
    >
    > for (n = 1; n < (upperBound+1)/10; n = n + 1)
    >
    > if I had n < upperBound then I get 11 entries when I ask for 12 (fair
    > enough, zero based indexing)
    > If I had n < upperBound+1 then I get 120 entries (12 * 10? where is the *10
    > coming from?!)
    > I have had to bodge it using the code I have in the line now.
    >
    > Any ideas why this is happening?


    Sorry, can you give some sample timesTable, upperBound and expected
    output for them? I'm trying hard but missing so far what is your aim
    in this program.
     
    VK, Jan 13, 2008
    #2
    1. Advertising

  3. Mark Scott wrote:
    > I have successfully written a function that writes out a times table as
    > follows:
    >
    > var timesTable
    > parseFloat(timesTable)
    > var upperBound
    > parseFloat(upperBound)
    > function tableWriter()
    > {
    > var n
    > parseFloat(n)
    > for (n = 1; n < (upperBound+1)/10; n = n + 1)
    > {
    > document.write (n + " times "+ timesTable + " equals " + n * timesTable
    > +"<BR>")
    > }
    > }
    > timesTable = window.prompt ("What times table do you wish to see?")
    > upperBound = window.prompt ("how many entries do you need?")
    > tableWriter(timesTable);
    >
    > However looking at the follwoing line:
    >
    > for (n = 1; n < (upperBound+1)/10; n = n + 1)


    Why do you post this line when you are using different code anyway?

    > if I had n < upperBound then I get 11 entries when I ask for 12 (fair
    > enough, zero based indexing)


    The reason for that is instead that you have started counting at 1 but you
    are excluding the cases where n > 11 (the integer opposite set of n < 12),
    which includes the loop that would be executed after n is increased to 12.
    What you call "zero-based indexing" is merely the program logic that is
    required when using the `<' instead of the `<=' operator as the condition of
    a loop with incremented index.

    > If I had n < upperBound+1 then I get 120 entries (12 * 10? where is the *10
    > coming from?!)


    The `+' operator converts its operands to String if one operand is of type
    String; the result of that is "12" + "1" which as a string concatenation
    evaluates to "121" (ES3 Final, 11.6.1). And in a `<' operation, both
    operands are converted to Number if one operand is of type Number (ES3
    Final, 11.8.5). The result is an iteration from n=1 to n=120 (n < 121).

    > I have had to bodge it using the code I have in the line now.
    >
    > Any ideas why this is happening?


    Your code as posted does _not_ convert the value typed by the user to Number.

    var timesTable
    parseFloat(timesTable)
    var upperBound
    parseFloat(upperBound)

    does not do anything besides declaring the two local variables `timesTable'
    and `upperBound' and initialize them with the value `undefined'.
    parseFloat() is not a mutator method, and strings are immutable anyway. The
    same goes for your

    var n
    parseFloat(n)

    Probably you were looking for something along

    var timesTable = window.prompt("What times table do you wish to see?")
    var upperBound = window.prompt("how many entries do you need?")

    // add additional tests for the input value here

    timesTable = parseFloat(timesTable);
    upperBound = parseFloat(upperBound);


    PointedEars
    --
    var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
    ) // Plone, register_function.js:16
     
    Thomas 'PointedEars' Lahn, Jan 13, 2008
    #3
  4. Mark Scott

    SAM Guest

    Mark Scott a écrit :
    >
    > for (n = 1; n < (upperBound+1)/10; n = n + 1)
    >
    > If I had n < upperBound+1 then I get 120 entries (12 * 10? where is the
    > *10 coming from?!)


    try :

    n < +upperbound+1;

    > I have had to bodge it using the code I have in the line now.


    certainly upperbound is seen as a string (not a number)

    to convert in a number :
    upperbound = 1*upperbound;
    upperbound = Number(upperbound);

    ou directly :

    n < upperbound*1 +1;
    n < Number(upperbound) + 1;
    n < +upperbound + 1;

    the sign + force the variable to be a number

    --
    sm
     
    SAM, Jan 13, 2008
    #4
  5. Mark Scott

    Mark Scott Guest

    Its just a simple program to test a function. for example

    if the line was set at: < upperBound + 1 and I gave it the entries of say 10
    and 12 then I would expect the 10 times table up to 10 x 12 is 120 but it
    goes up to 10 x 120 is 1200


    "VK" <> wrote in message
    news:...
    > On Jan 13, 7:31 pm, "Mark Scott" <> wrote:
    >> I have successfully written a function that writes out a times table as
    >> follows:
    >>
    >> var timesTable
    >> parseFloat(timesTable)
    >> var upperBound
    >> parseFloat(upperBound)
    >> function tableWriter()
    >> {
    >> var n
    >> parseFloat(n)
    >> for (n = 1; n < (upperBound+1)/10; n = n + 1)
    >> {
    >> document.write (n + " times "+ timesTable + " equals " + n * timesTable
    >> +"<BR>")}
    >> }
    >>
    >> timesTable = window.prompt ("What times table do you wish to see?")
    >> upperBound = window.prompt ("how many entries do you need?")
    >> tableWriter(timesTable);
    >>
    >> However looking at the follwoing line:
    >>
    >> for (n = 1; n < (upperBound+1)/10; n = n + 1)
    >>
    >> if I had n < upperBound then I get 11 entries when I ask for 12 (fair
    >> enough, zero based indexing)
    >> If I had n < upperBound+1 then I get 120 entries (12 * 10? where is the
    >> *10
    >> coming from?!)
    >> I have had to bodge it using the code I have in the line now.
    >>
    >> Any ideas why this is happening?

    >
    > Sorry, can you give some sample timesTable, upperBound and expected
    > output for them? I'm trying hard but missing so far what is your aim
    > in this program.
     
    Mark Scott, Jan 14, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dorayme
    Replies:
    1
    Views:
    623
    richard
    Jan 21, 2011
  2. richard
    Replies:
    0
    Views:
    585
    richard
    Jan 21, 2011
  3. richard
    Replies:
    0
    Views:
    618
    richard
    Jan 21, 2011
  4. Yigit Turgut

    Weird Loop Behaviour

    Yigit Turgut, Jan 20, 2012, in forum: Python
    Replies:
    3
    Views:
    169
    Arnaud Delobelle
    Jan 20, 2012
  5. Isaac Won
    Replies:
    9
    Views:
    382
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page