Build a Proofreading Tool - User Story 3

Tell us what’s happening:

Hi there! I’m having trouble de-structuring problems into smaller steps which I think is preventing me from writing the correct code without looking at what someone else wrote. Even when I do look at others’ code, I still am struggling to understand why they used a specific approach. Does anyone have tips on how to improve this skill? Or how I can get support in this way on the forum. The step/function I’m struggling with for this problem is #3, “findRepeatedPhrases”.

Thanks!! - Sam

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

try first understanding what the problem is, find the “algorithm” without writing any code but use plain human language

if you want feedback about it, share it with us

Hi @snaylspace,

Try looking up “sliding windows technique used for finding contiguous subarrays or substrings”.

Happy coding

DONE: You should define a function named findRepeatedPhrases that takes a words array and a phraseLength number as arguments.

UNCLEAR: It should return an array of all start indices where a sequence of phraseLength consecutive words appears more than once in the array — including the index of the first occurrence.
^ The parts that I don’t understand here are:
[A] For the returned array, if multiple elements in the words array match this criteria, do we need to specify which words array element indices map to which words array elements?
[B] For the returned array, is it meant to be a single array of the indices (ie. It could be [136])? Or is it meant to be multiple arrays, each representing an indice (eg. It could be [1], [3], [6])? My assumption is the first example, though it’s not fully clear to me.
[C] What is meant by phraseLength (ie. Is this referring the number of words in a “words” array element? Or is it perhaps referring to the the number of characters in a “words” array element)? I am leaning towards the first option.

TBC: It should return an empty array if phraseLength is greater than or equal to the length of words. Overlapping sequences should also be counted.
^ I’ll work on this piece once I have a good grip on the prior step. :slight_smile:

Thanks in advance for your help!

I’ll try to answer all your queries with the following example.

Consider the following array:

["the", "cat", "sat", "the", "cat"]

and phraseLength would be 2

I’m only talking about findRepeatedPhrases()

What is phraseLength? It’s basically like a window of length n which will slide over the array.

Now considering the above example,
["the", "cat", "sat", "the", "cat"] and phraseLength = 2

We get a window of length 2 so that means we can only look at 2 elements at a time.
Once we are at index 0

index 0 → [“the”, “cat”] ← phrase: “the cat” - what’s the length of this phrase (how many words)?
Now the window will shift to index 1
index 1 → [“cat”, “sat”] ← phrase: “cat sat”
and then continue the same process
index 2 → [“sat”, “the”] ← phrase: “sat the”
index 3 → [“the”, “cat”] ← phrase: “the cat”

Now we can’t go to index 4 as there is no element after it and hence we won’t be able to make a window of length 2

Now if you notice the above process, you can clearly spot the phrase “the cat” appears twice, once at index 0 and once at index 3 so now all you need to return these values in form of an array.
Expected output would be [0,3]

Now to even understand the above better, I’ll give you a question, you try to solve it by referencing the above.

["a", "b", "a", "b", "a"] and phraseLength = 3

If you are unable to do it, you can ask over here.

Thank you @imsomilg ! I think the answer to your example would be [0, 2].

If that’s the case, is it safe to assume that input for the “words” array would always be individual words that make up the array’s elements, rather than individual phrases making up the array’s elements? This might be one of the areas that I was mistaken, as I initially thought that the words array was an array of phrases.

you can consider each item inside the array as a word yes

Thanks everyone! With this insight. I was able to solve for this function & understand the code required to write it. :slight_smile:

I’m moving onto this problem now…

“You should define a function named analyzeTexts that takes a texts array and a phraseLength number as arguments. It should process each element of texts (each an array of words) and return an array of objects, each with repeatedPhrases and palindromeBreaks properties. It should return an empty array if texts is empty.”

For the texts array, would an example input be:

let texts = [["one", "two", "three"] , ["four", "five", "six"]

Congratulations on solving the previous problem and now let’s move to the next.
That’s correct, that’s how our texts array would be. You are on to the right path!