I have a piece of code that looks up sub-sentences on Wikipedia if it does not find the full one. My understanding of array slice was, that a negative n argument would start from the end instead of the begging of the array. So I thought I could do some kind of ZigZag iteration on it. But I was wrong. Can someone point me into the right direction?
So I did this:
function splitwords (words, i=0) {
const parts = words.split(` `);
const wordcount = parts.length;
let index = i%((wordcount-1)*2)-(wordcount-1); //zigzag
let par = parts.slice(index);
return par;
}
for (let = 0; i < 10, i++) {
splitwords('Fresh Prince of Bel Air', i)
}
My goal would be to this:
Input:
Fresh Prince of Bel Air
Output:
Prince of Bel Air
of Bel Air
Bel Air
Fresh Prince of Bel
Fresh Prince of
Fresh Prince
Fresh
Prince
of
Bel
Air
PS: What is the misconception I have about .slice()
?