Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 3 123 LastLast
Results 1 to 15 of 32
  1. #1
    Regular Coder
    Join Date
    Nov 2010
    Location
    canada
    Posts
    131
    Thanks
    51
    Thanked 0 Times in 0 Posts

    For Loops and Javascript

    This is a final loops in an exercise

    here is my code

    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

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,903
    Thanks
    15
    Thanked 226 Times in 226 Posts
    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.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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:
    Code:
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    worldtraveller (11-20-2010)

  • #4
    Regular Coder
    Join Date
    Nov 2010
    Location
    canada
    Posts
    131
    Thanks
    51
    Thanked 0 Times in 0 Posts
    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

  • #5
    Regular Coder
    Join Date
    Nov 2010
    Location
    canada
    Posts
    131
    Thanks
    51
    Thanked 0 Times in 0 Posts
    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>

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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:
    Code:
        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:
    Code:
        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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Regular Coder
    Join Date
    Nov 2010
    Location
    canada
    Posts
    131
    Thanks
    51
    Thanked 0 Times in 0 Posts
    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.

  • #8
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,215
    Thanks
    12
    Thanked 338 Times in 334 Posts
    I really need to stress out that the command is document.writeln(), not document.writeIn() (as DrDOS already mentioned)
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Quote Originally Posted by worldtraveller View Post
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    worldtraveller (11-21-2010)

  • #10
    Regular Coder
    Join Date
    Nov 2010
    Location
    canada
    Posts
    131
    Thanks
    51
    Thanked 0 Times in 0 Posts
    I will post on this forum ,my corrected code, but still not working, thanks much though

  • #11
    Regular Coder
    Join Date
    Nov 2010
    Location
    canada
    Posts
    131
    Thanks
    51
    Thanked 0 Times in 0 Posts
    ok i been working on my loops etc i icame up with this in a lesson

    Code:
    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

  • #12
    Regular Coder
    Join Date
    Nov 2010
    Location
    canada
    Posts
    131
    Thanks
    51
    Thanked 0 Times in 0 Posts
    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
    Code:
    <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

  • #13
    Regular Coder
    Join Date
    Nov 2010
    Location
    canada
    Posts
    131
    Thanks
    51
    Thanked 0 Times in 0 Posts

    Loops and Arrays problesm

    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

  • #14
    Regular Coder
    Join Date
    Nov 2010
    Location
    canada
    Posts
    131
    Thanks
    51
    Thanked 0 Times in 0 Posts

    Loops and Arrays problesm

    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
    Code:
    <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?

  • #15
    Regular Coder
    Join Date
    Nov 2010
    Location
    canada
    Posts
    131
    Thanks
    51
    Thanked 0 Times in 0 Posts
    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


  •  
    Page 1 of 3 123 LastLast

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •