Build a Proofreading Tool - Build a Proofreading Tool

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

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

when in doubt please check the user stories

That advice isn’t very helpful, if it was clearly not following the user stories, why would all the other tests pass and only 15 and 16 not? There’s likely some sort of edge case in those test cases that is at best a subtley in the use cases or more likely something that is left ambiguous by the use cases. The bigger problem is that the failed tests offer zero hints about what could be wrong.

my mistake, I read it as a question that was asking something from the user stories

@jacobbschwarz test your function like analyzeTexts([["racecar", "hello", "level", "hello"]], 1) please

This is the output of that test:


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

//[ { repeatedPhrases: [], palindromeBreaks: [ 0, 2 ] } ]

Is my findRepeatedPhrases function supposed to detect that there are two “hello” values?

My apologies I am just so lost. Every other test passes which would lead me to believe my other functions are correct…

yes, you need to detect that “hello” is repeated

I apologize but I am still struggling to pass this lab. I updated my findRepeatedPhrases function to:

function findRepeatedPhrases(words, phraseLength) {
  //stores the first index value of each phrase that repeats
  let repeatedPhrases = [];

  if (phraseLength >= words.length) {
    console.log("Length is longer than words")
    return repeatedPhrases;
  };

  //i tracks the starting index of the primary phrase
  for (let i = 0; i < words.length; i++) {
    let primaryPhrase = i+phraseLength < words.length ? words.slice(i, i+phraseLength) : 0;
    
    //j tracks the starting index of the secondary phrase
    for (let j = i + phraseLength; j < words.length; j++) {
      let secondaryPhrase = j < words.length ? words.slice(j, j+phraseLength) : 0;
      
      //checks if the secondaryPhrase is a repeat of the primaryPhrase
      let isRepeat = (primaryPhrase.length === secondaryPhrase.length && primaryPhrase.every((value, index) => value === secondaryPhrase[index]));
      
      //if it is a repeat and the indeces do not exist, add them to array
      if (isRepeat) {
        if (!repeatedPhrases.includes(i)) {
          repeatedPhrases.push(i)
        };
        if (!repeatedPhrases.includes(j)) {
          repeatedPhrases.push(j)
        };
      };
    };
  };
  
  return repeatedPhrases;
};

The output for the recommended test and my own test are:

analyzeTexts([["racecar", "hello", "level", "hello"]], 1)
//[ { repeatedPhrases: [ 1, 3 ], palindromeBreaks: [ 0, 2 ] } ]

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

analyzeTexts(test, 2);
//[ { repeatedPhrases: [ 0, 3 ], palindromeBreaks: [] },
//  { repeatedPhrases: [ 0, 3 ], palindromeBreaks: [ 1, 4 ] },
//  { repeatedPhrases: [], palindromeBreaks: [] } ]

I know I must be missing something obvious…

now it is not returning repeated sequences that are overlapping like

console.log(findRepeatedPhrases(["ba", "ba", "ba"], 2))

this needs to find the one starting at index 0 and at index 1, it is not finding any