Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Tallying Up Score from Arrays

Hello everyone,

I am trying to write a program, where an array is equal to 0,1, and 2 points. The the result that I should be getting is 6. Can anyone point me in the right direction in what I am doing wrong in my function. I appreciate the help.

var zero = [1,2,3,5,7]; // 0
var one = [0,4,6,9]; // 1
var two = 8; //2

var NUMBERS = zero.concat(one,two);


function calculate(){
  var TOTAL = 0; // 6

for(var i = 0; i < NUMBERS.length; i++){

   if(NUMBERS[i] === one[i]){

        TOTAL += 1;

    }else if(NUMBERS[i] == two){
        TOTAL +=2;
    }

  }

}

calculate();

1 Answer

Steven Parker
Steven Parker
243,656 Points

One problem is that this line is incomplete:

for(var i = 0; i 

You need to have an evaluation followed by an increment, plus a closing parenthesis and an opening brace. But assuming the loop was meant to step through each element of NUMBER, you'd still have these issues:

  • you will run out of values in one to compare against in the first if
  • the second if will only match on the last value
  • the total will be 2

I'm not sure why you are expecting a result of 6, unless the missing part of the for does something very unexpected.