diff --git a/code/reverseSentence.js b/code/reverseSentence.js index 342f8ba..7777358 100644 --- a/code/reverseSentence.js +++ b/code/reverseSentence.js @@ -40,3 +40,31 @@ function reverse(sentence) { function reverse(sentence) { return sentence.split(' ').reverse().join(' '); } + +// original code fixed by GitHub Copilot + +function reverseSentence(sentence) { + // lowercase the first letter of the sentence if it's not an "I" + if (sentence.charAt(0) !== "I") { + sentence = sentence.charAt(0).toLowerCase() + sentence.slice(1); + } + + // split the sentence + const sentenceArray = sentence.split(" "); + + // split the punctuation and handle null case + const punctuationArray = sentence.match(/[.,:;!?]/g) || []; + // Remove punctuation from the sentence + sentence = sentence.replace(/[.,:;!?]/g, ""); + + // reverse the sentence array and join it together + const reversedSentenceArray = sentenceArray.reverse(); + + // uppercase the first letter of the reversed sentence + reversedSentenceArray[0] = reversedSentenceArray[0].charAt(0).toUpperCase() + reversedSentenceArray[0].slice(1); + + // join the reversed sentence and add punctuation again + const reversedSentence = reversedSentenceArray.join(" ") + punctuationArray.join(""); + + return reversedSentence; + diff --git a/code/reverseSentenceOriginal.js b/code/reverseSentenceOriginal.js new file mode 100644 index 0000000..db680ad --- /dev/null +++ b/code/reverseSentenceOriginal.js @@ -0,0 +1,27 @@ +// reverse a sentence +// the start of the sentence must be capital +// no other part of the sentence should be capital unless it's a name or place +// the end of the sentence should contain the punctuation marks/s + +function reverseSentence(sentence) { + + // first, let's make the first letter of the sentence lower case if it's not an "I" + if (sentence.charAt(0) !== "I") { + sentence = sentence.charAt(0).toLowerCase() + sentence.slice(1); + } + + // next, let's split the sentence into an array of words + const words = sentence.split(" "); + + // then, let's take out the punctuation marks from the sentence + const punctuation = sentence.match(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g); + // now let's remove the punctuation marks from the sentence + sentence = sentence.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,""); + + // let's reverse the sentence and join it back together + const reversedSentence = words.reverse().join(" "); + + // finally, let's make the first letter of the sentence capital and add the punctuation marks back + return reversedSentence.charAt(0).toUpperCase() + reversedSentence.slice(1) + punctuationArray.join(""); + +}