...

View Full Version : Loops and Arrays situations



worldtraveller
11-17-2010, 11:59 PM
How does my array look so far?

thanks

I think so
what i was supposed to do was
insert a script element that contains the function
amountTotal()

then add commands to the function
such as
1 declare variable named total, set intial value to 0
2 create a FOR LOOP that loops all values in amount array.
add current value of array item to value of total variable
3 when completed return value of total variable

This is what I came up with
tell me if this makes sense

Thanks


title>The Lighthouse</title>
<link href="lhouse.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="list.js"></script>

<script type="text/javascript">

function amountTotal() {
totalAmount=0;
for(var i=0; i<amountTotal.length; i++){
total+=amount(i);

{
return total;
}
</script>
</head>

Does this seem right or is my format all wrong?
if so what should i have instead?
thanks

Old Pedant
11-18-2010, 12:55 AM
Nope. Four errors. At least.

(1) Array elements are always referenced via square brackets: arrayName[ elementNumber ]

(2) You are using two different names for the array.

(3) You are using two different names for the total.

(4) The number of left ( and right ) must always be the same. Also [ and ]. Also { and }. One of those is wrong in your code.

Hasn't anybody suggested that you should start using FireBug debugger with Firefox, to find errors such as these????

glenngv
11-18-2010, 03:03 AM
You're mixing up the variable names.


function amountTotal() {
var total=0;
for(var i=0; i<amountTotal.length; i++){
total += amountTotal[i];
}
return total;
}
You should learn to check the error console to check for errors. This will save you time to figure out the problem.

Old Pedant
11-18-2010, 05:39 AM
Glenn: I thought that *I* had given him too much help, for a simple homework problem like this.

But isn't giving him the full answer going way too far?

Not that it matters a ton, because your answer is very wrong.

Hint: Since when would you have an array with the same name as a function?

Hint: Read part 2 of the instructions more carefully.

glenngv
11-18-2010, 07:25 AM
Didn't pay too much attention that I didn't notice the array name is the same as the function name.

Old Pedant, when I posted my response above, I haven't seen yours. I'm always using the Quick Reply whenever I post here and I must have typed my response when the page has loaded for a long period of time. But nevertheless, I must admit I shouldn't have given him the full answer.

Philip M
11-18-2010, 07:41 AM
Hasn't anybody suggested that you should start using FireBug debugger with Firefox, to find errors such as these????

Yes, on at least two occasions, but this guy does not take a lot of notice. ;)

It looks as though he regards this forum as his first port of call when he is set a homework assignment. :(

Old Pedant
11-18-2010, 07:16 PM
And throws code at the wall and hopes it sticks?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum