Build a Proofreading Tool - Build a Proofreading Tool

Tell us what’s happening:

My code is running fine on all the test cases, but I’m unable to pass test 16 and 17. I have tried everything, but I’m unable to find out the issue. Can someone please help me.

Your code so far

const isPalindrome = word => {
  for(let i=0; i<word.length/2; i++) {
    if(word[i].toLowerCase()!=word[word.length-i-1].toLowerCase()) return false
  }
  return true
}

const findPalindromeBreaks = words => {
  if(!words.length) return [];
  let answer = [];
  for(let i=0; i<words.length; i++) {
    let result = isPalindrome(words[i])
    if(result == false) {
      answer.push(i)
    }
  }
  return answer
}

const findRepeatedPhrases = (words, phraseLength) => {
  if(phraseLength >= words.length) return []
  let repeatedPhrases = []
  let phrase = []
  for(let i=0; i<phraseLength; i++) {
    phrase.push(words[i])
  }

  for(let j=phraseLength; j<words.length-1; j++) {
    let newPhrase = []
    for(let k=0; k<phraseLength; k++) {
      newPhrase.push(words[j+k])
    }
    let matches = true
    for(let k=0; k<phrase.length; k++) {
      if(phrase[k] !== newPhrase[k]) matches = false
    }

    if(matches) {
      if(repeatedPhrases.length === 0) {
        repeatedPhrases.push(0)
      }
      repeatedPhrases.push(j)
    }
  }
  return repeatedPhrases
}

const analyzeTexts = (texts, phraseLength) => {
  if(!texts.length) return []
  let result = []
  for(let i=0; i<texts.length; i++) {
    const palindromeCount = findPalindromeBreaks(texts[i]);
    const repeatedWords = findRepeatedPhrases(texts[i], phraseLength);
    let textObject = {
      repeatedPhrases: repeatedWords,
      palindromeBreaks: palindromeCount
    };
    
    result.push(textObject);
  }
  return result;
}

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

let test = [
  ["the", "cat", "sat", "the", "cat"],
  ["the", "racecar", "races", "the", "racecar"],
  ["every", "ever", "everlasts", "every", "everlasting", "ever"]
];

console.log(analyzeTexts(test, 2));

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 Edg/148.0.0.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

please test your code with

analyzeTexts([
        [
            "racecar",
            "hello",
            "level",
            "hello"
        ]
    ],
    1)

what sould it return? what is it returning?

[ { repeatedPhrases: [], palindromeBreaks: [ 1, 3 ] } ]

The usecase you shared I’ve tested it, and it works fine as per me. The repeated phrases are 0 since the 0th element is not repeating, and the palindromeBreaks shows the indices with no palindrome which is 1 and 3 in this case.

What I assume that the repeated phrases should not only work on the 1st string, rather should see if any of the word(s) are being repeated. So, I guess the output should have been [1, 3].

Please correct me if I’m wrong.

it seems there is a misunderstanding on the repeating phrases

you need to find repeated sequences of words, the 0th index does not need to be involved. In this case the “hello” should be detected as a repeating sequence of length 1