Tell us what’s happening:
For “Build a Proofreading Tool” I must be misunderstanding how to implement the fourth function “analyzeTexts”…I am failing test 16 and 17. Am I not supposed to return each texts’ object as part of a single array? Any help or direction would be appreciated.
Your code so far
function isPalindrome(word) {
let casedWord = word.toLowerCase();
let reversedStr = casedWord.split("").reverse().join("");
return reversedStr === casedWord ? true : false;
};
function findPalindromeBreaks(words) {
let notPalindromeIndices = [];
for (let i = 0; i < words.length; i++) {
if (!isPalindrome(words[i])) {
notPalindromeIndices.push(i);
}
};
return notPalindromeIndices;
};
function findRepeatedPhrases(words, phraseLength) {
let repeatedPhrases = [];
if (phraseLength >= words.length) {
console.log("Length is longer than words")
return repeatedPhrases;
};
//creates phrase starting at index 0 to phraseLength
let phrase = [];
for (let i = 0; i < phraseLength; i++) {
phrase.push(words[i]);
};
//iterates from the next index after initial phrase
for (let j = phraseLength; j < words.length - 1; j++) {
let newPhrase = [];
//creates secondary phrase to check against first phrase
for (let k = 0; k < phraseLength; k++) {
newPhrase.push(words[j+k]);
};
//checks if each value of phrase matches newPhrase
let matches = true;
for (let k = 0; k < phrase.length; k++) {
if (phrase[k] !== newPhrase[k]) {
matches = false;
};
};
//if everything matches and repeatedPhrases doesn't have any values, add the index value of the first phrase, then add the index value of the next matching phrase
if (matches) {
if (repeatedPhrases.length === 0) {
repeatedPhrases.push(0);
};
repeatedPhrases.push(j);
}
}
return repeatedPhrases;
};
function analyzeTexts(texts, phraseLength) {
let analyzed = [];
if (texts.length === 0) {
return analyzed;
};
for (let i = 0; i < texts.length; i++) {
let textObj = {};
textObj.repeatedPhrases = findRepeatedPhrases(texts[i], phraseLength);
let palindromeBreaks = [];
for (let j = 0; j < texts[i].length; j++) {
if (!findPalindromeBreaks(texts[i]).includes(j)) {
palindromeBreaks.push(j)
}
};
textObj.palindromeBreaks = palindromeBreaks;
analyzed.push(textObj);
};
return analyzed;
}
let test = [
["the", "cat", "sat", "the", "cat"],
["the", "racecar", "races", "the", "racecar"],
["every", "ever", "everlasts", "every", "everlasting", "ever"]
];
console.log(analyzeTexts(test, 2));
//[ { repeatedPhrases: [ 0, 3 ], palindromeBreaks: [] },
// { repeatedPhrases: [ 0, 3 ], palindromeBreaks: [ 1, 4 ] },
// { repeatedPhrases: [], palindromeBreaks: [] } ]
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36
Challenge Information:
Build a Proofreading Tool - Build a Proofreading Tool