Build a Proofreading Tool - Build a Proofreading Tool

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 :sweat_smile:

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

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-proofreading-tool/69dd63d1dcdeccb7b39ba4c3.md at main · freeCodeCamp/freeCodeCamp · GitHub

test with the below please

console.log(analyzeTexts([
    "racecar",
    "hello",
    "level",
    "hello"
], 1))

For any repeated word in the words array, just ensure the findPalindromeBreaks function returns the actual index for each instance and not always repeating the index for the first occurance. I stumbled on this for quite sometime but after rectifying that, it finally passed.

The thing is, analyzeText is said to need an array of arrays, not a single array. So i designed the code to work with what’s asked for.
I’ll try redoing it in a way it works with simple array of words, but i don’t know how i will make it work for both the array and 2d array.

my bad, I posted it wrong, it should be an array of arrays. Just try with that sequence of strings, it should find correctly the two repeated phrases at index 1 and 3

console.log(analyzeTexts([[
    "racecar",
    "hello",
    "level",
    "hello"
]], 1))

The console logs:
[ { repeatedPhrases: [ 0 ], palindromeBreaks: [ 1, 3 ] } ]
Which is an array of objects, with repeatedPhrases and palindromeBreaks.
I just left it for now, as i don’t know what "correctly aggregate results for each text" is about…

is this correct? where are the repetitions? racecar is not repeated

I took out the if (result.length < 1) {result.push(i)} that was hardcoding the first index, and now it not pass on 12, 13, 17 and 18, i simply don’t know what to do anymore.

I looked at the github repository for the specific assertion logic expected for correctly aggregating results for each text.

const result = analyzeTexts([["racecar", "hello", "level", "hello"]], 1);
assert.sameDeepOrderedMembers(result[0].repeatedPhrases, [1, 3]);
assert.sameDeepOrderedMembers(result[0].palindromeBreaks, [1, 3]);

For the findRepeatedPhrases function consider using a phraseMap, which maps a phrase with the first index of the phrase, then add the firstIndex and the index of the next time it’s found (if there is any).

please post your updated code if you need more help

Hi @marlonguimaraes.cont,

Try looking up “javascript sliding windows technique used for finding contiguous subarrays or substrings” as an approach to consider for implementing the findRepeatedPhrases function.

Happy coding