Skip to content

Latest commit

 

History

History
19 lines (17 loc) · 357 Bytes

ReverseSentence.md

File metadata and controls

19 lines (17 loc) · 357 Bytes

Recursive Reverse Sentence Solution

JavaScript

function reverseSentence(sentence) {
  if (sentence.length === 0) {
    return "";
  } else {
    var words = sentence.split(" ");
    var word = words.pop();
    if (words.length === 0) {
      return word;
    } else {
      return word + " " + reverseSentence(words.join(" "));
    }
  }
}