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

293
Vistas
How to optimalize for loop to work for everyon?

i have a problem with the code, idk how to optimalize the code to work for every new "person" in object.

customers_data={
    'Ben10': [22, 30, 11, 17, 15, 52, 27, 12],
    'Sameer': [5, 17, 30, 33, 40, 22, 26, 10, 11, 45],
    'Zeeshan': [22, 30, 11, 5, 17, 30, 6, 57] 
    }
var count = 0
for (let e of customers_data.Ben10) { if (e >= 20) count ++}
    if (count >= 5) {console.log("Ben10 has Premium Membership")}
    count = 0
for (let e of customers_data.Sameer) { if (e >= 20) count ++}
    if (count >= 5) {console.log("Sameer has Premium Membership")}
    count = 0
for (let e of customers_data.Zeeshan) { if (e >= 20) count ++}
    if (count >= 5) {console.log("Zeeshan has Premium Membership"); count = 0}
    count = 0

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

0

customers_data={
    'Ben10': [22, 30, 11, 17, 15, 52, 27, 12],
    'Sameer': [5, 17, 30, 33, 40, 22, 26, 10, 11, 45],
    'Zeeshan': [22, 30, 11, 5, 17, 30, 6, 57] 
    }
var count = 0
for(let prop in customers_data)
{
  for (let e of customers_data[prop]) { if (e >= 20) count ++}
      if (count >= 5) {console.log(prop + " has Premium Membership")}
      count = 0
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

Use two loops. Outer to loop name => values, inner to loop values

let customers_data = {
    'Ben10': [22, 30, 11, 17, 15, 52, 27, 12],
    'Sameer': [5, 17, 30, 33, 40, 22, 26, 10, 11, 45],
    'Zeeshan': [22, 30, 11, 5, 17, 30, 6, 57] 
    }

for (let dataKey in customers_data) {
    let count = 0;

    for (let value of customers_data[dataKey]) {
        if (value >= 20) {
            count++;
        }
    }
    
    if (count >= 5) {
        console.log(dataKey + " has Premium Membership");
    }
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can DRY this out a little by using a function into which you pass the data, and the name you want to check, and then return a message based on the result.

const customers_data = {
  Ben10: [22, 30, 11, 17, 15, 52, 27, 12],
  Sameer: [5, 17, 30, 33, 40, 22, 26, 10, 11, 45],
  Zeeshan: [22, 30, 11, 5, 17, 30, 6, 57]
}

// Accept data and a name
function hasMembership(data, name) {

  // `filter` out the numbers that are greater
  //  or equal to 20
  const greaterThan20 = data[name].filter(entry => {
    return entry >= 20;
  });

  // If the length of the filtered array is
  // greater or equal to five, return a positive message
  if (greaterThan20.length >= 5) {
    return `${name} has Premium Membership!`;
  }

  // Otherwise return something else
  return `Boo. No membership for ${name}.`;

}

console.log(hasMembership(customers_data, 'Ben10'));
console.log(hasMembership(customers_data, 'Sameer'));
console.log(hasMembership(customers_data, 'Zeeshan'));

Additional documentation

  • filter

  • Template/string literals

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