Jump to content

Trying to Understand


zeroCool

Recommended Posts

Hello:I am currently a student in at Wastenaw Community College. One of the classes I am taking teaches JavaScript. But I am not learning so well, the only points I have gotten in the class so far were from the HTML/CSS and tests. I really don't understand at all, and unless I pick my grades up, I am going to fail. So I decided to review all the old assignments in an attempt to try and understand. But I am still having trouble so I thought I should get some help, it would be appreciated.Right now I'm on for-loops, in the first assignment. note: We were still using DOM0 methods this early on in the class.My Code:var listItems = ["Automotive","Energy","Entertainment","Health Care","Education","Government"];function list() { document.write("<ul>"); for (var items = listItems[0]; itmes < listItmes[5]; itmes++) { document.write("<li>" + items + "</li>") } document.write("</ul>")}Code form the Solution:var industries = ["Automotive","Energy","Entertainment","Health Care","Education","Government"];function list() { document.write('<ul>'); for (var i=0, allIndustries = industries.length; i<allIndustries; i++) { document.write('<li>' + industries + '</li>'); } document.write('</ul>');}For this I basically need someone to explain to me why mine is wrong and how the for-loop from the solution works.

Link to comment
Share on other sites

The problem with your code is this:for(var items = listItems[0]; itmes < listItmes[5]; itmes++)The for command is an iterator: it only needs a number, not any other kind of data:.The program interprets this:

items is "Automotive"as long as items is less than "Government" perform this task and add 1 to items
The real solution does this:
i is 0as long as i is less than 6 (the length of your array) perform this task and add 1 to i
Link to comment
Share on other sites

The length property of an array object gives the amount of elements that the array contains:var G = ["Hello","testing","element"];document.write(G.length)//This will return 3var G = ["Hello","testing","element","something","something else"];document.write(G.length)//This will return 5To go through the elements of an array you need to do this:for (x=0;x<myArray.length;x++) {document.write(myArray[x])}

Link to comment
Share on other sites

Have I missed something?var listItems = ["Automotive","Energy","Entertainment","Health Care","Education","Government"];function list() { document.write("<ul>"); for (var i=0, all = listItems.length; i<listItmes; i++) { document.write("<li>" + listitems + "</li>") } document.write("</ul>")}The 'i' is just the letter he chose I could replace it with any other letter or word, right?

Link to comment
Share on other sites

No, that's not right.Let's try it in english:I have an array with 6 elements:0. Automotive1. Energy2. Entertainment3. Health care4. Education5. GovernmentNow, just for reference: i++ is the same as i = i + 1Let's start counting:i = 0. I can use i, or any other name, it's just to count.write "<ul>"As long as i is less than the amount of items in the list (i < listItems.length), do the following and keep counting (i++):write: "<li>" put item number (i) here (Example: if i is 2, write "Entertainment") "</li>"write "</ul>"

Link to comment
Share on other sites

You must put it into the for statement.A FOR statement has three parts:Initialization; condition; continuationYou initialize a variable, tell the condition in which to break the loop, and then tell it what to do on each iteration.

Link to comment
Share on other sites

Do I have to say i=0 in the for-loop or can I place that as 'var i = 0' before the function?
You can put the i=0 outside of the for loop, but it's not the typical way that programmers write for loops. Someone else reading your code - or you, coming back to read it later - may have a hard time understanding what you are trying to do.But, that being said, the following for loops would all do the same thing:
for(var i = 0; i < 10; i++){	document.write(i + "<br />");}

var i = 0;for(; i < 10; i++){	document.write(i + "<br />");}

var i = 0;for(; i < 10; ){	document.write(i + "<br />");	i++;}

Link to comment
Share on other sites

Okay so I was fairly busy up until now. But I have around 8 hours tomorrow and 6 hours the next day to work on the current assignment. This is what I have currently:CurrentFirst I have to add the Java generated tool-tips.making it look like this:timesheet-tooltip screenshot (provided by the instructor.) So I was wondering, does anyone know good method or how could I edit the javascript to function properly?

Link to comment
Share on other sites

What would happen if you just add "title" attributes to the cells manually? Isn't that allowed? I mean, if it was up to me, I was going to generate the titles with PHP, or if S3Ls were not an option - XSLT... are you forced to use JavaScript for the sake of the class?If the later, then I think you can do it with a nested loop... something like:

var tableContents = document.getElementsByTagName('tbody')[0];for(var i=0;i<tableContents.rows.length;i++) {var projectOrTask = tableContents.rows[i].cells[0].nodeValue;for (var e=1;e<tableContents.rows[i].cells.length - 2;e++) {tableContents.rows[i].cells[e].title = projectOrTask+' hours for '+document.getElementsByTagName('thead')[0].rows[0].cells[e].innerHTML;}}

(place it after the table markup, or attach it as a funciton that would activate after at least the table has been loaded)The above is of course not guaranteed to work and is certainly not very efficient. If you're also greaded on performance, you should rework this bit. Think of it as an extra credit work :) .BTW, for the above to work properly (and also for cleanlyness' sake) you might consider moving the last row as a tfoot instead.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...