Show text input line by line

How do I display the input parameter like this:

  displayText("you and me");
  expected output:
  ["you and me", "you and", "and me", "you", "and", "me"]

I have tried like the following code, but the results are still wrong. My code is like this now.

let displayText = (txt) => {
   let output= []
   for(let i = 0; i < txt.length; i++) {
       for(j = i + 1; j < txt.length + 1; j++) {
          output.push(txt.slice(i, j))
       }
   }
   return output
}

Hi Shbdn, welcome to the forum.

Right now your code breaks up the string character by character …

First part of the output I got when trying out your code:

0: "y"
​
1: "yo"
​
2: "you"
​
3: "you "
​
4: "you a"
​
5: "you an"
​
6: "you and"
​
7: "you and "
​
8: "you and m"
​
9: "you and me"
​
10: "o"
​
11: "ou"
​
12: "ou "
​
13: "ou a"
​
14: "ou an"
​
15: "ou and"
​
16: "ou and "
​
17: "ou and m"
​
18: "ou and me"
​
19: "u"
​
20: "u "
​
21: "u a"
​
22: "u an"
​
23: "u and"
​
24: "u and "
​
25: "u and m"
​
26: "u and me"

etc. ...

Since you need to handle whole words, you could consider the split() method and regex. This exercise might be a good starting point: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/functional-programming/split-a-string-into-an-array-using-the-split-method

ok, I’ll check, thank you.