Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

189
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!