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

Oscar Mauricio Lancheros Murillo
Oscar Mauricio Lancheros Murillo
3,608 Points

Hi, why my program is not entering to the loop?

function sumame(arr){
 console.log('Inicio del programa')
 var sum = 0;
 for (i=0; i < arr.lenght; i++){
   console.log("Inicio del ciclo")
   sum = sum + arr[i];
   console.log(`Ahora sum es ${sum}`)
 }
 console.log(`Finalmente sum es ${sum}`)
 return sum;
}

4 Answers

Steven Parker
Steven Parker
230,274 Points

Your problem is caused by a misspelled property name.

You wrote "arr.lenght" instead of "arr.length".

Also, while "sum = sum + arr[i]" is fine, you might like to know that there is a shorthand version that does the same thing: "sum += arr[i]".

Christof Baumgartner
Christof Baumgartner
20,864 Points

Can you put

        console.log(arr.lenght);

before the loop and tell if it shows a valid value. Or post an example array to be sure that it is valid.

Also don't omit the ";"-characters after your statements. They might cause the error as well. So write in the loop:

    console.log("Inicio del ciclo");
   sum = sum + arr[i];
   console.log(`Ahora sum es ${sum}`);
Steven Parker
Steven Parker
230,274 Points

That's because of the spelling — see my answer above. :arrow_heading_up: