Actualmente trabajando en un proyecto CodeAcademy JS.

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'];

La pregunta es "Desea que el usuario de su programa sepa cuántas veces ha usado estas palabras en exceso".

Luchando por conseguirlo, una sugerencia era usar una instrucción If Else pero no estaba seguro de cómo hacerlo.

-4
conye9980 27 oct. 2017 a las 14:27

4 respuestas

La mejor respuesta
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.';
var overusedWords = ['really', 'very', 'basically'];

var used = {};
story.split(/[\s,]+/).forEach(item => {
    if (overusedWords.indexOf(item) >= 0) {
        if (!used[item]) {
            used[item] = 1
        }
        else {
         used[item] = used[item]+ 1   
        }
    }
});


console.log(JSON.stringify(used));
0
edkeveked 27 oct. 2017 a las 12:32

Podrías usar expresiones regulares para encontrar las palabras usadas en exceso:

> story.match(/\b(really|very|basically)\b/g).length
8

Esto buscará cualquier cosa que sea really, very o basically y sea una sola palabra (\b es el delimitador de palabras).

Tenga en cuenta que match puede devolver null, por lo que debe verificarlo antes de consultar la longitud.

2
Birne94 27 oct. 2017 a las 11:32

Tienes que usar el método de división de cadenas. También debe reemplazar todas las comas.

var array = story.replace(/,/g , "").split(' ');
1
Andrew Koval 27 oct. 2017 a las 11:31

Por favor, consulte el siguiente código.

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'];
function getCount(){
for(var i=0; i<overusedWords.length;i++){
alert(overusedWords[i]+" word count: "+story.split(overusedWords[i]).length);
}
}
<input type ="button" value="test" onclick="getCount()"/>
1
Venkatesh Konatham 27 oct. 2017 a las 11:34