PDA

View Full Version : Problem with Arrays...

Wardy118
07-13-2006, 11:05 PM
What i'm trying to do is use an array to store the distance travelled by a robot each day and then a total distance throughout the entire mission. I also need to add in the Javascript program print outs of the distance travelled each day and since the begining of the mission. So far i have spent ages on the first 2 parts and just cant seem to get it to work. I cant seem to find anything in my book on how to add a print out function.

Here is what i've got so far:

<HTML>
<TITLE> Solution_8.2.7
</TITLE>
<SCRIPT>

// Program to calculate the distance travelled by the Robot each day
// and the total travelled since the begining of the mission

var distanceArray = [35,42,12,18,67,53,41,47,33,27];
var totalDistance;
var dayDistanceArray = [1,2,3,4,5,6,7,8,9,10];

totalDistance = 0;

for (var day = 0; day < distanceArray.length; day = day + 1)
{
totalDistance = totalDistance + distanceArray[day]
}
};

document.write('On day'
+dayDistanceArray[dayDistanceIndex] + 'the robot travelled'
+distanceArray[distanceIndex] + 'it has travelled
'+ totalDistance + ' metres since the begining of the mission' + '<BR>')

</SCRIPT>
<BODY>
</BODY>
</HTML>

Any idea where i'm going wrong?

07-13-2006, 11:09 PM
You seem to have a syntax error, what errors are you receiving?

Wardy118
07-13-2006, 11:24 PM
Not to sure. With the course i am doing i am given links and then told to look at the source via the internet,change it and save it using notepad, and then refresh the internet explorer to see what happens. At the moment i just get a blank white screen.

Kor
07-13-2006, 11:32 PM
you used 2 variables which are not defined, dayDistanceIndex and distanceIndex

Wardy118
07-14-2006, 12:02 PM
Does that mean that i need to change them to dayDistanceArray and distanceArray?

Kor
07-14-2006, 08:20 PM
that means that you have to define those variables a way or another, or by asigment or by an input...

Are u sure u understand what you are looking for? What your code suppose to do, anyway?

Wardy118
07-31-2006, 12:55 PM
that means that you have to define those variables a way or another, or by asigment or by an input...

Are u sure u understand what you are looking for? What your code suppose to do, anyway?

Hi Kor,

Basically what the code is supposed to do is use an array to store the distance travelled by the robot each day and print it out(which is the distanceArray variable). This array should then be used to calcuate the total distance travelled from the 1st day onwards and print out this information. I then basically want the output to say something like:

"On day 1 the robot travelled 35 metres, it has travelled 35 metres since the beginning of the mission."
"On day 2 the robot travelled 42 metres, it has travelled 77 metres since the beginning of the mission."

Kor
07-31-2006, 01:32 PM
You need a sigle array. What you set as var dayDistanceArray is, in fact, the indexes +1 of the first array elements

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
var distanceArray = [35,42,12,18,67,53,41,47,33,27];
function calculate(){
var tot=0;
var txt='';
for(var i=0;i<distanceArray.length;i++){
tot+=distanceArray[i];
txt+='On day '+(i+1)+' the robot travelled '+distanceArray[i]+' meters, it has travelled '+tot+' meters since the beginning of the mission<br>';
}
document.getElementById('mydiv').innerHTML=txt
}
</script>
<body>
<div id="mydiv"></div>
</body>
</html>

------
What was wrong was that you need to se the text to be write inside the loop, and write it once, but after the loop is done. And I have used innerHTML rather than document.write(). document.write() is not a dynamic method.

Wardy118
08-01-2006, 01:12 PM
Thanks alot for all your help Kor. I have just completed the final stages of the program so it should be fingers crossed up and running soon.

cheers again

Wardy