Jump to content

Please help, Seems to keep running don't know why???


morfusaf

Recommended Posts

Hello, I have a homework assignment and basically I have to use arrays and loop to display my top 20 fav songs and year they were produced..

 

so i did some changing it up and useing extra stuff to show off.. the only problem is... when it runs.. it seems to get stuck.. don't know why, my only loop should end.. also.. you can't reload the page to start over thats odd???

 

well here is the script..

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-Strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<title>Homework Assignment 1</title>

<script type="text/javascript">

<!-- HIDE FROM INCOMPATIBLE BROWSERS

var favSongList = new Array(20);

var songDateList = new Array(20);

 

favSongList[0] = "Song Name";

songDateList[0] = "1984";

favSongList[1] = "Song Name";

songDateList[1] = "1984";

favSongList[2] = "Song Name";

songDateList[2] = "1984";

favSongList[3] = "Song Name";

songDateList[3] = "1984";

favSongList[4] = "Song Name";

songDateList[4] = "1984";

favSongList[5] = "Song Name";

songDateList[5] = "1984";

favSongList[6] = "Song Name";

songDateList[6] = "1984";

favSongList[7] = "Song Name";

songDateList[7] = "1984";

favSongList[8] = "Song Name";

songDateList[8] = "1984";

favSongList[9] = "Song Name";

songDateList[9] = "1984";

favSongList[10] = "Song Name";

songDateList[10] = "1984";

favSongList[11] = "Song Name";

songDateList[11] = "1984";

favSongList[12] = "Song Name";

songDateList[12] = "1984";

favSongList[13] = "Song Name";

songDateList[13] = "1984";

favSongList[14] = "Song Name";

songDateList[14] = "1984";

favSongList[15] = "Song Name";

songDateList[15] = "1984";

favSongList[16] = "Song Name";

songDateList[16] = "1984";

favSongList[17] = "Song Name";

songDateList[17] = "1984";

favSongList[18] = "Song Name";

songDateList[18] = "1984";

favSongList[19] = "Song Name2";

songDateList[19] = "1984";

 

function startIt(){

var howManySongs = prompt("How many of my top 20 favorite songs would you like to be displayed. (1-20)", "");

if (howManySongs >= 1 && howManySongs <=20) {

var showDates = confirm("Do you wish to display the year the songs were published?");

print_song_list(howManySongs, showDates);

}

else

alert("I am sorry, you entered an incorrect value.  Please try again!");

}

 

function print_song_list(how_many, displayDates) {

for(count = 0; count != how_many; ++count) {

document.write("" + (count + 1) + ".) " + favSongList[count]);

if (displayDates == true)

document.write(", Published in " + songDateList[count] + ".<br />");

else

document.write("<br />");

}

return;

}

// Stop hiding from incompatible Browsers-->

</script>

</head>

<body>

<center>

<br /><br /><br />

  <input type="button" value="Click me to begin!" onclick="startIt();" />

</center>

</body>

</html>

 

 

 

 

ok keep inmind i dind't change the names of songs or dates yet.. I have also tried to use ...      return;      after the for statement.. :( pelase help..

Link to comment
Share on other sites

I'm not sure why, but when i use document.write() my pages always 'hang' like that too. Instead I usually add a div to the page with an ID, and use document.getElementById() or just use document.body.innerHTML:

 

document.body.innerHTML += "" + (count + 1) + ".) " + favSongList[count];

Link to comment
Share on other sites

I'm resisting the temptation to "fix" all of your code. But, for this particular problem.

 

This:

   for(count = 0; count != how_many; ++count) {[code]

Should be this:
[code]   for(count = 0; count != how_many; count++) {[code]

(The double plus (++) comes after the variable to increment it by 1.

[/code][/code][/code]

Link to comment
Share on other sites

I'm resisting the temptation to "fix" all of your code. But, for this particular problem.

 

This:

   for(count = 0; count != how_many; ++count) {[code]

Should be this:
[code]   for(count = 0; count != how_many; count++) {[code]

(The double plus (++) comes after the variable to increment it by 1.

[/code][/code][/code]

 

in JavaScript ++count works the same as count++

 

i was resistant to help too since it was an assignment....but they clearly understand the logic

Link to comment
Share on other sites

Everything looks ok to me. I think the issue might have to do with the browser and trying 40 write methods all at once. I would suggest you try building a string in your for loop and then only using one write method to write that string you made. Something like this:

function print_song_list(how_many, displayDates) {
   output = "";
   for(count = 0; count != how_many; ++count) {
      output = output + (count + 1) + ".) " + favSongList[count];
      if (displayDates == true) 
         output = output + ", Published in " + songDateList[count] + ".";
   }
   document.write(output);
   return;
}

Link to comment
Share on other sites

Hmmm, didn't know you could do ++x

 

Ok, well I just copied and pasted the code into a new page and it worked fine for me in IE (assuming the two line breaks in the document.write() calls were supposed to be BR tags. It only stall in FF.

Link to comment
Share on other sites

Thankz

I'm resisting the temptation to "fix" all of your code. But, for this particular problem.

 

This:

   for(count = 0; count != how_many; ++count) {[code]

Should be this:
[code]   for(count = 0; count != how_many; count++) {[code]

(The double plus (++) comes after the variable to increment it by 1.

[/code][/code][/code]

 

Well nothing I tried worked... thanks for the help though.  It mainly gets stuck in FF, but even in IE you can't use the reload button?

 

So what else was wrong with my code?.. like I said I spruced it up a bunch to do a bunch of extra stuff than required, just to see if I could. 

All i needed was a loop and an array. So what else is wrong with my code?

 

OH ya.. also I tried to do the

 

<-- hide this blah blah in the body like my book says, but when I try to use any type of input it doesn't show up on my FF or IE?

 

 

Link to comment
Share on other sites

OK, I think the problem here is that you are running document.write() after the page has loaded. If you run the script in IE and then do a view source you will see that the "content" of the page only includes the text that was written to the page using the document.write()'s. All the HTML markup and javascript is gone. FF must be getting confused by this.

 

If you use document.write during page load this does not occure.

 

This also explains why "refreshing" the page has no effect. The original code no longer exists in the browser so refreshing has no effect.

 

You should follow rhodesa's advice and use a div and add the text to the div using innerHTML.

 

I just rewrote your code using that method and it works perfectly.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.