Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Jul 2006
    Posts
    81
    Thanks
    4
    Thanked 0 Times in 0 Posts

    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>
    <HEAD>
    <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>
    </HEAD>
    <BODY>
    </BODY>
    </HTML>

    Any idea where i'm going wrong?

  • #2
    Regular Coder PremiumBlend's Avatar
    Join Date
    Apr 2006
    Location
    Marion, Iowa
    Posts
    201
    Thanks
    0
    Thanked 13 Times in 13 Posts
    You seem to have a syntax error, what errors are you receiving?
    My Website: DumpsterDoggy

  • #3
    New Coder
    Join Date
    Jul 2006
    Posts
    81
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    you used 2 variables which are not defined, dayDistanceIndex and distanceIndex
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    New Coder
    Join Date
    Jul 2006
    Posts
    81
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Does that mean that i need to change them to dayDistanceArray and distanceArray?

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #7
    New Coder
    Join Date
    Jul 2006
    Posts
    81
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote 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."

  • #8
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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>
    <head>
    <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
    }
    onload=calculate
    </script>
    </head>
    <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.
    Last edited by Kor; 07-31-2006 at 01:35 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #9
    New Coder
    Join Date
    Jul 2006
    Posts
    81
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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
    •