Problem iterating through array

Hi everyone,

I am trying to write a vowel count function, but I don’t understand what is being logged to the console when I test it (several lines with the last line being the correct number). I also don’t understand what the grey circle with the white number to the left of the output in VS code means.

The correct number does appear in the last line of the console, but I am not sure what the rest means when I try to test the function in VS code. Ultimately, it is not returning the correct vowel count anyway and I am trying to figure out what is going on if someone can help please?

function getCount(str) {
  let tempArray = str.split("");
  let vowelCount = 0;
  for (let i = 0; i <= tempArray.length; i++) {
    if (
      tempArray[i] == "a" ||
      tempArray[i] == "e" ||
      tempArray[i] == "i" ||
      tempArray[i] == "o" ||
      tempArray[i] == "u"
    ) {
      vowelCount++;
    }

    return vowelCount;
  }
}

Hello:)
To me it is giving the correct answer.
You have to put return or console.log after the for loop not in it.

function getCount(str) {
  let tempArray = str.split("");
  let vowelCount = 0;
  for (let i = 0; i <= tempArray.length; i++) {
    if (
      tempArray[i] == "a" ||
      tempArray[i] == "e" ||
      tempArray[i] == "i" ||
      tempArray[i] == "o" ||
      tempArray[i] == "u"
    ) {
      vowelCount++;
    }

    
  }
  console.log(vowelCount);
}
getCount("hellobellotello")

I called it and get the number 6:)

If you ask for return value within the for loop then you will get a value in every iteration. Those numbers are going to show you the actual state but not the final result. As you said, the result is going to be the last one.

thanks so much for your help

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.