Tell us what’s happening: I have been trying to figure out user story three for literal months. Nothing I try does what it is asking. I genuinely don’t even know where to start at this point.
Your code so far
function isPalindrome (word) {
word = word.toLowerCase();
let reverseWord = "";
for (let i = 0; i < word.length; i++) {
reverseWord = word.charAt(i) + reverseWord;
}
if (reverseWord == word) {
return true;
}
return false;
}
function findPalindromeBreaks(wordArr) {
const notPalindrome = [];
for (let i = 0; i < wordArr.length; i++) {
const isItPalindrome = isPalindrome(wordArr[i]);
if (!isItPalindrome) {
notPalindrome.push(i);
}
}
return notPalindrome;
}
function findRepeatedPhrases (words, phraseLength) {
const repeatedWords = [];
if (phraseLength >= words.length) {
return repeatedWords;
}
return repeatedWords;
}
function analyzeTexts (texts, phraseLength) {
let analyzedTexts = [];
for (let i = 0; i < texts.length; i++) {
const palindromeCount = findPalindromeBreaks(texts[i]);
const repeatedWords = findRepeatedPhrases(texts[i], phraseLength);
let textObject = {
palindromeBreaks: palindromeCount,
repeatedPhrases: repeatedWords
}
analyzedTexts.push(textObject);
}
return analyzedTexts;
}
console.log(findRepeatedPhrases(["the", "cat", "sat", "the", "cat"], 2));
You may want to try looking through other forum posts for this topic to get some ideas about how to finish coding the findRepeatPhrases function. I used the sliding window technique when I wrote code for this.
Respectfully this isn’t super helpful. I know that it will always return an empty array. That isn’t the issue. It’s empty because nothing I’ve tried even comes close to working and I just needed a place to start not because I don’t know it won’t be empty.
I worked on it some more and got halfway to where I wanted. I know have an array that holds the right amount of words to compare to the array but I just need to figure out how to compare them.
function isPalindrome (word) {
word = word.toLowerCase();
let reverseWord = "";
for (let i = 0; i < word.length; i++) {
reverseWord = word.charAt(i) + reverseWord;
}
if (reverseWord == word) {
return true;
}
return false;
}
function findPalindromeBreaks(wordArr) {
const notPalindrome = [];
for (let i = 0; i < wordArr.length; i++) {
const isItPalindrome = isPalindrome(wordArr[i]);
if (!isItPalindrome) {
notPalindrome.push(i);
}
}
return notPalindrome;
}
function findRepeatedPhrases (words, phraseLength) {
const repeatedWords = [];
if (phraseLength >= words.length) {
return repeatedWords;
}
let doesRepeat = false;
const windowPhrase = [];
for (let i = 0; i < words.length; i++) {
for (let j = i; j < words.length; j++) {
windowPhrase.push(words[j]);
if (windowPhrase.length == phraseLength) {
break;
}
}
for (let k = 0; k < windowPhrase.length; k++) {
let m = 0;
while (m < phraseLength) {
m++;
let repeatTracker = 0;
if (windowPhrase[k+repeatTracker] === words[i+repeatTracker]) {
repeatTracker++;
doesRepeat = true;
} else {
doesRepeat = false;
}
}
if (doesRepeat) {
repeatedWords.push(i);
}
}
}
return repeatedWords;
}
function analyzeTexts (texts, phraseLength) {
let analyzedTexts = [];
for (let i = 0; i < texts.length; i++) {
const palindromeCount = findPalindromeBreaks(texts[i]);
const repeatedWords = findRepeatedPhrases(texts[i], phraseLength);
let textObject = {
palindromeBreaks: palindromeCount,
repeatedPhrases: repeatedWords
}
analyzedTexts.push(textObject);
}
return analyzedTexts;
}
console.log(findRepeatedPhrases(["the", "cat", "sat", "the", "cat"], 2));
Yes. Can you now put each of those phrases into a data structure you can use to later check for matches?
Edit: I’ll back up a little bit. What data structure do you think you could use to store those phrases considering that each phrase could be one or more words?