Struggling with arrays and for loops

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Vik0711
    New Member
    • Jun 2010
    • 3

    Struggling with arrays and for loops

    Hiya,
    I am a student and I am struggling with some arrays I have. There are three arrays, all of which have been declared, two contain numbers, and the third contains names. I need to add the two with numbers together and display with the name. The results of the two numbers added up, needs to be stored in another array.

    Please help, I have been up all night, trying everything, looked everywhere, just a hint is all I need, a big hint would be great,

    Thanks :)
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    what do you have so far? and please explain a bit more what the output should be ...

    kind regards

    Comment

    • Vik0711
      New Member
      • Jun 2010
      • 3

      #3
      Hiya Gits,

      Its basically a little program to calculate the combine points scored. So for each team I need to work out their total score.

      I have firstly created another array called 'combinedpoints array' which is where I need the results to be stored.
      I then started writing the code for the maths...but I am so very lost now. This is what I have so far..

      Code:
      <SCRIPT LANGUAGE = "JavaScript">
       
      
      //names of people stored in array
      var namesArray = ['Dave and Doris', 'Fred and Elsie', 'Mike and Sue', 'Geoff and Vera', 
      'Paul and Dedrie'];
      
      //points awarded by first and second stored in arrays
      var firstPointsArray = [2,1,5,4,3];
      var secondPointsArray = [4,5,2,3,1];
      
      
      //Add code to declare a new array to store the combined points for each couple
      
      // I think I have this bit right...
      
      var combinedPointsArray = new Array(5);
      
      
      //Part  (i)(b)
      //Add code to calculate the combined points for each couple and store it in the combined points array
      // Its this bit that has really confused me
      	
      	for (var score = 0; score < names.length; score = score + 1)
      		{
      		combinedPointsArray = firstPointsArray [0] + secondPointsArray [0];
      		
      		document.write (namesArray + '<br>' + combinedPointsArray + '<br>');
      		}
      		
      </script>
      Thanks for taking the time to look at this for me :)
      Last edited by Dormilich; Jun 24 '10, 06:36 PM. Reason: Please use [code] tags when posting code

      Comment

      • Vik0711
        New Member
        • Jun 2010
        • 3

        #4
        Originally posted by Vik0711
        Hiya Gits,

        Its basically a little program to calculate the combine points scored. So for each team I need to work out their total score.

        I have firstly created another array called 'combinedpoints array' which is where I need the results to be stored.
        I then started writing the code for the maths...but I am so very lost now. This is what I have so far..

        Code:
        <SCRIPT LANGUAGE = "JavaScript">
         
        
        //names of people stored in array
        var namesArray = ['Dave and Doris', 'Fred and Elsie', 'Mike and Sue', 'Geoff and Vera', 
        'Paul and Dedrie'];
        
        //points awarded by first and second stored in arrays
        var firstPointsArray = [2,1,5,4,3];
        var secondPointsArray = [4,5,2,3,1];
        
        
        //Add code to declare a new array to store the combined points for each couple
        
        // I think I have this bit right...
        
        var combinedPointsArray = new Array(5);
        
        
        //Part  (i)(b)
        //Add code to calculate the combined points for each couple and store it in the combined points array
        // Its this bit that has really confused me
        	
        	for (var score = 0; score < names.length; score = score + 1)
        		{
        		combinedPointsArray = firstPointsArray [0] + secondPointsArray [0];
        		
        		document.write (namesArray + '<br>' + combinedPointsArray + '<br>');
        		}
        		
        </script>
        Thanks for taking the time to look at this for me :)
        Thanks Dormilich, I am a total newbie. Sorry for the mistake

        Vik :)

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5390

          #5
          there are 2 issues ...

          first:

          Code:
          for (var score = 0; score < names.length; score++) {
          should be:

          Code:
          for (var score = 0; score < namesArray.length; score++) {
          since you don't have names declared but namesArray.

          second - you should use your counter:

          Code:
          combinedPointsArray = firstPointsArray[score] + secondPointsArray[score];
          since you currently just add the element 0 for all steps.

          Comment

          Working...