...

View Full Version : For Loops and Javascript



worldtraveller
11-20-2010, 08:32 PM
This is a final loops in an exercise

here is my code



<script type="text/javascript">

var counter;
for (counter=0; counter<amount.length; counter++) {}
{
document.writeIn(counter+"<br/>");

document.writeIn("<tr><td>"date"[counter]"</td>");
document.writeIn("<td class = 'amt'>myArray</td>");
document.writeIn("<td>"firstName[counter]"</td>");
document.writeIn("<td>"lastName[counter]"</td>");
document.writeIn("<td>"street[counter]"<br />");
document.writeIn("<td>"+street[counter]+" "+city[counter]+" "state[counter]+" "+zip[counter]+"</td></tr>);
Total=total+amount[counter];
document.writeIn("<table>");
}


</script>


This is what i have, what do i need to do to make this more proper and written properly?
thanks

DrDOS
11-20-2010, 08:42 PM
It's confusing if you're using sans-serif font, but it's document write line, so it's written as document.writeln, with a lower case ell.

Old Pedant
11-20-2010, 09:54 PM
Tons and tons of mistakes in such a short amount of code.

WHY DO YOU REFUSE TO USE FIREBUG DEBUGGER???????????????

How come you figure out to put in all the + string concatenations in *THIS* line:


document.writeIn("<td>"+street[counter]+" "+city[counter]+" "state[counter]+" "+zip[counter]+"</td></tr>);

But then don't put them into ANY of the other lines????

Even that line is missing a " mark. I will let you figure out where.

Or you could use FIREBUG and have it tell you.

worldtraveller
11-20-2010, 10:05 PM
I can not get firebug to work. I run the code in firefox but it shows nothing to be corrected


Is it possible u can show me me how exactly to write the above code correctly then?

thanks

worldtraveller
11-20-2010, 10:31 PM
What is the proper way to write out this loop for javascript?

I am seriously stumped on this one and need help on this now!! please send me a correct way of doing this exactly, how it should look. thanks

.<script type="text/javascript">

var counter;
for (counter=0; counter<amount.length; counter++)
{
document.writeIn(counter+"<br/>");

document.writeIn("<tr><td>"+date[counter]"</td>");
document.writeIn("<td class = 'amt'>myArray</td>");
document.writeIn("<td>"firstName[counter]"</td>");
document.writeIn("<td>"lastName[counter]"</td>");
document.writeIn("<td>"street[counter]"<br />");
document.writeIn("<td>"+street[counter]+" "+city[counter]+" "state[counter]+" "+zip[counter]+"</td>);
Total=total+amount[counter];
document.writeIn("<table>");
document.writeIn("</tr>");
}


</script>

Old Pedant
11-20-2010, 10:58 PM
Then you need to spend some more time reading the instructions on how to install and use Firebug.

Look, we all know this is homework, and READ THE RULES of this forum:
www.codingforums.com/rules.htm

Read rule 1.5.

NO HOMEWORK.

You make a serious effort to write your code and debug it, and if you still have a problem we will help.

But you haven't yet made a serious effort here, I feel.

For example, you didn't even try to fix the bug I pointed out to you in that one line, where you are missing a " mark.

And you are inconsistent. Just look at these two lines:


document.writeIn(counter+"<br/>");
document.writeIn("<tr><td>"+date[counter]"</td>");

IN the first line, you correctly put the + between the non-string and the string.
IN the second line, you correctly put the + between the string and the non-string.
BUT WHERE, in the second line, is the + between the non-string and the following string????

Oh, w.t.h. I'll show you this one:


document.writeIn("<tr><td>" + date[counter] + "</td>");


Now look at all your OTHER lines.

You *MUST* be very very careful in programming. It's not like writing text, where if you miss a punctuation mark or spelling here and there people can still read your writing and get the idea. You must be LETTER PERFECT. (And punctuation perfect.) No sloppiness allowed.

Enough. Really and truly, you need to take the time to figure out how to use Firebug. It might take you a couple of hours now, but you will save many many times that in the upcoming weeks.

worldtraveller
11-20-2010, 11:15 PM
Sorry for all of this i have made an effort to correct but not exactly on here. I have made corrections but entire program is still not working.
does anyone know of anyone who can full help on this issue? or of anyone who is willing to give javascript tips and hints.

Dormilich
11-21-2010, 03:27 AM
I really need to stress out that the command is document.writeln(), not document.writeIn() (as DrDOS already mentioned)

Old Pedant
11-21-2010, 07:19 AM
I have made corrections but entire program is still not working.

Then show the code WITH the corrections.

And, if it is getting an error in FireBug that you don't understand, tell us what the error is.

The last set of code you posted, you had not made *ANY* of the changes we had suggested so far.

worldtraveller
11-21-2010, 05:29 PM
I will post on this forum ,my corrected code, but still not working, thanks much though

worldtraveller
11-21-2010, 06:31 PM
ok i been working on my loops etc i icame up with this in a lesson



var firstName = Array("Mike", "Fred", "Sue");
var lastName = Array("Smith", "Jones", "Walker");

function buildTable(){
var table1 = document.getElementById("D1"); //get the HTML id of the table
var len = firstName.length; // this assumes all the arrays have the same number of elements
for(var i = 0; i < len; i++){
var tr = document.createElement('tr'); //create a table row
table1.appendChild(tr); //stick it in the table
//the code that follows really needs to be a separate function where you pass it the text and the reference to the table row
var td = document.createElement('td'); //create a table cell
tr.appendChild(td); //stick it in the table row
var txt = document.createTextNode(firstName[i]); //create a text node
td.appendChild(txt); //stick it in the table cell
//likewise--this is essentially duplicate code
var td = document.createElement('td'); //create another table cell for the lastName field
tr.appendChild(td); //stick it in the table row
var txt = document.createTextNode(lastName[i]); //create a text node mirrowing the lastName value
td.appendChild(txt); //stick it in the table cell
}
}


window.onload= function(){buildTable();}
</script>
</head>
<body>
<table id ="D1">
</table>

</body>
</html>


and im trying to implement this into my program, but notworking so far

worldtraveller
11-21-2010, 06:33 PM
Now I am trying to put it into my program.
do i still need to use

document.write for my loops. this is what i originally had



<script type="text/javascript">

var counter;
for (counter=0; counter<amount.length; counter++)
{
document.writeln("<table>");
document.writeln(counter+"<tr><br/>");

document.writeln("<td>"+date[newArray]+"</td>");
document.writeln("<td class = 'amt'>counter</td>");
document.writeln("<td>"firstName[counter]"</td>");
document.writeln("<td>"lastName[counter]"</td>");
document.writeln("<td>"street[counter]"</td><br />");
document.writeln("<td>"+street[counter]+" "+city[counter]+" "state[counter]+" "+zip[counter]+"</td>);
Total=total+amount[counter];
document.writeln("</table>");
document.writeln("</tr>");
}


</script>


this is what i changed it to
but not working

worldtraveller
11-21-2010, 06:34 PM
Now i am reworking the entire code, but still not working

i am trying to follow the practice loop i created

this how i looks in my entire program so far, but not working

hmm perhaps i have to follow another format

anyways here is the code so far
look at last bottom part
for loop

worldtraveller
11-21-2010, 06:35 PM
Now i am reworking the entire code, but still not working

i am trying to follow the practice loop i created

this how i looks in my entire program so far, but not working

hmm perhaps i have to follow another format

anyways here is the code so far
look at last bottom part
for loop


<html>
<head>
<!--
New Perspectives on JavaScript
Tutorial 3
Case Problem 1

The Lighthouse
Author: John R
Date: 19 november 2010

Filename: clist.htm
Supporting files: lhouse.css, list.js, logo.jpg

-->
<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() {

var total = 0;

for (total=1; total <=10; i++)
}
Total=total+i;
{
document.write("the sum of 1,2,3,4,5,6,7,8,9,10 is "+total);
document.write("<td>"+"total"+"</td>");
}
</script>
</head>

<body>

<div id="title">
<img src="logo.jpg" alt="The Lighthouse" />
The Lighthouse<br />
543 Oak Street<br />
Delphi, KY &nbsp;&nbsp;89011<br/>
(542) 555-7511
</div>

<div id="data_list">
<table border='1' rules='rows' cellspacing='0'>
<tr>
<th>Date</th>
<th>Amount</th>
<th>First Name</th>
<th>Last Name</th>
<th>Address</th>
</tr>

<script type="text/javascript">

var new Array = Array("<table>");
var date = Array("date");
var firstName = Array("firstName");

function buildTable(){
var table1 = document.getElementById("D1"); //get the HTML id of the table
var len = new Array.length;
for (new Array=0; new Array<amount.length; new Array++)
var tr = document.createElement('tr');
table1.appendChild(tr);

var td = document.createElement('td'); //create a table cell
tr.appendChild(td);
var txt = document.createTextNode(new Array[i]); //create a text node
td.appendChild(txt);
//likewise--this is essentially duplicate code
var td = document.createElement('td');
tr.appendChild(td); //stick it in the table row
var txt = document.createTextNode(date[i]); //create a text node mirrowing the lastName value
td.appendChild(txt); //stick it in the table cell

var td = document.createElement('td');
tr.appendChild(td); //stick it in the table row
var txt = document.createTextNode(firstName[i]); //create a text node mirrowing the lastName value
td.appendChild(txt); //stick it in the table cell
}
}


window.onload= function(){buildTable();}




</script>

</div>

<div id="totals">

<table border='1' cellspacing='1'>
<tr>
<th id='sumTitle' colspan='2'>

</th>
<tr>
<th>Contributors</th>
<td>contributons</td>
</tr>
<tr>
<th>amount</th>
<td>$total</td>
</tr>
</table>


</div>

</body>
</html>


what is wrong with this picture?

worldtraveller
11-21-2010, 06:45 PM
When i use my original code in firebug, before this major change on fire fox, the error i get is this

Firebug's log limit has been reached. 0 entries not shown. Preferences
syntax error
[Break on this error] }\n
clist.htm (line 26)

missing ) after argument list
[Break on this error] document.writeln("<td>"firstName[counter]"</td>");\n

Philip M
11-21-2010, 06:47 PM
You seem to be using this forum as a substitute for your regular teachers/instructors. That - remedial teaching - is not what the forum is supposed to do.

Your code contains several obvious errors.

One more time:- Really and truly, you need to take the time to figure out how to use Firebug. It might take you a couple of hours now, but you will save many many times that in the upcoming weeks.

worldtraveller
11-21-2010, 06:50 PM
I don't really have a teacher for my online courses.
just a tutor, who does not give instruction or answer my questions about anything

i am stuck learning on my own totally , just restricted to text book and any resources on google, so its a tough one.

Philip M
11-21-2010, 06:54 PM
I don't really have a teacher for my online courses.
just a tutor, who does not give instruction or answer my questions about anything

i am stuck learning on my own totally , just restricted to text book and any resources on google, so its a tough one.

That is indeed tough, but people on this forum cannot be expected to stand in as unpaid replacements for your useless teacher. Otherwise there will be no end to it. Why do you not complain to the course supervisor?

worldtraveller
11-21-2010, 08:04 PM
Really i have been stuck on same question for a while now. I just really want the main answer to it. Seems no one wants to give me the answer to it.

I just want the answer to it and i will be able to learn from it.

After that I do not plan to post any more assignment questions on this forum at all. I understand about tying up people's time.
if possible if anyone like to help me out more on that one question. I have 2 loops/arrays that are not working, i really like to know the correct answer to doing though, its hard when no one gives feedback,

and yes i have complained to course supervisor.
thanks

Philip M
11-21-2010, 08:15 PM
When i use my original code in firebug, before this major change on fire fox, the error i get is this

Firebug's log limit has been reached. 0 entries not shown. Preferences
syntax error
[Break on this error] }\n
clist.htm (line 26)

missing ) after argument list
[Break on this error] document.writeln("<td>"firstName[counter]"</td>");\n


