I am working on an optional exercise for a course, being optional no one has posted any helpful feedback on the course forum. I have finished the code as instructed, to the best of my ability and cannot located the problem.
The script is supposed to activate a red ball when clicked to travel around a national park map. Whenever the ball hits a landmark it is supposed to stop, a text description is supposed to appear as well as a photo in some cases. Once the ball makes it to the end of the map it is supposed to reload the page after a 7 second delay.
Below is my js file and my htm file. I have placed comments within the code to explain whenever possible what I have been asked to do. There are quite a few pictures for this so I won't post them unless asked.
I think this is a very interesting way to use JavaScript and would really like to get the program working. A lot of the content inside is a first for me, so be patient please when you run into an error and help me to correct my ways. Thanks to you!
/*
Filename: trail.js
Function List:
placeIt(id, x, y)
Places the id object at the coordinates (x,y)
hideIt(id)
Hides the id object by setting its visibility style to "hidden"
showIt(id)
Hides the id object by setting its visibility style to "visible"
*/
function placeIt(id, x, y) {
object=document.getElementById(id);
object.style.left=x+"px";
object.style.top=y+"px";
}
function showIt(id) {
object=document.getElementById(id);
object.style.visibility="visible";
}
function hideIt(id) {
object=document.getElementById(id);
object.style.visibility="hidden";
}
<html>
<head>
<!--
Filename: trail.htm
Supporting files: photo2.gif-photo8.gif, redball.gif, text1.gif-text9.gif,
trail.css, trail.gif, trail.js
-->
<title>Heart of Rocks Trail</title>
<link href="trail.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="trail.js"> </script>
<script type="text/javascript">
/*
Trail Map
Variable List:
x
Contains an array of the x-coordinates of the trail map path
y
Contains an array of the y-coordinates of the trail map path
*/
var xArray = new Array(304,306,310,314,318,323,327,330,332,333,334,335,336,
336,335,333,330,327,324,322,321,322,325,328,331,334,337,339,340,338,337,
338,339,340,342,343,345,347,349,351,353,355,358,361,365,368,373,378,384,
391,397,404,411,418,426,434,442,451,458,465,472,479,484,490,499,504,505,
506,507,507,502,497,492,486,480,474,468,462,457,453,450,447,443,438,433,
429,424,418,413,408,404,400,396,389,386,381,379,378,378,377,377,364,342,
336,330,323,316,309,301,293,285,277,270,263,257,251,245,236,230,224,219,
213,209,204,200,196,191,187,182,177,172,166,162,159,155,152,148,143,137,
130,123,116,110,103,97,93,93,94,94,93,91,88,85,81,78,75,73,72,75,80,85,
90,95,99,103,108,112,116,120,122,123,122,121,119,118,118,121,126,129,132,
135,140,146,154,163,172,180,185,188,192,198,204,210,212,215,225,230,236,
242,248,255,262,270,278,286,293,300,306,312,317,322,330,334,336,337,337,
338,338,339,339,339,339,335,330,328,325,323,321,324,330,337,344,352,360,
367,373,379,384,385,385,391,397,405,412,421,430,438,445,452,457,462,464,
467,471,475,480,486,491,498,504,510,516,521,525,529,527,527,532,537,543);
var yArray = new Array(438,432,426,422,419,415,410,405,400,395,389,382,375,
367,359,354,350,345,341,336,330,325,320,315,310,305,301,296,290,285,279,
271,266,260,254,248,241,234,226,220,213,207,202,197,192,189,187,185,184,
183,182,182,182,182,182,182,183,183,183,183,184,184,183,183,181,179,175,
170,164,157,151,149,147,146,145,143,142,139,135,131,126,122,117,113,110,
107,104,102,99,97,95,93,91,87,84,80,78,77,76,76,75,71,67,66,66,65,64,63,
63,62,61,61,60,59,58,58,57,56,55,54,52,49,46,43,39,35,31,27,24,21,19,20,
23,27,31,35,39,41,42,42,41,40,39,39,41,47,54,61,68,75,80,85,90,95,100,
105,110,115,120,122,124,126,127,130,133,137,141,145,150,155,161,168,174,
180,185,190,196,198,203,209,212,216,218,219,218,218,219,222,226,229,229,
229,229,234,238,241,241,242,242,242,242,242,242,242,242,242,242,242,243,
243,243,250,250,255,260,266,273,280,288,295,301,307,313,316,321,326,332,
339,346,348,348,348,348,348,348,348,349,353,358,363,366,367,367,366,365,
365,364,364,365,367,369,372,375,379,382,385,387,390,393,396,399,402,405,
408,416,422,428,433,436,439);
var index = 1;
// hide all text and photo objects on the screen as the red ball travels around the trail
function hideAll() {
hideIt("text1");
hideIt("text2");
hideIt("photo2");
hideIt("text3");
hideIt("photo3");
hideIt("text4");
hideIt("text5");
hideIt("photo5");
hideIt("text6");
hideIt("photo6");
hideIt("text7");
hideIt("photo7");
hideIt("text8");
hideIt("photo8");
hideIt("text9");
}
//display the text description and photo for a given landmark
/* since not every landmark has a photo use object detection to determine whether a div element with the value of the photo parameter exists and if it does call the showIt() function to display the photo */
function describeIt(text, photo) {
showIt("text1");
if (document.getElementById("photo1")) {
showIt("photo1"); }
showIt("text2");
if (document.getElementById("photo2")) {
showIt("photo2"); }
showIt("text3");
if (document.getElementById("photo3")) {
showIt("photo3"); }
showIt("text4");
if(document.getElementById("photo4")) {
showIt("photo4");}
showIt("text5");
if(document.getElementById("photo5")) {
showIt("photo5"); }
showIt("text6");
if(document.getElementById("photo6")) {
showIt("photo6"); }
showIt("text7");
if(document.getElementById("photo7")) {
showIt("photo7"); }
showIt("text8");
if(document.getElementById("photo8")) {
showIt("photo8") ; }
showIt("text9"); {
if (document.getElementById("photo9")) {
showIt("photo9"); }
}
}
// move the red ball along the hiking trail, stopping if the red ball encounters one of the landmarks
function runPath() {
hideAll(); // call the hideAll() function to ensure that all text and photos are hidden on the page
index ++; // increase the value of the index variable by 1
placeIt("redBall", xArray[index], yArray[index]) // place the redball at the xArray index, and yArray index
switch(index) {
case 0:
describeIt("text1");
break;
case 16:
describeIt("text2", "photo2");
break;
case 68:
describeIt("text3", "photo3");
break;
case 82:
describeIt("text4", "photo4");
break;
case 97:
describeIt("text5", "photo5");
break;
case 130:
describeIt("text6", "photo6");
break;
case 188:
describeIt("text7", "photo7");
break;
case 231:
describeIt("text8", "photo8");
break;
case 269:
describeIt("text9"); //call the describeIt() function with "text9"
var index = 0; // set the value of the index variable to 0
window.location.reload(7000); // reload the web page after a seven-second delay.
break;
default:
// the default behaviour is to call the runPath() function agasin after a 10th of a second
setTimeout("runPath()", 100);
}
}
</script>
</head>
<body>
<div id="trail">
<img src="trail.gif" width="650" height="490" alt="Heart of Rocks Trail" />
</div>
<!-- Mark the redBall.gif inline image as a link and set the href attricute of the link to run the runPath() function when the ball is clicked -->
<div id="redBall" onClick="runPath()">
<img src="redball.gif" alt="Click to follow trail" />
</div>
<div id="text1">
<img src="text1.gif" alt="A Welcome Message" />
</div>
<div id="text2">
<img src="text2.gif" alt="The Big Balanced Rock" />
</div>
<div id="photo2">
<img src="photo2.jpg" width="200" height="155" alt="Big Balanced Rock" />
</div>
<div id="text3">
<img src="text3.gif" alt="Duck on a Rock" />
</div>
<div id="photo3">
<img src="photo3.jpg" width="200" height="155" alt="Duck on a Rock" />
</div>
<div id="text4">
<img src="text4.gif" alt="The Camels Head" />
</div>
<div id="photo4">
<img src="photo4.jpg" width="200" height="155" alt="Camels Head" />
</div>
<div id="text5">
<img src="text5.gif" alt="Punch and Judy" />
</div>
<div id="photo5">
<img src="photo5.jpg" width="200" height="155" alt="Punch and Judy" />
</div>
<div id="text6">
<img src="text6.gif" alt="Thors Hammer" />
</div>
<div id="photo6">
<img src="photo6.jpg" width="200" height="155" alt="Thors Hammer" />
</div>
<div id="text7">
<img src="text7.gif" alt="The Pinnacle Balanced Rock" />
</div>
<div id="photo7">
<img src="photo7.jpg" width="125" height="200" alt="Pinnacle Balanced Rock" />
</div>
<div id="text8">
<img src="text8.gif" alt="The Balanced Rock" />
</div>
<div id="photo8">
<img src="photo8.jpg" width="200" height="155" alt="Big Balanced Rock" />
</div>
<div id="text9">
<img src="text9.gif" alt="A Short Exit Message" />
</div>
</body>
</html>