Using unshift to get desired outcome

Tell us what’s happening:

I got the code to work and just wanted to know is this an efficient way of using the unshift method vs using the reverse method?

Your code so far


function reverseString(str) {
   str.split();
let update = []
for (let i = 0; i < str.length; i++){
  update.unshift(str[i])
  console.log(update)
}
 

return update.join("");
}

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/85.0.4183.121 Safari/537.36.

Challenge: Reverse a String

Link to the challenge:

In theory adding to the beginning of an array is not very effective, because other elements must be shifted also.
Doing for loop in negative direction and pushing to array would be more effective.
Of course reverse would be the most readable.

2 Likes

Thanks for the help @jenovs