I do not see either of these in the code you posted. But once they are pointed out to you, then you should be able to correct the errors yourself.

worldtraveller
11-21-2010, 08:16 PM
This is the external file we are supposed to get it all from


/*
New Perspectives on JavaScript
Tutorial 3
Case Problem 1

Filename: list.js

Variable List:

firstName: Contains the first name of the contributors.
lastName: Contains the last name of the contributors.
street: Contains the contributor's street address
city: Contains the contributor's city of residence
state: Contains the contributor's state of residence
zip: Contains the contributor's zip code.
amount: Contains the amount of the contribution.
date: Contains the date of the contribution.

*/

firstName = new Array();
lastName = new Array();
street = new Array();
city = new Array();
state= new Array();
zip = new Array();
amount = new Array();
date = new Array()


firstName[0]="Nina";
lastName[0]="Largent";
street[0]="88 Regal Lane";
city[0]="Wheaton";
state[0]="KY";
zip[0]="89780";
amount[0]=125;
date[0]="2007-09-18";


firstName[1]="Mike";
lastName[1]="Hunt";
street[1]="404 Barrow Street";
city[1]="Drake";
state[1]="KY";
zip[1]="88841";
amount[1]=75;
date[1]="2007-09-18";


firstName[2]="Monica";
lastName[2]="Lang";
street[2]="743 Stawlings Drive";
city[2]="Youngston";
state[2]="KY";
zip[2]="89747";
amount[2]=50;
date[2]="2007-09-16";


