Jump to content

Script Help With For Loops


pauldboughter
 Share

Recommended Posts

I am trying to use a For Loop to loop through all the values in the amount array. While at each iteration of the loop add current value of array item to value of total variable. After loop is completed I I returned the value of the total variable.This is the code I got can someone take a look at it and hopefully explain where I went wrong. Thanks<script type="text/javascript"> function amountTotal() { var total = 0; for (var i = 0; < amount.length; i++) { commands involving array; total += amount; } return total; } </script>

Link to comment
Share on other sites

Hi there Big Papa,

commands involving array[i];

Did you literally write this line of code in your script? Did you copy it from of a book or lecture notes?I'm a novice Javascriptor and at risk of embarrassing myself, but I don't think that it's actually Javascript. It appeared in a book called New Perspectives on JavaScript, just as a badly marked placeholder, telling you to manipulate your array in this space.Consider
document.write('Hello World.');

Javascript certainly isn't going to write 'Hello World.' if you just say commands about writing to document;.Perhaps this is a place to start.Regards,No Nails

Edited by No Nails
Link to comment
Share on other sites

Well actually yes this is from the book New Perspectives but the book really doesn't explain in enough detail to grasp it. So I am trying to see if anyone can explain it better to me.

Hi there Big Papa,Did you literally write this line of code in your script? Did you copy it out of a book or lecture notes?I'm a novice Javascriptor and at risk of embarrassing myself, but I don't think that it's actually Javascript. It appeared in a book called New Perspectives on JavaScript, just as a badly marked placeholder, telling you to manipulate your array in this space. It's a bit like saying commands about writing to document;, as opposed to
document.write('Hello World.');

Javascript certainly isn't going to print 'Hello World'.Perhaps this is a place to start.Regards,No Nails

Link to comment
Share on other sites

Okay... well I'll try annotating it as best I know how...

function amountTotal() {	var total = 0;		// Sets the value of a new variable, 'total', at 1. Remember how 0 = 1?	for (var i = 0; i < amount.length; i++) {			// "var i = 0" sets the value of a new variable, 'i', at 1			// "i < amount.length" is the continuation condition for the loop -			// so the script inside the 'for loop' will only stop looping when the value			// of 'i' is no longer less than the number of items in your array named 'amount'.			// "i++" tells the loop to "add 1 to the value of 'i'" every time it cycles through.			// Taken all together it means, "For as long as the value of 'i' is less than the			// number of items in your array named 'amount', keep adding 1 to 'i' and			// executing the script inside the curly brackets."			// Also, notice I filled in an 'i' that was missing from your version of this line.		[commands involving your amount[i]];			// Perspectives book: "now tell your loop what to do with your array named 'amount'."		total += amount[i];			// I'm not sure what making variable (with a numerical value) and an array "equal"			// would do. What did you mean by this? What are you trying to write?	}	return total;		// After all the looping, return the final value of your variable, 'total'.}

I'm not certain because what goes inside the loop might count as a "condensed array", but you may need to declare 'amount' as an array, and define its value/s.It may help to read up a little on them. Try W3Schools Array Object for a fast and intuitive explanation.

Edited by No Nails
Link to comment
Share on other sites

Can I email you the files I got with a description of what I need to do. I want to say thanks for the help on that part. I see I missed a few things.

Okay... well I'll try annotating it as best I know how...
function amountTotal() {	var total = 0;		// Sets the value of a new variable, 'total', at 1. Remember how 0 = 1?	for (var i = 0; i < amount.length; i++) {			// "var i = 0" sets the value of a new variable, 'i', at 1			// "i < amount.length" is the continuation condition for the loop -			// so the script inside the 'for loop' will only stop looping when the value			// of 'i' is no longer less than the number of items in the array named 'amount'.			// "i++" tells the loop to "add 1 to the value of 'i'" every time it cycles through.			// Taken all together it means, "For as long as the value of 'i' is less than the			// number of items in an array named 'amount', keep adding 1 to 'i' and			// executing the script inside the curly brackets."			// Also, notice I filled in an 'i' that was missing from your version of this line.		[commands involving your amount[i]];			// Perspectives book: "now tell your loop what to do with your array named 'amount'."		total += amount[i];			// I'm not sure what making variable (with a numerical value) and an array "equal"			// would do. What did you mean by this? What are you trying to write?	}	return total;		// After all the looping, return the final value of your variable, 'total'.}

It may help to read up a little on arrays. There are plenty of good Javascript explanations & examples on the Internet, and the easiest to get the hang of often seem to be on W3Schools.

Link to comment
Share on other sites

I'm delighted to help, but I would prefer if we worked through it together in the forum thread we have already started. This is for three reasons:1) Keeping it in the public domain means we are likely to be helped out by others if we miss something, or make a mistake, or get stuck,2) Keeping it *documented* in the public domain means, for what little it may be worth, future Javascript-learners could benefit from our discussion, and3) It would probably be more useful if I didn't do your assignment for you. Even if it's not literally a school assignment, take that as an analogy for whatever project you've undertaken. If you're working to a deadline, then say so & we can probably figure something out.So, I would start by explaining in the thread what exactly it is you are trying to achieve. So far your function doesn't seem to do anything... I am guessing this is not what you're going for.

Link to comment
Share on other sites

This is the work that needs to be done.... I only want to know what I messed up on and hopefully an explination of why it is... 2. Insert a script element to link list.js.3. below the script element you just created, insert another script element that contains the function amountTotal(). The purpose of the amountTotal() function is to return the sum of all the values in the amount array. There are no parameters for this function. Add the following commands to the function:a. Declare a variable named total, setting its initial value to 0.b. Create a For loop that loops through all of the values in the amount array. At each iteration of the loop, add the current value of the array item to the value of the total variable.c. After the For loop is completed, return the value of the total variable.4. Scroll down the document and locate the div element with the ID data_list. Within the div element, add a script element that contains the following commands:a. Write the following code to the document to create the header row for the table or contributions: <table borer=’1’ rules=’rows’ cellspacing=’0’> <tr> <th>Date</th><th>Amount</th><th>First Name</th> <th>Last Name</th><th>Address</th> </tr>b. Create a For loop in which the counter variable starts at 0 and, while the counter is less than the length of the amount array, increase the counter in increments of 1.c. Display every other row in the data list with a yellow background. To do this, within the For loop, insert and If condition that tests whether the counter variable is divisible evenly by 2 (Hint use the % modulus operator). If the counter variable is divisible by 2, write the following HTML tag:<tr>Otherwise, write the following tag:<tr class=’yellowrow’>d. Next, within the For loop, write the HTML code<td>date</td><td class=’amt’>amount</td><td>firstName</td><td>lastName</td>to the document, where date, amount, firstName, and lastName are the values of the date, amount, firstName, and lastName arrays for the index indicated by the current value of the For loop’s counter variable.e. Finally, within the For loop, write the HTML code<td>street<br /> city, state zip</td> </tr>to the document, where street, city, state, and zip are the values for the street, city, state, and zip arrays for the current index value.5. Go to the div element with the ID totals. Insert a script element that writes the HTML code<table border=’1’ cellspacing=’1’> <tr> <th id=’sumTitle’ colspan=’2’> Summary </th> </tr> <tr> <th>Contributors</th> <td>contributors</td> </tr> <tr> <th>Amount</th> <td>$total</td> </tr></table>to the document, where contributions is the length of the amount array and total is the value returned by the amountTotal() function you created earlier.This is the coded document that I have so far.<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"> <title>The Lighthouse</title> <link href="lhouse.css" rel="stylesheet" type="text/css" /> <script src="list.js" type="text/javascript"></script> <script type="text/javascript"> function amountTotal() { var total = 0; for (var i = 0; i < amount.length; i++) { [commands involving amount]; total += amount; } return total; } </script> </head><body> <div id="title"> <img src="logo.jpg" alt="The Lighthouse" /> The Lighthouse<br /> 543 Oak Street<br /> Owensboro, KY   42302<br/> (270) 555-7511 </div> <div id="data_list"> <script type="text/javascript"> document.write("<table border='1' rules='rows' cellspacing='0'>"); document.write("<tr>"); document.write("<th>Date</th><th>Amount</th><th>First Name</th>"); document.write("<th>Last Name</th><th>Address</th>"); document.write("</tr>"); for (i = 0; i < amount.length; i++) { if (i % 2 == 0) { document.write("<tr>"); else document.write("<tr class='yellowrow'>"); } } document.write("<td>" + date + "</td>"); document.write("<td class='amt'>" + amount + "</td>"); document.write("<td>" + firstName + "</td>"); document.write("<td>" + lastName + "</td>"); document.write("<td>street<br />" + city + "," + state + "," + zip + "</td>"); document.write("</tr>"); </script> </div> <div id="totals"> <script type="text/javascript"> document.write("<table border='1' cellspacing='1'>"); document.write("<tr>"); document.write("<th id='sunTitle' colspan='2'>"); document.write("Summary"); document.write("</th>"); document.write("</tr>"); document.write("<tr>"); document.write("<th>Contributors</th>"); document.write("<td>amount.length</td>"); document.write("</tr>"); document.write("<tr>"); document.write("<th>Amount</th>"); document.write("<td>" + total + "</td>"); document.write("</tr>"); document.write("</table>"); </script> </div></body></html>

Link to comment
Share on other sites

3...b. Create a For loop that loops through all of the values in the amount array. At each iteration of the loop, add the current value of the array item to the value of the total variable.
This is the only bit you are obviously having difficulty with. At a glance, everything else looks sweet. I suppose you can tell for yourself whether all the values & tables are correctly displayed, and if every other row is yellow.So, first refresh your memory on array syntax and loop syntax. Again, I would suggest you refer to this article, and also this one.After that, if you're still unclear on why the question describes "loop[ing] through all of the values in the amount array," I recommend you investigate the mechanics of how loops and arrays actually "work together".That's the "go out and learn something" conscience purge out of the way.Now - the answer - as I'm sure you understand, the point of amountTotal() is to add up all the values in the array named 'amount'. This array is presumably defined in the external .js file they gave you, so you don't need to worry about declaring/defining.The good news is that you've basically done everything right. Obviously you can get rid of the line,
commands about amount[i];

Let me know if it doesn't do what it's supposed to after that.

Edited by No Nails
Link to comment
Share on other sites

I have been looking and still am alittle puzzled on the For Loop.These is the first loop I am working with. <script type="text/javascript"> function amountTotal() { var total = 0; for (var i = 0; i < amount.length; i++) { total += amount; } return total; } </script> I set this up to add the amount.length to itself to return the total var to use in another script shown below. This isn't working for some reason. <script type="text/javascript"> document.write("<table border='1' rules='rows' cellspacing='0'>"); document.write("<tr>"); document.write("<th>Date</th><th>Amount</th><th>First Name</th>"); document.write("<th>Last Name</th><th>Address</th>"); document.write("</tr>"); for (i = 0; i < amount.length; i++) { if (i % 2 == 0) { document.write("<tr>"); else document.write("<tr class='yellowrow'>"); } } document.write("<td>" + date + "</td>"); document.write("<td class='amt'>" + amount + "</td>"); document.write("<td>" + firstName + "</td>"); document.write("<td>" + lastName + "</td>"); document.write("<td>street<br />" + city + "," + state + "," + zip + "</td>"); document.write("</tr>"); </script>This is suppose to display the table and values with an associate .js file. If I remove the for loop the Table headers show up but when the for loop is added nothing is displayed. Which tells me that the issue is the For Loop. I cannot seem to get this right. I have looked online and did some research but everything that comes back says this should work. Could someone explain what I am doing wrong alittle better. Thanks

This is the only bit you are obviously having difficulty with. At a glance, everything else looks sweet. I suppose you can tell for yourself whether all the values & tables are correctly displayed, and if every other row is yellow.So, first refresh your memory on array syntax and loop syntax. Again, I would suggest you refer to this article, and also this one.After that, if you're still unclear on why the question describes "loop[ing] through all of the values in the amount array," I recommend you investigate the mechanics of how loops and arrays actually "work together".That's the "go out and learn something" conscience purge out of the way.Now - the answer - as I'm sure you understand, the point of amountTotal() is to add up all the values in the array named 'amount'. This array is presumably defined in the external .js file they gave you, so you don't need to worry about declaring/defining.The good news is that you've basically done everything right. Obviously you can get rid of the line,
commands about amount[i];

Let me know if it doesn't do what it's supposed to after that.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...