1

I have an array with object:

 next: [
        {
          max_score: 5,
          outcome: "rest_and_come_back_later"
        },
        {
          max_score: 49,
          outcome: "see_a_doctor"
        },
        {
          outcome: "go_to_emergency_room"
        }
      ]

And a variable that holds a patientScore, let us say that the patientScore is 70. If the score is smaller then 5 it should return the outcome rest_and_come_back_later and if it is then max_score 49 it should return the right outcome. If it higher then 49 it should return the outcome : go_to_emergency_room.

What is the best way to do this in javascript?

Does simple ifelse do the job?, like this:

next.forEach((item) => {
    if(patientScore < item.max_score && patientScore >= item.max_score){
        return console.log("max_score: " + item.max_score)
    }else if(patientScore > item.max_score){ return console.log("max_score: " + item.max_score)}})
2
  • The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific problem you're having in a minimal reproducible example. For more information, please see How to Ask and take the tour. Commented Oct 30, 2018 at 23:23
  • With regards to your if(patientScore < item.max_score && patientScore >= item.max_score): how do you believe that the patientScore can be both less-than item.max_score and greater-than-or-equal-to that same item.max_score? Did you mean to use || (OR) in place of && (AND)? Commented Oct 30, 2018 at 23:32

3 Answers 3

1
  1. You're returning a value undefined return console.log(...) and not only that, but also inside of the handler you're using for the function Array.prototype.forEach which it doesn't make sense.
  2. An alternative is sorting the array and the just make <= comparison in order to find the object with the right max_score.

let next = [{      max_score: 5,      outcome: "rest_and_come_back_later"    },    {      max_score: 49,      outcome: "see_a_doctor"    },    {      outcome: "go_to_emergency_room"    }  ],
    // Sort the array to avoid multiple OR conditions.
    array = next.slice().sort((a, b) => {
      if (!('max_score' in a)) return Number.MAX_SAFE_INTEGER;
      if (!('max_score' in b)) return Number.MIN_SAFE_INTEGER;  
      return a.max_score - b.score;
    }),
    // This function finds the specific 'outcome' just comparing the 
    // current index.
    findDesc = (arr, score) => {
      for (let i = 0; i < arr.length; i++) {
        if (score <= arr[i].max_score) return arr[i].outcome;
      }
      return arr.slice(-1).pop().outcome;
    }

console.log(findDesc(array, 4));
console.log(findDesc(array, 5));
console.log(findDesc(array, 48));
console.log(findDesc(array, 49));
console.log(findDesc(array, 50));
console.log(findDesc(array, 70));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

Comments

0

If I understand you question correctly, then one approach to this problem might be to define a function getOutcome() as shown below.

This method returns the desired outcome, based on input patientScore parameter that you pass it:

var object = {
  next : [
    {
      max_score: 5,
      outcome: "rest_and_come_back_later"
    },
    {
      max_score: 49,
      outcome: "see_a_doctor"
    },
    {
      outcome: "go_to_emergency_room"
    }
  ]
};


function getOutcome (score) {

  return object.next.filter(item => {

    if(score < 5) {
      return (item.max_score <= 5)
    }
    else if(score > 49) {
      return (item.max_score >= 49)
    }
    else {
      return (item.max_score > 5 && item.max_score < 49) || (item.max_score === undefined)
    }

  }).map(item => item.outcome)[0]
}

console.log('patientScore = 70', getOutcome(70) );
console.log('patientScore = 3', getOutcome(3) );
console.log('patientScore = 25', getOutcome(25) );

Comments

0

The easiest way to do this is to define your array with scores in the correct order and then use Array.prototype.find to return score <= item.max_score

const list = [
  {
    max_score: 5, outcome: "rest_and_come_back_later"
  },
  {
    max_score: 49, outcome: "see_a_doctor"
  },
  {
    max_score: Infinity, outcome: "go_to_emergency_room"
  }
];

function test(score) {
  // Here it is:
  const item = list.find(i => score <= i.max_score);
  console.log(item.outcome);
}

const testScores = [1, 5, 12, 50, 100];
testScores.forEach(test);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.