firstName[3]="William";
lastName[3]="Mcknight";
street[3]="102 Maple Lane";
city[3]="Youngston";
state[3]="KY";
zip[3]="88660";
amount[3]=150;
date[3]="2007-09-15";


firstName[4]="Latrina";
lastName[4]="Hults";
street[4]="750 Whitehall Road";
city[4]="Drake";
state[4]="KY";
zip[4]="89664";
amount[4]=250;
date[4]="2007-09-14";


firstName[5]="Danny";
lastName[5]="Shamblin";
street[5]="123 Smith Drive";
city[5]="Delphi";
state[5]="KY";
zip[5]="88585";
amount[5]=50;
date[5]="2007-09-13";


firstName[6]="Tina";
lastName[6]="Ammons";
street[6]="888 Evans Way";
city[6]="Wheaton";
state[6]="KY";
zip[6]="89847";
amount[6]=50;
date[6]="2007-09-13";


firstName[7]="Joanne";
lastName[7]="Fine";
street[7]="210 Bowling Terrace";
city[7]="Wheaton";
state[7]="KY";
zip[7]="88632";
amount[7]=125;
date[7]="2007-09-11";


firstName[8]="Charlotte";
lastName[8]="Foulk";
street[8]="109 South Road";
city[8]="Youngston";
state[8]="KY";
zip[8]="88835";
amount[8]=50;
date[8]="2007-09-10";


