0

var str = "ahceclwlxo";
for (var i = 1; i <str.length; i+=2){
    console.log(str[i]);
}

in my browser again problem, here it shows correct like "hello" but in my browser it shows "he2Lo", where the problem?i am confused really

  • If you are looking at the results in the console, perhaps what you see is the console telling you it combined the two duplicate messages – JonSG Jul 31 '18 at 21:42

2 Answers2

2

What you're seeing in your console is an output of occurrences for each of the console.logs so the 2l is really just saying there are two occurrences of l being logged.

Example Console Output

enter image description here

Here's your code again, but this time with the code being added to an array, then the array being printed.

var str = "ahceclwlxo";
var result = []
for (var i = 1; i <str.length; i+=2){
  result.push(str[i])
}
console.log(result)

That prints out (5) ["h", "e", "l", "l", "o"] in chrome's console. Indicating that you have an array with a length of 5.

JonSG
  • 10,542
  • 2
  • 25
  • 36
stdlo
  • 55
  • 8
1

Some browsers' console windows are configured to not actually reprint similar outputs repeatedly. Instead they print out the first copy and display the number of repetitions.

Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43