Ho to implement the vaxTrail function?

vaxTrail function will take an array of persons which will return another object base on conditions. thanks!

Why don’t you show us what you have so far and then we can give you suggestions?

HI @mizanmahi !

Please write code directly into the forum instead of posting screenshots.
It is easier to work with that way.

When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

ok, thanks for your suggestion!

1 Like
const vaxTrail = (persons) => {
   const result = { A: [], B: [], C: [], D: [] };

   persons.forEach((person) => {
      if (person.age >= 20 && person.age <= 30 && person.temperature < 100) {
         result['A'].push(person);
      } else if (
         person.age >= 31 &&
         person.age <= 40 &&
         person.temperature < 100
      ) {
         result['B'].push(person);
      } else if (
         person.age >= 41 &&
         person.age <= 50 &&
         person.temperature < 100
      ) {
         result['C'].push(person);
      } else if (person.temperature >= 100) {
         result['D'].push(person);
      }
   });

   return result;
};

const result = vaxTrail(persons);
console.log(result);

I did it this way? and for the last bit I need to sort the each array by even ages people. thanks!

Disired output is something like this

{
  A: [
    { name: 'Biplap', age: 22, temperature: 98 }
    { name: 'Sunil', age: 21, temperature: 98 },
  ],
...

where the even aged people will remain at first

Thanks for the code. Can you provide some sample input?

Also, I’m not sure I understand what sorting by “even ages people” means? Can you give a detailed example? Regardless, I think you would use the Array.sort method to do this.

vaxTrail() function will take a persons array which contains the objects of person, each person wil have name, age and temperature and will return an object base on some conditons.

const persons = [
   {
      name: 'Sunil',
      age: 21,
      temperature: 98,
   },
   {
      name: 'Biplap',
      age: 22,
      temperature: 98,
   },
   {
      name: 'Kabir',
      age: 36,
      temperature: 99,
   },
   {
      name: 'Ruhul',
      age: 37,
      temperature: 99,
   },
   {
      name: 'Paul',
      age: 42,
      temperature: 98,
   },
   {
      name: 'Kat',
      age: 41,
      temperature: 98,
   },
   {
      name: 'Nayem',
      age: 50,
      temperature: 100,
   },
   {
      name: 'Sabnaj',
      age: 51,
      temperature: 101,
   },
];

const vaxTrail = (persons) => {
   const result = { A: [], B: [], C: [], D: [] };

   persons.forEach((person) => {
      if (person.age >= 20 && person.age <= 30 && person.temperature < 100) {
         result['A'].push(person);
      } else if (
         person.age >= 31 &&
         person.age <= 40 &&
         person.temperature < 100
      ) {
         result['B'].push(person);
      } else if (
         person.age >= 41 &&
         person.age <= 50 &&
         person.temperature < 100
      ) {
         result['C'].push(person);
      } else if (person.temperature >= 100) {
         result['D'].push(person);
      }
   });

   return result;
};

const result = vaxTrail(persons); 

// output is : 

{
  A: [
    { name: 'Sunil', age: 21, temperature: 98 },
    { name: 'Biplap', age: 22, temperature: 98 }
  ],
  B: [
    { name: 'Kabir', age: 36, temperature: 99 },
    { name: 'Ruhul', age: 37, temperature: 99 }
  ],
  C: [
    { name: 'Paul', age: 42, temperature: 98 },
    { name: 'Kat', age: 41, temperature: 98 }
  ],
  D: [
    { name: 'Nayem', age: 50, temperature: 100 },
    { name: 'Sabnaj', age: 51, temperature: 101 }
  ]
}

// but the outpust should be something like this where in each array in the output object will contain the person where the eveb aged people will reamin at first then the odd aged people. like say in the first array of the output object Biplap will come first age his age is even and then the sunit will come.

For the sorting, if the only thing that matters is whether their age is an even number or an odd number, and the order within the evens and odds isn’t important (assuming you had more than two people in each array) then one way to look at this could be that you want to add even people to the beginning of the array and odd people to the end of the array.

1 Like

Order is important as all the even aged people will come first then the odd aged people.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.