Skip to content

Commit

Permalink
and in some original code
Browse files Browse the repository at this point in the history
  • Loading branch information
mishmanners committed Aug 16, 2024
1 parent 0f14534 commit 209be86
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
28 changes: 28 additions & 0 deletions code/reverseSentence.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

27 changes: 27 additions & 0 deletions code/reverseSentenceOriginal.js
Original file line number Diff line number Diff line change
@@ -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("");

}

0 comments on commit 209be86

Please sign in to comment.