Reverse Words in String

  • n, n

function reverseWordsInString(str) {

  const arr = [];
  let word = [];
  for (let i = 0; i < str.length; i++) {
    const curr = str[i];
    if (curr !== ' ') {
      word.push(curr);
      continue;
    }
    
    arr.unshift(...word);
    arr.unshift(curr)
    word = [];
  }

  if (word.length > 0) 
    arr.unshift(...word);

  return arr.join('');
}

// Do not edit the line below.
exports.reverseWordsInString = reverseWordsInString;

Last updated