FAQ: Loops - The For Loop

I don’t understand why the output starts with 0 with the following loop:

for (let counter = 0; counter < 4; counter++) { console.log(counter); }

I thought it would start with 1 since we console.log after counter++.

Hello, @katell2020.

Welcome to the forums.

The counter variable will be incremented by 1 after each iteration of the for loop’s code block then the stop condition is checked prior to the next iteration of the code block. In this case, your stop condition is: counter < 4;, so as long as that evaluates to ‘truthy’, the code inside the block will be executed again.

1 Like

I understand that counter=10 will reassign and evaluate to truthy, but why does the following code not work?

for (let counter=5; counter===10; counter++) {
console.log(counter);
}

The condition of the loop is false (5 === 10), so the loop never runs.

The loops run while/as long as the condition is true

Ah! Thank you! I was thinking that it would run UNTIL the stopping condition is true. Thank you for clearing that up!

Initially I forgot to declare my number variable, but my code still logged the numbers from 5 to 10.
Why is this format not correct even though it accomplishes the task?

for (number = 5; number < 11; number++) {
console.log(number);
}

now the variable is going to exists outside its purview:

for (number = 5; number < 11; number++) {
console.log(number);
}

console.log(number)

maybe the exercise validates let keyword is used?

1 Like

I know what I did wrong now, so I went on to the next task.
In your last sentence, do you mean I forgot to include the let keyword in my code?

yes. And the exercise might give an error because of it.

I tried to work on a code that detects an input if it is a “Vocal” or “Consonant” using the for loop method, it actually works fine but what i do understand is that the for loop will run until it goes all over the total index length. Now i would like to change the same “for” loop and use a “while” loop to stop the loop at the time the condition is reached. How can i do that with my code because im struggling with the while loop. this is the original code.

const vocalArr = ['a','A','e','E','i','I','o','O','u','U'];

const consonantArr = ['b','B','c','C','d','D','f','F','g','G','h','H','j','J','k','K','l','L','m','M','n','N','p','P','q','Q','r','R','s','S','t','T','w','W','x','X','y','Y','z','Z']


const letterDetector = userWord => {
  for (let v = 0; v < vocalArr.length ; v++) {
    for (let c = 0; c < consonantArr.length ; c++ ) {
      if (userWord === vocalArr[v]) {
        return 'Vocal';
      } else if (userWord === consonantArr[c]) {
        return 'Consonant';
      } 
    }
  }
};

console.log(letterDetector());

There is a parameter so there needs to be an argument.

We’re not sure that you should have a nested loop. Perhaps you could better describe what it is you expect the function to do.

How does a word equate to a letter?

1 Like

For a better syntax, an iterator can be an option? i’m struggling because we need to verify on 2 different arrays for de condition to determine if it’s “vocal” or “consonant” and also i have a parameter, which may input an argument typed by the user. i thought about .includes().

Is the objective to compare each letter in the word?

1 Like

I think this works better, what do you think?

const vocalArr = ['a','A','e','E','i','I','o','O','u','U'];

const consonantArr = ['b','B','c','C','d','D','f','F','g','G','h','H','j','J','k','K','l','L','m','M','n','N','p','P','q','Q','r','R','s','S','t','T','w','W','x','X','y','Y','z','Z']

const letterDetector = userLetter => {
if (vocalArr.includes(userLetter)) {
  return 'Vocal';
} else if (consonantArr.includes(userLetter)) {
  return 'Consonant';
 } else {
   return 'Error! Only letters are allowed.'
 }
};

console.log(letterDetector('I'));
1 Like