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.
Results 1 to 15 of 15

Thread: Loop problem?

  1. #1
    New Coder
    Join Date
    Jan 2005
    Location
    UK
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Loop problem?

    Code:
    function printExam() {
    line3 = "<h3>" + this.code + " " + this.modName + " " + "Exam Papers" + "<\/h3>\n"
    line4 = "<ul style=\"list-style: none\">\n"
    line5 = "<\/ul>\n"
    for (var i = this.num; i > 0; i--) {
    line7 = '<a href="www.google.com/' + this.modId + "/" + this.examDir
    + "/" + this.modId + "Exam"
    + dateToAbbrevSessOffset(new Date(), -i)
    + ".pdf" + "\">"
    + dateToSessionOffset(new Date(), -i)
    + " " + "PDF version" + "<\/a>\n"
    }
    outStr = line3 + line4 + line5 + line7
    document.write (outStr)
    }
    
    function module(modId,code,modName,course,modDir,examDir, num) {
    
    this.modId = modId;
    this.code = code;
    this.modName = modName;
    this.course = course;
    this.modDir = modDir;
    this.examDir = examDir;
    this.num = num;
    this.printModule = printModule;
    this.printExam = printExam;
    
    }
    I have got a problem with the for loop above. the page displays fine enough but heres the problem. if this.num = 2, it should display two hyperlinks. if this.num = 1, it would display one hyperlink etc.

    when this.num = 1, it displays one link fine but when it is greater than 1, it can not seem to display more than one link.

    what is wrong with the loop or coding? this Javascript code is driving me mad!
    Last edited by Javascript2005; 01-13-2005 at 12:26 PM.

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Edinburgh, UK
    Posts
    402
    Thanks
    2
    Thanked 1 Time in 1 Post
    Your problem is the fact that you're not printing within your loop, you're printing outside it, which means you're printing the values that you're loop produces last.

    To solve it move the following two lines within the for loop:
    Code:
    outStr = line3 + line4 + line5 + line7
    document.write (outStr)
    That should solve it
    Rich

    "An expert is a person who has made all the mistakes that can be made in a very narrow field."

  • #3
    New Coder
    Join Date
    Jan 2005
    Location
    UK
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Badman3k
    Your problem is the fact that you're not printing within your loop, you're printing outside it, which means you're printing the values that you're loop produces last.

    To solve it move the following two lines within the for loop:
    Code:
    outStr = line3 + line4 + line5 + line7
    document.write (outStr)
    That should solve it
    i'm confused sorry but can u show me how to correct it?

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Another solution (which is the better) is to concatenate the strings:

    line7 += '<a href="....';
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #5
    New Coder
    Join Date
    Jan 2005
    Location
    UK
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by glenngv
    Another solution (which is the better) is to concatenate the strings:

    line7 += '<a href="....';
    I have just tried that. it works but I'm getting the word undefined (firefox) along with the links. but it don't work in IE. how can i solve this?
    Last edited by Javascript2005; 01-13-2005 at 10:34 AM.

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    We need to see the whole code or a link to it.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #7
    New Coder
    Join Date
    Jan 2005
    Location
    UK
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    whole code - save as htm document to see what I mean.

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
     "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head><title>Phil Molyneux Modules Page</title>
    <style type="text/css">
        body  {
          background-color: white;
          color: black;
        }
      </style>
    
      <script type="text/javascript" language="JavaScript"
              src="javascript/dateUtilities.js"></script> 
      <script type="text/javascript" language="JavaScript"
              src="javascript/docUtilities.js"></script>
    
    
    <script language="JavaScript" type="text/javascript">
    
    function initDoc() {
      // tests to see if the utilities are loaded
      if (window.appendDocSourceDiv) {
        //alert("window.appendDocSource is here");
        appendDocSourceDiv(document);
      } else {
        alert("function appendDocSourceDiv is not here");
      }
      return true ;
    }
    </script>
    <script language="JavaScript" type="text/javascript">
    
    function printModule() {
    	line1 = "<h3>" + this.code + " " + this.modName + " " 
                         + " " + currDateToSession() + "</h3>"
    	line2 = '<p><a href="http://www.kingston.ac.uk/~ku00597/' + this.modId + '\">Module Directory</a></p>'
    	document.write(line1, line2);
    }
    
    function printExam() {
    	line3 = "<h3>" + this.code + " " + this.modName + " " + "Exam Papers" + "<\/h3>\n"
    	line4 = "<ul style=\"list-style: none\">\n"
    	line5 = "<\/ul>\n"
    	for (var i = this.num; i > 0; i--) {
            line7  += '<a href="http://www.kingston.ac.uk/~ku00597/' + this.modId + "/" + this.examDir
                        + "/" + this.modId + "Exam" 
                        + dateToAbbrevSessOffset(new Date(), -i) 
                        + ".pdf" + "\">"
                        + dateToSessionOffset(new Date(), -i)
                        + " " + "PDF version" + "<\/a>\n"        
           }
    	outStr = line3 + line4 + line5 + line7
          	document.write (outStr)  
        }
    
    function module(modId,code,modName,course,modDir,examDir, num) {
    
    this.modId = modId;
    this.code = code;
    this.modName = modName;
    this.course = course;
    this.modDir = modDir;
    this.examDir = examDir;
    this.num = num;
    this.printModule = printModule;
    this.printExam = printExam;
    
    }
    
    </script>
    
    </head>
    
    <body onload="initDoc();">
    
    <script language="JavaScript" type="text/javascript">
    
    	// Outputs the top heading with current year
    	var head = "<h1>Phil Molyneux <br>Teaching Notes for " + currDateToSession() + "</h1>"
    	document.write(head);
    </script>
    
    <script language="JavaScript" type="text/javascript">
    software = new module("softdev", "BB1659", "Software Development", "BIT1", "softdev", "Exams", 3);
    software.printModule();
    software.printExam();
    </script>
    
    
    <p>dfs</p>
    
    
    </body>
    </html>
    Last edited by Javascript2005; 01-13-2005 at 12:11 PM.

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts

    Incomplete

    You didn't post the js utilities. Do you have the page online? It's easier to test when it's online. And next time when you post codes, please enclose them in [code][/code] tags to preserve formatting.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #9
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    this is cross-posted here, chaps!
    *keep it simple (TM)

  • #10
    New Coder
    Join Date
    Jan 2005
    Location
    UK
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    check url below
    Last edited by Javascript2005; 01-13-2005 at 12:40 PM.

  • #11
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Since you have a site, then why don't you just upload the page there and post the link here instead of me having to set the whole thing up locally?
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #12
    New Coder
    Join Date
    Jan 2005
    Location
    UK
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Last edited by Javascript2005; 01-13-2005 at 12:37 PM.

  • #13
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    After a bunch of testing, i found out that you forgot to initialize line7 variable to empty. Since you are concatenating itself (line7 += "..."), it must be initialized to an empty string.
    Code:
    function printExam() {
      var line7='';
      ...
    }
    I suggest you to make it a habit to declare variables especially the local variables. If you don't declare a variable (with keyword var), it is treated as a global variable. You might get unexpected results when same undeclared variables in multiple functions exist especially if they call each other.
    Code:
    function printExam() {
      var line7='';
      var line3 = "<h3>" + this.code + " " + this.modName + " " + "Exam Papers" + "<\/h3>\n"
      var line4 = "<ul style=\"list-style: none\">\n"
      var line5 = "<\/ul>\n"
      ...
    }
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #14
    New Coder
    Join Date
    Jan 2005
    Location
    UK
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you glenngv for all your help.

  • #15
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Javascript2005
    thank you glenngv for all your help.
    if that's it finished, you should now close your other thread on Webmaster World! don't want to upset peeps there.
    *keep it simple (TM)


  •  

    Posting Permissions

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