Tell us what’s happening:
So I am trying to make a function the takes one argument, a string. And I am trying to make it reverse. So in order to do that I have to convert in into an array. So [h,e,l,l,o]
becomes [o,l,l,e,h]
and comes out as a string 'olleh'
. I managed to convert it into an array. I am trying to test out my method before making a for
loop. I tried unshift(a[b])
and b=a.length
so it would add the last letter to the front. But its says [undefined, h, e, l, l,o]
and I am stuck here, I was gonna use pop()
to delete the last letter and make a loop until the array was reversed. Can someone explain why it is undefined?
Your code so far
function reverseString(str) {
let a = []
a = [...str];
const b = a.length;
a.unshift(a[b]);
return a;
}
console.log(reverseString("hello"));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36
.
Challenge: Reverse a String
Link to the challenge: