Je suis nouveau dans le codage et j'ai besoin d'aide avec la fonction intitulée filter (). Le filtre est supposé prendre tous les autres éléments du tableau indexNum (en commençant à l'index 1 et en excluant le dernier index). Le filtre doit utiliser la valeur de ces 3 index (doit être 79, 113, 154) utiliser ces trois valeurs comme emplacements d'index pour le tableau betterWords et les filtrer, créant un nouveau tableau appelé finishStatement. s'il vous plaît, toute aide serait appréciée.

J'essaierai de couper autant de code que possible, mais je pense que c'est nécessaire pour fournir le contexte.

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()

donc encore une fois ce que je ne fais pas, commence par l'index 1 du tableau indexNum, prend toutes les autres valeurs, à l'exclusion du dernier index, en utilisant ces trois valeurs (79, 113, 154) et en les utilisant comme numéros d'index pour les meilleurs mots tableau pour les filtrer. cela devrait créer un nouveau tableau appelé finishStatement. à cet effet, doneStatement === (betterWords - (index 79, 113, 154)).

SVP AIDEZ!

0
Joe Misheski 4 nov. 2019 à 05:16

1 réponse

Si je comprends bien, vous voulez supprimer le mot inutile de l'histoire, n'est-ce pas? vous voulez le filtrer en utilisant l'index du mot dans l'histoire. J'ai fait était de stocker l'index filtré dans un tableau, puis de filtrer l'histoire par l'index stocké. corrigez-moi s'il vous plaît si je me trompe dans ma compréhension de la 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(' '))
0
HuntsMan 4 nov. 2019 à 02:39