Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

78
Vistas
How can i count a specific words in a certain string?

i'm new to javascript langguage and still learning some of the data types and i get execise which i can not comprehend, please help me.

The exercise question :

'Love is the best thing in this world. Some found their love and some are still looking for their love.' Count the number of word love in this sentence.

so i tried some of the basic method such as,

  1. match
  2. search
  3. split

and i cannot count the words love and given love words index.

how can i code this to find the answers?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

This should work.

let sentence = "Love is the best thing in this world. 
Some found their love and some are still looking for their love."

console.log(sentence.split("love").length)
about 4 years ago · Juan Pablo Isaza Denunciar

0

This should work

const sentence = 'Love is the best thing in this world. Some found their love and some are still looking for their love.'
const occurrences = sentence.match(/love/gi); // (g is is for match all occurrences, i is for case insensitive)
console.log("number of occurrences of 'love'", occurrences.length)
about 4 years ago · Juan Pablo Isaza Denunciar

0

To make it a little more generic and to show of the use of reduce:

const data = "Love is the best thing in this world. Some found their love and some are still looking for their love"

const dataArray = data.split(' ');

const wordMap = dataArray.reduce((result, word) => {
    result[word.toLowerCase() ]= result[word.toLowerCase() ] ? result[word] + 1 : 1;
    return result
}, {});

console.log(wordMap["love"])
console.log(wordMap["is"])
console.log(wordMap["best"])

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda