Reputation: 73
im new to coding and need help with the function titled filter(). filter is suppose to take every other element from the indexNum array (starting at index 1 and excluding the last index). filter should use the value at those 3 indexes (should be 79, 113, 154) use thes three values as index locations for the betterWords array and filter them out, creating a new array called finishedStatement. please, any help would be apreciated.
ill try to cut out as much code as i can, but i feel its needed to supply context.
let story = 'Last weekend, I took literally the most beautiful bike ride of my life. The route is called "The 9W to Nyack" and it actually stretches all the way from Riverside Park in Manhattan to South Nyack, New Jersey. It\'s really an adventure from beginning to end! It is a 48 mile loop and it basically took me an entire day. I stopped at Riverbank State Park to take some extremely artsy photos. It was a short stop, though, because I had a really long way left to go. After a quick photo op at the very popular Little Red Lighthouse, I began my trek across the George Washington Bridge into New Jersey. The GW is actually very long - 4,760 feet! I was already very tired by the time I got to the other side. An hour later, I reached Greenbrook Nature Sanctuary, an extremely beautiful park along the coast of the Hudson. Something that was very surprising to me was that near the end of the route you actually cross back into New York! At this point, you are very close to the end.';
let storyWords = story.split(' ');
let overusedWords = ['really', 'very', 'basically'];
let unnecessaryWords = ['extremely', 'literally', 'actually' ];
const betterWords = storyWords.filter(word =>{
return !unnecessaryWords.includes(word);
})
var indexNum = [];
var id = 0;
for(i of overusedWords){
id =+ 1;
const correction = () => {
var iP = betterWords.indexOf(i);
while(iP != -1){
indexNum.push(iP);
iP = betterWords.indexOf(i, iP + 1);
}
}
correction();
}
var finishedStatement = [];
const filter = () => {
for (i = 1; i < indexNum.length-1; i = i + 2){
console.log(indexNum[i]);
console.log(betterWords[indexNum[i]]);
finishedStatement = betterWords.filter(word => word != betterWords[indexNum[i]]);
}
};
filter()
so again what i am failing to do, is starting with index 1 of the indexNum array, take every other value, excluding the last index, using these three values (79, 113, 154) and using them as the index numbers for the betterWords array to filter them out. this should create a new array called finishedStatement. therefor, finishedStatement === (betterWords - (index 79, 113, 154)).
PLEASE HELP!
Upvotes: 0
Views: 41
Reputation: 792
As what i understand you want to removed the unnecessary word from the story right ? you want to filtered it by the using the index from the word in the story. i did was to store the filtered index into an array then filter the story by the stored index. correct me please if i am wrong with my understanding to the question.
let story = 'Last weekend, I took literally the most beautiful bike ride of my life. The route is called "The 9W to Nyack" and it actually stretches all the way from Riverside Park in Manhattan to South Nyack, New Jersey. It\'s really an adventure from beginning to end! It is a 48 mile loop and it basically took me an entire day. I stopped at Riverbank State Park to take some extremely artsy photos. It was a short stop, though, because I had a really long way left to go. After a quick photo op at the very popular Little Red Lighthouse, I began my trek across the George Washington Bridge into New Jersey. The GW is actually very long - 4,760 feet! I was already very tired by the time I got to the other side. An hour later, I reached Greenbrook Nature Sanctuary, an extremely beautiful park along the coast of the Hudson. Something that was very surprising to me was that near the end of the route you actually cross back into New York! At this point, you are very close to the end.';
let storyWords = story.split(' ');
let overusedWords = ['really', 'very', 'basically'];
let unnecessaryWords = ['extremely', 'literally', 'actually' ];
const betterWords = storyWords.filter(word =>{
return !unnecessaryWords.includes(word);
})
var indexNum = [];
var id = 0;
for(i of overusedWords){
id =+ 1;
const correction = () => {
var iP = betterWords.indexOf(i);
while(iP != -1){
indexNum.push(iP);
iP = betterWords.indexOf(i, iP + 1);
}
}
correction();
}
var finishedStatement = [];
var fIndex = [];
const filter = () => {
for (i = 1; i < indexNum.length-1; i = i + 2){
fIndex.push(indexNum[i]);
console.log(indexNum[i], betterWords[indexNum[i]]);
finishedStatement = betterWords.filter((word, i) => !fIndex.filter((idx) => idx == i).length > 0);
}
};
filter()
console.log(finishedStatement.join(' '))
Upvotes: 1