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 JavaScript Basics (Retired) Making Decisions with Conditional Statements Introducing Conditional Statements

Gesang Lamu
Gesang Lamu
2,742 Points

Js looping: problem with length, help!

So I am writing this simple method to loop through this array, but my console gives me an error say

"Uncaught TypeError: Cannot read property 'length' of undefined"

var array = ["apple","pear","juice"];

var msgs = function(array){ for(i=0; i < array.length; i++); console.log(array[i]); }

msgs();

I am pretty sure there is nothing wrong with the function, i googled it seem to be some problem with the server but I don't exactly get it. So seeking help here. Many thanks!

  • Gesang

2 Answers

Sean T. Unwin
Sean T. Unwin
28,690 Points

msgs() is defined as taking an argument but none are passed when you call it in the last line.

Also, you have forgotten the curly brackets for the for loop.

The code should look similar to this:

var array = ["apple","pear","juice"];

var msgs = function(array){ 
  for(var i=0; i < array.length; i++) {
    console.log(array[i]); 
  }
}

msgs(array);

You were almost there; just needed a couple of tweaks. :)

I'm wondering why is there an "undefined" at the end of the loop when running the code in the console ?

Stone Preston
Stone Preston
42,016 Points

doesnt i need to be declared as a var as well?

var msgs = function(array){ 
  for(var i=0; i < array.length; i++) {
    console.log(array[i]); 
  }
}
Gesang Lamu
Gesang Lamu
2,742 Points

so here is my site http://myhundredwebsites.herokuapp.com/fortunecookies I am trying to make this happen with less code, image is problem is something with Rails I think but ignore it for now, when you click it still works.

var array = ["you are lucky","you are pretty lucky","you are very lucky"];

var msgs = function(array){ for(i=0; i < array.length; i++){ console.log(array[i]); } } msgs(array);

$(document).ready(function(){ $(".pinkfc,.fancyfc,.greenfc").click(function(){ $(this).fadeOut(2000); $(this).text(msgs(array));/ this is where the problem is I am taking a a wild guess but doesnt work $(this).css({'color':'#0000ff', 'font-size':'300%'});

}); });

.pinkfc,.fancyfc,.greenfc are the images wrapped in a class

Sean T. Unwin
Sean T. Unwin
28,690 Points

You`re right, Stone Preston. I edited my comment.

Michael Alaev
Michael Alaev
5,415 Points

You should add "Var" before your "i" variable that you are looping with.

Mike.

Gesang Lamu
Gesang Lamu
2,742 Points

oh you are right, like this

var msgs = function(array){ for(var i=0; i < array.length; i++){ console.log(array[i]); } }