Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

195
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda