Reverse a String not passing

This is my code so far for this challenge https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/reverse-a-string.

I tried the same block of code below on the MDN site https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split but added

console.log(reverseString("hello"));

to the end and it correctly displayed “olleh”. Any suggestions?

function reverseString(str) {
var newStr= "";
var wordSplit = str.split('');
var wordRev = wordSplit.reverse();

for (i=0; i<str.length; i++) {
	newStr = newStr + wordRev.shift();
  }
  return newStr;
}

Very very very TEENY tiny problem. I opened the console (on most browsers, the F12 key or Developer Tools), and there’s the problem.

Take a look in your for loop - you have

for (i=0; i<str.length; i++){

and, given that the tests are running javascript’s strict mode (I think), that’s giving you an error. It is saying that i is undefined. You’ve assigned it, but you’re missing one TEENY TINY word before the i=0 in that for loop’s opening statement…

@snowmonkey Thank you for seeing that! Totally missed it. Works now.

Easy one to slip past, but the console is your friend. Have you played with it much as yet?

@snowmonkey I have not. Really just took a peek when the actual site was down yesterday after I saw the thread here that reproduced the issue and included screen shots of what was going on from the console I believe. Definitely will going forward.

1 Like