Tell us what’s happening:
My code is passing all the tests besides 16, which says i should correctly aggregate results for each text. At the moment i don’t know what i should do, my logs are showing an array of objects with the ‘repeatedPhrases’ and ‘palindromeBreaks’ properties holding each an array with the resulting indexes of the previous functions.
Can someone hint me more or less to what I’m doing wrong?
And don’t mind the silly arrays i created for testing ![]()
Your code so far
const isPalindrome = word => {
let reversedWord = [];
//Loop starts from the end to the beginning pushing every word into a array, forming a reversed word array
for (let i = word.length; i >= 0; i--) {
reversedWord.push(word[i]);
}
//Joins the reversed array, and compares to the original
if (reversedWord.join('').toLowerCase() === word.toLowerCase()) {
return true;
}
return false;
}
const findPalindromeBreaks = words => {
let palindromeBreaks = [];
//Loops through every word of the array, then checks with previous function if is not a palindrome, if not, pushes into array
for (let i = 0; i < words.length; i++) {
if (!isPalindrome(words[i])) {
palindromeBreaks.push(i)
}
}
return palindromeBreaks
}
const findRepeatedPhrases = (words, phraseLength) => {
let masterPhrase = [];
let result = [];
// Catch in case phrase length is longer than array
if (phraseLength >= words.length) {
console.log('Phrase length longer than words array')
return result;
}
// Builds the phrase that will be used to compare subsequent phrases
for (let i = 0; i < phraseLength; i++) {
masterPhrase.push(words[i])
if (result.length < 1) {result.push(i)}
}
// Compare the phrases and store the index
for (let i = masterPhrase.length; i < words.length; i++) {
if (masterPhrase.join(',') === words.slice(i, i + phraseLength).join(',')) {
result.push(i)
}
}
return result
}
const analyzeTexts = (texts, phraseLength) => {
let analyzedArr = []; // The requested arrays of objects
//Loops through every text array and push an object with the previous functions results
for (let text of texts) {
analyzedArr.push({repeatedPhrases: findRepeatedPhrases(text, phraseLength),
palindromeBreaks: findPalindromeBreaks(text)})
}
return analyzedArr
}
const textArs = [['my','arara','is','fun','and','cute','my','arara','is','my','arara','is','here','to','make','me','happy','my', 'arara','is','my','friend'],
['i','level', 'you', 'i', 'level', 'myself', 'i', 'and', 'only', 'i', 'level', 'you'],
['no', 'racecar', 'can', 'beat', 'me', 'no', 'one', 'i', 'said', 'no','racecar', 'can', 'beat', 'me']]
const testArr = ['my','cat','is','fun','and','cute','my','cat','is','my','cat','is','here','to','make','me','happy','my', 'cat','is','my','friend'];
console.log(analyzeTexts(textArs, 2))
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:149.0) Gecko/20100101 Firefox/149.0
Challenge Information:
Build a Proofreading Tool - Build a Proofreading Tool