I am having hard time to understand the loop in javascript

I am struggling to understand how to use loops in context therefore I do not know where to start to make project. Please someone help me to understand it more easily. The credit Card Checker project in java I could not understand a bit .

1 Like

I think you should break down your loop first so you can decide what you’re making it do. Review the mdn documentation if you need to. The way the for-loop is formatted now is going to throw an error. (Did you mean for (valid1 = valid1.length; valid1 > 0; valid1-- ) ?? If so, remember the condition always goes in the middle)

To contextualize it, see if you can think in words what you want, and how that would fit this in words (before code) for ([ *initialization* ]; [ *condition* ]; [ *final-expression* ])

Let’s say I have 3 students and I want to check that they did their homework.
My condition is that all 3 are checked, I want to go one by one, and I want to start from the first one (0 index). In pseudo-code you might write

for student in totalStudents{ // student++ is implicit
   log(student.hw)
   }

// in javascript it's the familiar:

for (let i=0; i < totalStudents; i++){
   console.log(student[i].hw);
   }

Some examples below that you can play around with:

let valid1 = [1,2,3,4,5]


for (let i = 0; i < 3; i++) {
  console.log(valid1[i]);
}
// 1
// 2
// 3

for (let i = 0; i < valid1.length; i++) {
  console.log(valid1[i]);
}

// 1
// 2
// 3
// 4
// 5
2 Likes

thank you so much for taking time and replying in such a good way. I really got backward and did not open my codecademy for 2 days thinking if I could learn. But I am back because I know one thing I wont do is give up. I should be patient enough.

1 Like

Don’t give up! That’s the best skill (not giving up) to get better at this.

Sometimes I’ll have to deal with a problem for an entire week. But usually I always get the answer I need. At the end of the day you get great skills and can have great projects.

2 Likes

1 whole week for one problem thats really great patience. I need to work on that and as you have suggested rather than watching videos and jumping from one to another I tried burning my in this project. And I think I am grasping something. here in this code what is wrong I tried this after I google and google but something still not working what could it be?

Can you copy and paste the error message?

For me that’s always the root in solving any problem.