firstName[9]="Candice";
lastName[9]="Alfaro";
street[9]="108 Atwood Avenue";
city[9]="Delphi";
state[9]="KY";
zip[9]="89611";
amount[9]=400;
date[9]="2007-09-08";


firstName[10]="Kristi";
lastName[10]="Laine";
street[10]="512 North Lane";
city[10]="Wheaton";
state[10]="KY";
zip[10]="88754";
amount[10]=225;
date[10]="2007-09-08";


firstName[11]="Elisabeth";
lastName[11]="Carbone";
street[11]="381 Main Street";
city[11]="Drake";
state[11]="KY";
zip[11]="89704";
amount[11]=200;
date[11]="2007-09-07";


firstName[12]="James";
lastName[12]="Larsen";
street[12]="212 Rawlings Way";
city[12]="Jasper";
state[12]="KY";
zip[12]="88646";
amount[12]=125;
date[12]="2007-09-07";


firstName[13]="Ralph";
lastName[13]="Thornton";
street[13]="444 Smith Drive";
city[13]="Delphi";
state[13]="KY";
zip[13]="89755";
amount[13]=100;
date[13]="2007-09-07";


firstName[14]="Robin";
lastName[14]="Witt";
street[14]="78 Norland Pines";
city[14]="Drake";
state[14]="KY";
zip[14]="88711";
amount[14]=75;
date[14]="2007-09-07";


firstName[15]="Alex";
lastName[15]="Ruiz";
street[15]="102 Sunset Road";
city[15]="Jasper";
state[15]="KY";
zip[15]="89614";
amount[15]=50;
date[15]="2007-09-06";


firstName[16]="Callie";
lastName[16]="Rudy";
street[16]="3 Sunset Road";
city[16]="Jasper";
state[16]="KY";
zip[16]="88576";
amount[16]=50;
date[16]="2007-09-06";


firstName[17]="Michael";
lastName[17]="Harrell";
street[17]="125 Sunset Road";
city[17]="Jasper";
state[17]="KY";
zip[17]="89711";
amount[17]=50;
date[17]="2007-09-06";


firstName[18]="Edgar";
lastName[18]="Morales";
street[18]="387 North Lane";
city[18]="Wheaton";
state[18]="KY";
zip[18]="89802";
amount[18]=250;
date[18]="2007-09-05";


firstName[19]="Arlene";
lastName[19]="Lutz";
street[19]="7888 Clear View Drive";
city[19]="Youngston";
state[19]="KY";
zip[19]="88699";
amount[19]=75;
date[19]="2007-09-05";


firstName[20]="Earl";
lastName[20]="Holmes";
street[20]="1001 Rawlings Way";
city[20]="Jasper";
state[20]="KY";
zip[20]="88781";
amount[20]=500;
date[20]="2007-09-04";


firstName[21]="Bernice";
lastName[21]="Drew";
street[21]="25 Main Street";
city[21]="Drake";
state[21]="KY";
zip[21]="88786";
amount[21]=150;
date[21]="2007-09-04";


firstName[22]="Patrick";
lastName[22]="Granier";
street[22]="100 Atwood Avenue";
city[22]="Delphi";
state[22]="KY";
zip[22]="88557";
amount[22]=75;
date[22]="2007-09-03";


firstName[23]="Henry";
lastName[23]="Bailey";
street[23]="37 East Maple Street";
city[23]="Youngston";
state[23]="KY";
zip[23]="89554";
amount[23]=50;
date[23]="2007-09-03";


firstName[24]="Ginny";
lastName[24]="Rainey";
street[24]="657 Dawson Lane";
city[24]="Youngston";
state[24]="KY";
zip[24]="88873";
amount[24]=50;
date[24]="2007-09-03";


firstName[25]="Ginny";
lastName[25]="Rainey";
street[25]="657 Dawson Lane";
city[25]="Youngston";
state[25]="KY";
zip[25]="88873";
amount[25]=75;
date[25]="2007-09-03";


firstName[26]="Basilia";
lastName[26]="Lu";
street[26]="851 Flad Court";
city[26]="Jasper";
state[26]="KY";
zip[26]="88633";
amount[26]=500;
date[26]="2007-09-02";


firstName[27]="Livia";
lastName[27]="Mckinnon";
street[27]="557 Ivy Avenue";
city[27]="Jasper";
state[27]="KY";
zip[27]="88960";
amount[27]=50;
date[27]="2007-08-31";


firstName[28]="Kris";
lastName[28]="Levesque";
street[28]="542 Upton Avenue";
city[28]="Delphi";
state[28]="KY";
zip[28]="88793";
amount[28]=100;
date[28]="2007-08-31";


firstName[29]="Lynwood";
lastName[29]="Ingersoll";
street[29]="723 Jackson Avenue";
city[29]="Delphi";
state[29]="KY";
zip[29]="88802";
amount[29]=500;
date[29]="2007-08-30";


firstName[30]="Petronila";
lastName[30]="Damico";
street[30]="44 Stewart Street";
city[30]="Drake";
state[30]="KY";
zip[30]="88604";
amount[30]=250;
date[30]="2007-08-30";


firstName[31]="Hugh";
lastName[31]="Warren";
street[31]="585 Lindon Court";
city[31]="Wheaton";
state[31]="KY";
zip[31]="88877";
amount[31]=50;
date[31]="2007-08-28";


firstName[32]="Tom";
lastName[32]="Thomas";
street[32]="Rigel Avenue";
city[32]="Drake";
state[32]="KY";
zip[32]="89411";
amount[32]=100;
date[32]="2007-08-27";


firstName[33]="Steve";
lastName[33]="Bones";
street[33]="900 Lawton Street";
city[33]="Wheaton";
state[33]="KY";
zip[33]="89211";
amount[33]=50;
date[33]="2007-08-25";


