DEV Community

loading...

Discussion on: Interview Qs Decoded - # 2

Collapse
htshah profile image
Het Shah

How about this solution?

const reverse = ([...str]) => {
  const len = str.length;
  for(let i=0;i<len/2;i++){
    [str[i],str[len-i]] = [str[len-i],str[i]];
  }

  return str.join('');
}
Collapse
htshah profile image
Het Shah

Or not so optimized but shorter code:

const reverse2 = ([...str]) => 
    str.map((ch,i)=>str[str.length-1-i]).join('');