Build a Proofreading Tool Test 12

I’ve gotten every test but this one to pass. It worked for a moment when just run through repeatedPhrases, but when going through analyze texts, it says “arr.filter is not function”.

function isPalindrome (word) {

word = word.toLowerCase();

let reverseWord = “”;

for (let i = 0; i < word.length; i++) {

reverseWord = word.charAt(i) + reverseWord;

}

if (reverseWord == word) {

return true;

}

return false;

}

function findPalindromeBreaks(wordArr) {

const notPalindrome = [];

for (let i = 0; i < wordArr.length; i++) {

const isItPalindrome = isPalindrome(wordArr\[i\]);

if (!isItPalindrome) {

  notPalindrome.push(i);

}

}

return notPalindrome;

}

function findRepeatedPhrases (words, phraseLength) {

const repeatedPhrases = [];

if (phraseLength >= words.length) {

return repeatedPhrases;

};

let findDuplicates = arr => arr.filter((item, index) => arr.indexOf(item) !== index)

const duplicateArray = findDuplicates(words);

const dupeCheck = [];

for (let i = 0; i < words.length; i++) {

if (duplicateArray.includes(words\[i\])) {

  dupeCheck.push(words\[i\]);

}

}

if (dupeCheck.length - 1 >= phraseLength) {

for (let i = 0; i < words.length; i++) {

  if (duplicateArray.includes(words\[i\])) {

    repeatedPhrases.push(i);

  }

}

}

return repeatedPhrases;

}

function analyzeTexts (texts, phraseLength) {

let analyzedTexts = [];

for (let i = 0; i < texts.length; i++) {

const palindromeCount = findPalindromeBreaks(texts\[i\]);

const repeatedWords = findRepeatedPhrases(texts\[i\], phraseLength);



let textObject = {

  palindromeBreaks: palindromeCount,

  repeatedPhrases: repeatedWords

}



analyzedTexts.push(textObject);

}

return analyzedTexts;

}

Link to Lab

Hi @shawdowdancer14,

Please format your code so we can test.


There are two ways you can format your code to make it easier to read and test:

  1. After you copy/paste your code into the editor, select it by dragging your mouse over it then click the (</>) button in the toolbar to automatically wrap your code in backticks. (You can click on the animated demo image below to enlarge it.)

  1. Manually add three backticks on a new line above your code and on a new line after your code. Note that a backtick is NOT the same as a single quote('). To find the backtick key on your keyboard, see this post.

To see changes to your post as you make them, you can click the (M+) button on the toolbar to bring up the rich text editor:


Sorry I was in a rush in I wrote it. Here’s it properly formatted.

function isPalindrome (word) {
  word = word.toLowerCase();
  let reverseWord = "";
  for (let i = 0; i < word.length; i++) {
    reverseWord = word.charAt(i) + reverseWord;
  }
  if (reverseWord == word) {
    return true;
  }
  return false;
}

function findPalindromeBreaks(wordArr) {
  const notPalindrome = [];
  for (let i = 0; i < wordArr.length; i++) {
    const isItPalindrome = isPalindrome(wordArr[i]);
    if (!isItPalindrome) {
      notPalindrome.push(i);
    }
  }
  return notPalindrome;
}

function findRepeatedPhrases (words, phraseLength) {
  const repeatedPhrases = [];
  

  if (phraseLength >= words.length) {
    return repeatedPhrases;
  };

  let findDuplicates = arr => arr.filter((item, index) => arr.indexOf(item) !== index)
  
  const duplicateArray = findDuplicates(words);
  const dupeCheck = [];

  for (let i = 0; i < words.length; i++) {
    if (duplicateArray.includes(words[i])) {
      dupeCheck.push(words[i]);
    }
  }


  if (dupeCheck.length - 1 >= phraseLength) {
    for (let i = 0; i < words.length; i++) {
      if (duplicateArray.includes(words[i])) {
        repeatedPhrases.push(i);
      }
    }
  } 
  return repeatedPhrases;
}

function analyzeTexts (texts, phraseLength) {
  let analyzedTexts = [];

  for (let i = 0; i < texts.length; i++) {
    const palindromeCount = findPalindromeBreaks(texts[i]);
    const repeatedWords = findRepeatedPhrases(texts[i], phraseLength);

    let textObject = {
      palindromeBreaks: palindromeCount,
      repeatedPhrases: repeatedWords
    }

    analyzedTexts.push(textObject);
  }
  return analyzedTexts;
}

Thank you.

It looks like your findRepeatedPhrases function is returning the index of both words in a duplicate phrase rather than just the first index where the phrase occurs.

For example, if you test your function like this:

console.log(findRepeatedPhrases(["the", "cat", "sat", "the", "cat"],2))

your function returns [ 0, 1, 3, 4 ], but [0, 3] is expected.

Hope that helps.