firstName[34]="Jeri";
lastName[34]="White";
street[34]="Hawkes Lane";
city[34]="Delphi";
state[34]="KY";
zip[34]="89211";
amount[34]=150;
date[34]="2007-08-25";


hope this makes sense

Philip M
11-21-2010, 09:08 PM
hope this makes sense

No, it doesn't. What is your probem? It is no use just vomiting your undigested code over the forum and expecting us to guess what your problem is. It would have been quite enough to show details of the first 3 or 4 contributors, not 34. And it seems to be a completely different question to the one you were looking at before.

To be frank, I think you have taken your pitcher to this well often enough. As I say, you cannot expect this forum to act as unpaid substitutes for your useless tutor.

You are welcome to ask for guidance on specific Javascript topics - examples, how do I reference an element in a form, or how do I count the number of checked checkboxes, - but you should not ask people to plough through great wodges of code to find syntax or other basic errors. Nor, as I say, to provide remedial tuition or act as some sort of on-line textbook.

worldtraveller
11-21-2010, 09:18 PM
Ok i am going to move on to a nother question now, and focus on exercises. I posted another forum but it is more specific. to just javascript.

Thanks all

Old Pedant
11-21-2010, 10:13 PM
When i use my original code in firebug, before this major change on fire fox, the error i get is this
...
missing ) after argument list
document.writeln("<td>"firstName[counter]"</td>");\n

And I *told* you about this problem--and even fixed one of your lines of code--in my posts #3 and #6 in this thread.

I will show you ONE MORE TIME:


document.writeln("<td>" [B]+ firstName[counter] + "</td>");

I have to think that you have just skipped over parts of your tutorial materials if you thought you could just plunk values together like that without the plus signs. I can't imagine any book or online material that would tell you that you can omit those very much needed operators.

ALSO: The \n DOES NOT BELONG THERE! PERIOD. You can ONLY use \n in JavaScript code *INSIDE* of quotes.

worldtraveller
11-21-2010, 11:34 PM
ok i put this as my line


document.writeln("<td>" + firstName[counter] + "</td>");


would i do the same for the other lines like last name etc?

so u mentioned the the </td> does not belong

then what would i write in it spot?
</tr>

Old Pedant
11-21-2010, 11:38 PM
I hereby quote myself:


The \n DOES NOT BELONG THERE!


No, I did not "mentioned the the </td> does not belong".

You seem to have some trouble reading our answers. It probably explains why you have trouble reading whatever tutorial materials you have.

**********


would i do the same for the other lines like last name etc?

I would ask what you think the answer should be, but I won't.

Yes. Of course. It's the same error, repeated over and over. Why would you not fix that same error in the same way, repeated over and over.

worldtraveller
11-21-2010, 11:46 PM
I do not see the \n in my line?

Old Pedant
11-21-2010, 11:52 PM
When i use my original code in firebug, before this major change on fire fox, the error i get is this

Firebug's log limit has been reached. 0 entries not shown. Preferences
syntax error
[Break on this error] }\n
clist.htm (line 26)

missing ) after argument list
[Break on this error] document.writeln("<td>"firstName[counter]"</td>");\n

You don't see any \n in your code?

Then why did FireBug complain about it?

Well... maybe you didn't have it in your code. Okay. Maybe FireBug is just telling you that it detected the problem at the linebreak (end of the line) in your code.

Okay...if you don't really have it there, ignore me on this one.

But I clearly never said to change the </td>

worldtraveller
11-22-2010, 12:06 AM
Ok in my actual editor i can not see the \n
but when i run firefox in the firebug

i can see the \n
i dont know y i can not get rid of it.

worldtraveller
11-22-2010, 12:38 AM
Ya it is picking it up on this

line

document.writeln("<td>"+street[counter]+" "+city[counter]+" "state[counter]+" "+zip[counter]+"</td>");

but only visible on firebug,

do i need to put in something between the " " to make it go away?

Old Pedant
11-22-2010, 12:53 AM
No, I think Firebug is just telling you it saw an end of line after that. Let it go for now.

You have other problems to worry about.

worldtraveller
11-22-2010, 01:01 AM
very true, still on that loop hmm



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum