Skip to content

Instantly share code, notes, and snippets.

@adames
Created May 13, 2023 18:15
Show Gist options
  • Save adames/fdd78a1059724b8d414c46c43590b11f to your computer and use it in GitHub Desktop.
Save adames/fdd78a1059724b8d414c46c43590b11f to your computer and use it in GitHub Desktop.
Reverse sentence function
// Create a method that reverses a sentence, no punctuation
let sentence = "the quick brown fox jumped over the lazy dog"
if (process.argv.length > 2) {
sentence = process.argv.slice(2).join(" ");
}
let reversedSentence= [];
function reverseSentence(sentence) {
let splitSentence = sentence.split(" ");
for (let index = splitSentence.length - 1; index >= 0; index--) {
reversedSentence.push(splitSentence[index]);
}
return reversedSentence.join(" ");
}
console.log(reverseSentence(sentence));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment