PDA

View Full Version : Loop problem?


Javascript2005
01-12-2005, 10:56 PM
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!

Badman3k
01-12-2005, 11:15 PM
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:

outStr = line3 + line4 + line5 + line7
document.write (outStr)


That should solve it :thumbsup:

Javascript2005
01-13-2005, 01:03 AM
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:

outStr = line3 + line4 + line5 + line7
document.write (outStr)


That should solve it :thumbsup:

i'm confused sorry but can u show me how to correct it?

glenngv
01-13-2005, 01:52 AM
Another solution (which is the better) is to concatenate the strings:

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

Javascript2005
01-13-2005, 02:15 AM
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?

glenngv
01-13-2005, 02:35 AM
We need to see the whole code or a link to it.

Javascript2005
01-13-2005, 09:26 AM
<!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>

glenngv
01-13-2005, 10:35 AM
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 tags to preserve formatting.

jbot
01-13-2005, 11:14 AM
this is cross-posted here (http://www.webmasterworld.com/forum91/3023.htm), chaps! :(

Javascript2005
01-13-2005, 11:15 AM
check url below

glenngv
01-13-2005, 11:20 AM
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?

Javascript2005
01-13-2005, 11:26 AM
Here is a direct link to the web page (http://studentnet.kingston.ac.uk/~k0308971/wwwinternetK0308971.htm)

glenngv
01-13-2005, 02:43 PM
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.

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.

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"
...
}

Javascript2005
01-13-2005, 03:11 PM
thank you glenngv for all your help. :thumbsup:

jbot
01-13-2005, 03:38 PM
thank you glenngv for all your help. :thumbsup:

if that's it finished, you should now close your other thread on Webmaster World! don't want to upset peeps there.