diff --git a/fullstack-cert/js-projects/lab-signal-pattern-detector/script.js b/fullstack-cert/js-projects/lab-signal-pattern-detector/script.js new file mode 100644 index 000000000..9d5050906 --- /dev/null +++ b/fullstack-cert/js-projects/lab-signal-pattern-detector/script.js @@ -0,0 +1,49 @@ +function findRepeatedPhrases(words, phraseLength) { + const repeatedWordsIndecies = [] + + for (let i = 0; i <= words.length - phraseLength; i++) { + for (let j = i + 1; j <= words.length - phraseLength; j++) { + let match = true + + for (let k = 0; k < phraseLength; k++) { + if (words[i + k] !== words[j + k]) { + match = false + break + } + } + + if (match) { + repeatedWordsIndecies.push(i) + break + } + } + } + + return repeatedWordsIndecies +} + +function findPalindromeBreaks(words) { + const palindromesBroken = [] + + for (let i = 0; i < Math.floor(words.length / 2); i++) { + const palindromeIndex = words.length - 1 - i + + if (words[i] !== words[palindromeIndex]) { + palindromesBroken.push(i) + } + } + + return palindromesBroken +} + +function analyseTexts(texts, phraseLength) { + let results = [] + for (let i = 0; i < texts.length; i++) { + const repeatedPhrases = findRepeatedPhrases(texts[i], phraseLength) + const palindromeBreaks = findPalindromeBreaks(texts[i]) + + results.push({ repeatedPhrases, palindromeBreaks }) + } + + return results +} diff --git a/fullstack-cert/js-projects/lab-signal-pattern-detector/user-stories.md b/fullstack-cert/js-projects/lab-signal-pattern-detector/user-stories.md new file mode 100644 index 000000000..2a922e349 --- /dev/null +++ b/fullstack-cert/js-projects/lab-signal-pattern-detector/user-stories.md @@ -0,0 +1,9 @@ +In this lab, you will create a proofreading tool, a tool that checks text for repeated phrases and palindrome breaks. + +**Objective**: Fulfill the user stories below and get all the tests to pass to complete the lab. + +**User Stories**: + +1. You should implement `findRepeatedPhrases(words, phraseLength)` - given an array of words and a phrase length, find the starting indices of any repeated word sequences. Use nested loops with early exit `break` flags. +2. You should implement `findPalindromeBreaks(words)` — given an array of words, find the indices where the word-level palindrome is broken. +3. You should implement `analyseTexts(texts, phraseLength)` — run both checks on each text in an array and return an array of result objects.