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

364
Views
What is the best way to write if-then-else for a large data set in JavaScript?

I am trying to write conditional statements for a large data set in JavaScript. The goal is to calculate Calorie (results) intake based on age and lifestyle.

Here is an example:

HTML

    <label>Enter Age (2 to 100)</label>
    <input id="getAge" type="text" placeholder="" />
    <label>Enter 1 for Sedentary, 2 for Moderate, 3 for Active</label>
    <input id="getLifeStyle" type="text" placeholder="" />

JavaScript:

let age, lifestyle, calorie;

age = document.getElementById("getAge").value
lifestyle = document.getElementById("getLifeStyle").value

// two
if (age==2) {
 if (lifestyle==1) {
   calorie=1000
  }
 if (lifestyle==2) {
   calorie=2000
 }
 ....
 // three
 else if (age==3) {
  if (lifestyle==1) {
   calorie=1000
  }
 if (lifestyle==2) {
   calorie=2000
 }....
 // four...and so, until 100

 // Show calorie
 console.log("Calorie: "+calorie)

Here is what I am trying to achieve:

enter image description here

Basically repeat the above several times. I am not an expert in JavaScript. While the above is working fine to get the results, I don't think this may be the most efficient way to write if-then-else. Are there any other efficient options in JavaScript to do this?

about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

A nested object indexed by age, then indexed by lifestyle would work.

const calorieData = {
  2: {
    1: 1000,
    2: 2000,
  },
  3: {
    1: 1000,
    2: 2000,
  }
};

// ...

const calories = calorieData[age][lifestyle];

A DRY-er version would use arrays instead, but it could require slightly more confusing code due to 0-based indicies.

const calorieData = [
  // omit index 0
  ,
  // omit index 1
  ,
  [1000, 2000],
  [1000, 2000],
  // ...
];

// ...

// because lifestyle is 1-indexed
const calories = calorieData[age][lifestyle - 1];
about 4 years ago · Santiago Gelvez Report

0

const LIFE_STYLE = {
  SEDENTARY: '1',
  MODERATE: '2',
  ACTIVE: '3'
}
const data = [
  { age: 2, lifeStyle: LIFE_STYLE.SEDENTARY, calories: 1000 },
  { age: 3, lifeStyle: LIFE_STYLE.SEDENTARY, calories: 1000 }
]

const getCaloryData = (age, lifestyle) => {
  const entry = data.find(entry => entry.age === age && entry.lifeStyle === lifestyle)
  return entry ? entry.calories : undefined;
}

The complexity is higher but the readability is better.

about 4 years ago · Santiago Gelvez Report

0

the best & shortest alternative way for that is using ternary operator 👇

// two
{age == 2 && lifestyle == 1 && calorie = 1000}
{age == 2 && lifestyle == 2 && calorie = 2000}
//three
{age == 3 && lifestyle == 1 && calorie = 1000}
{age == 3 && lifestyle == 2 && calorie = 2000}
// until 100
about 4 years ago · Santiago Gelvez 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!