Chrome console gave me the same result when executing these two snippets.
I think that I should use let i = Array.length -1
instead of i = Array.length
right?
function reverse(string){
let newArray = [];
let Array = string.split('');
for(let i = Array.length; i>-1; i--){
newArray.push(Array[i]);
}
return newArray.join('');
}
reverse('racecar');
function reverse(string){
let newArray = [];
let Array = string.split('');
for(let i = Array.length-1; i>-1; i--){
newArray.push(Array[i]);
}
return newArray.join('');
}
reverse('racecar')
because a 3 letter string ends with string[2] so I should be adding in element at [2] instead of [3] right?
but the result is the same cos I was adding in one empty element???
Is my speculation correct?