Basic Algorithm Scripting: Reversing a string

Tell us what’s happening:
I’m sure that I read the string correctly into an array. But I’m not sure if I read the array correctly in reverse to the string? Did I mess something up or am I approaching this wrongly?

Thanks in advance for the help!

Regards
Ma3_str0

Your code so far


function reverseString(str) {
let newStr = "";
let arr = [];
for(var i = 0;i < str.length;i++){
  arr.push(str[i]);
}
for(let j = arr.length - 1;j > 0;j--){
  newStr += arr[j];
}
return newStr;
}

reverseString("hello");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0.

Challenge: Reverse a String

Link to the challenge:

In the second loop change the test condition to j >= 0

hey,you almost did it
see that you have a condition j>0 but index starts with 0 so it should be j>=0
and one another way you can make you code simpler is by not using arr because you can iterate over string and add it into newStr.
basically no need for arr

1 Like