weird for loop behaviour

M

Mark Scott

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
 
V

VK

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.
 
T

Thomas 'PointedEars' Lahn

Mark said:
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
 
S

SAM

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
 
M

Mark Scott

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top