Created
May 13, 2023 18:15
-
-
Save adames/fdd78a1059724b8d414c46c43590b11f to your computer and use it in GitHub Desktop.
Reverse sentence function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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