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

190
Vistas
Convert if else statement into function with array

How to create function if else statement with array instead of variables(bill, tip1, tip2)

const bill = 320
const tip1 = 50
const tip2 = 300

if (bill < tip1) {
    console.log(`bill is ${bill} with 10% tip so the total will be ${bill * 0.10 + bill}`);
} else if (bill < tip2) {
    console.log(`bill is ${bill} with 15% tip so the total will be ${bill * 0.15 + bill}`);
} else {
    console.log(`bill is ${bill} with 20% tip so the total will be ${bill * 0.20 + bill}`);
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Did you mean?

function chck(bill, tips, discounts){
  let sm = false;
  for(let i = 0; i < tips.length; i++){
    if(bill < tips[i]){
      console.log(`bill is ${bill} with ${discounts[i]} tip so the total will be ${bill * discounts[i] + bill}`);
      sm = true;
      break;
    }
  }
  if(sm == false){
  console.log(`bill is ${bill} with 40% tip so the total will be ${bill * 0.20 + bill}`);
  }
}
let = bill = 1000;
let tips = [50,300,500,600,800,900];
let discounts = [0.10,0.15,0.20,0.25,0.30,0.35];
chck(bill, tips, discounts);
bill = 320;
chck(bill, tips, discounts);
bill = 290;
chck(bill, tips, discounts);
bill = 30;
chck(bill, tips, discounts);

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