Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Problem with Arrays...

1. ## Problem with Arrays...

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?

• You seem to have a syntax error, what errors are you receiving?

• 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.

• you used 2 variables which are not defined, dayDistanceIndex and distanceIndex

• Does that mean that i need to change them to dayDistanceArray and distanceArray?

• 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?

• Originally Posted by Kor
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."

• You need a sigle array. What you set as var dayDistanceArray is, in fact, the indexes +1 of the first array elements
Code:
```<!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.

• 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

•

#### Posting Permissions

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