Back to Freecodecamp

Challenge 32: Reverse Sentence

curriculum/challenges/english/blocks/daily-coding-challenges-javascript/68af0687ef34c76c28ffa54b.md

latest1.3 KB
Original Source

--description--

Given a string of words, return a new string with the words in reverse order. For example, the first word should be at the end of the returned string, and the last word should be at the beginning of the returned string.

  • In the given string, words can be separated by one or more spaces.
  • The returned string should only have one space between words.

--hints--

reverseSentence("world hello") should return "hello world".

js
assert.equal(reverseSentence("world hello"), "hello world");

reverseSentence("push commit git") should return "git commit push".

js
assert.equal(reverseSentence("push commit git"), "git commit push");

reverseSentence("npm install sudo") should return "sudo install npm".

js
assert.equal(reverseSentence("npm  install   apt    sudo"), "sudo apt install npm");

reverseSentence("import default function export") should return "export function default import".

js
assert.equal(reverseSentence("import    default   function  export"), "export function default import");

--seed--

--seed-contents--

js
function reverseSentence(sentence) {

  return sentence;
}

--solutions--

js
function reverseSentence(sentence) {
  return sentence.split(/\s+/).reverse().join(" ");
}