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 overusedWords = ['really', 'very', 'basically']; let unnecessaryWords = ['extremely', 'literally', 'actually' ]; let storyWords = story.split(' '); //console.log(storyWords.length); let betterWords = storyWords.filter(function(word) { return !unnecessaryWords.includes(word) }); //console.log(betterWords);Q4:
"Hay una serie de palabras llamadas overusedWords . Estas son palabras sobreutilizadas en esta story . Desea que el usuario de su programa sepa cuántas veces ha utilizado estas palabras sobreutilizadas. Hay dos formas de lograrlo. Pruébelo en su primero. Si necesita ayuda, consulte la pista".
Insinuación:
Puede iterar sobre la matriz betterWords tres veces por separado (una para cada una de las palabras en la matriz overusedWords ). Cree una variable que represente el total de veces que aparece esa palabra. Agregue 1 a la variable cada vez que la palabra actual sea la misma que esa palabra.
Puede hacer esto más simple usando una instrucción if y dos declaraciones else if en el bloque de código de función de su iterador. De esa manera, puede recopilar los recuentos de las tres palabras usadas en exceso a la vez.
Sin usar las sugerencias, mi código inicial era
let countOverusedWords = storyWords.filter(function(word) { return overusedWords.includes(word) }); console.log(countOverusedWords.length);Creo que mi código responde a la pregunta, pero ¿debería haber registrado las palabras usadas en exceso en la consola dentro del cuerpo de la función countOverusedWords?
Me han mostrado una forma diferente de responder a la pregunta, pero no me han explicado cómo funciona.
let overusedWordCount = []; overusedWords.forEach(word => { overusedWordCount[word] = 0; }) overusedWords.forEach(overusedWord => { betterWords.forEach(word => { if (word === overusedWord) { overusedWordCount[overusedWord]++; } }); });