Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
106 views
in Technique[技术] by (71.8m points)

javascript - Replace multiple switch case when checking for multiple integer ranges

I would like to simplify this code by removing all those switch case with (which make this look very bulky and I would like to condense this if at all possible), a single function and/or everything that could be used to remove those if recurrencies.

This is my code:

  function hemoglobine() {

hemoglobineInput.addEventListener('input', () => {
  let hemoglobineContainerValue = document.querySelector('#hemoglobina').value;
  switch(true) {
    case (ageGroup === 0 && (hemoglobineContainerValue > 2 && hemoglobineContainerValue <= 6.9)):
      hemoglobineValue = 'AST';
      break;
      
    case (ageGroup === 0 && (hemoglobineContainerValue > 6.9 && hemoglobineContainerValue <= 7.9)):
      hemoglobineValue = 'AS';
      break;

    case (ageGroup === 0 && (hemoglobineContainerValue > 7.9 && hemoglobineContainerValue <= 11.4)):
      hemoglobineValue = 'AM';
      break;

    case (ageGroup === 0 && (hemoglobineContainerValue > 11.4 && hemoglobineContainerValue <= 13.4)):
      hemoglobineValue = 'AL';
      break;

    case (ageGroup === 0 && (hemoglobineContainerValue > 13.4 && hemoglobineContainerValue <= 19.8)):
      hemoglobineValue = 'HN';
      break;

    case (ageGroup === 0 && (hemoglobineContainerValue > 19.8)):
      hemoglobineValue = 'PL';
      break;

    case (ageGroup === 1 && (hemoglobineContainerValue > 2 && hemoglobineContainerValue <= 6.9)):
      hemoglobineValue = 'AST';
      break;
        
    case (ageGroup === 1 && (hemoglobineContainerValue > 6.9 && hemoglobineContainerValue <= 7.9)):
      hemoglobineValue = 'AS';
      break;

    case (ageGroup === 1 && (hemoglobineContainerValue > 7.9 && hemoglobineContainerValue <= 9.9)):
      hemoglobineValue = 'AM';
      break;

    case (ageGroup === 1 && (hemoglobineContainerValue > 9.9 && hemoglobineContainerValue <= 10.9)):
      hemoglobineValue = 'AL';
      break;

    case (ageGroup === 1 && (hemoglobineContainerValue > 10.9 && hemoglobineContainerValue <= 13)):
      hemoglobineValue = 'HN';
      break;

    case (ageGroup === 1 && (hemoglobineContainerValue > 13)):
      hemoglobineValue = 'PL';
      break;

    case (ageGroup === 2 && (hemoglobineContainerValue > 2 && hemoglobineContainerValue <= 6.9)):
      hemoglobineValue = 'AST';
      break;
        
    case (ageGroup === 2 && (hemoglobineContainerValue > 6.9 && hemoglobineContainerValue <= 7.9)):
      hemoglobineValue = 'AS';
      break;

    case (ageGroup === 2 && (hemoglobineContainerValue > 7.9 && hemoglobineContainerValue <= 9.9)):
      hemoglobineValue = 'AM';
      break;

    case (ageGroup === 2 && (hemoglobineContainerValue > 9.9 && hemoglobineContainerValue <= 11.4)):
      hemoglobineValue = 'AL';
      break;

    case (ageGroup === 2 && (hemoglobineContainerValue > 11.4 && hemoglobineContainerValue <= 15)):
      hemoglobineValue = 'HN';
      break;

    case (ageGroup === 2 && (hemoglobineContainerValue > 15)):
      hemoglobineValue = 'PL';
      break;

    case (ageGroup === 3 && (hemoglobineContainerValue > 2 && hemoglobineContainerValue <= 6.9)):
      hemoglobineValue = 'AST';
      break;
        
    case (ageGroup === 3 && (hemoglobineContainerValue > 6.9 && hemoglobineContainerValue <= 7.9)):
      hemoglobineValue = 'AS';
      break;

    case (ageGroup === 3 && (hemoglobineContainerValue > 7.9 && hemoglobineContainerValue <= 10.4)):
      hemoglobineValue = 'AM';
      break;

    case (ageGroup === 3 && (hemoglobineContainerValue > 10.4 && hemoglobineContainerValue <= 12.4)):
      hemoglobineValue = 'AL';
      break;

    case (ageGroup === 3 && (hemoglobineContainerValue > 12.4 && hemoglobineContainerValue <= 16)):
      hemoglobineValue = 'HN';
      break;

    case (ageGroup === 3 && (hemoglobineContainerValue > 16)):
      hemoglobineValue = 'PL';
      break;

    case (ageGroup === 4 && (hemoglobineContainerValue > 2 && hemoglobineContainerValue <= 6.9)):
      hemoglobineValue = 'AST';
      break;
        
    case (ageGroup === 4 && (hemoglobineContainerValue > 6.9 && hemoglobineContainerValue <= 7.9)):
      hemoglobineValue = 'AS';
      break;

    case (ageGroup === 4 && (hemoglobineContainerValue > 7.9 && hemoglobineContainerValue <= 9.9)):
      hemoglobineValue = 'AM';
      break;

    case (ageGroup === 4 && (hemoglobineContainerValue > 9.9 && hemoglobineContainerValue <= 11.9)):
      hemoglobineValue = 'AL';
      break;

    case (ageGroup === 4 && (hemoglobineContainerValue > 11.9 && hemoglobineContainerValue <= 16)):
      hemoglobineValue = 'HN';
      break;

    case (ageGroup === 4 && (hemoglobineContainerValue > 16)):
      hemoglobineValue = 'PL';
      break;

    case (ageGroup === 5 && (hemoglobineContainerValue > 2 && hemoglobineContainerValue <= 6.9)):
      hemoglobineValue = 'AST';
      break;
        
    case (ageGroup === 5 && (hemoglobineContainerValue > 6.9 && hemoglobineContainerValue <= 7.9)):
      hemoglobineValue = 'AS';
      break;

    case (ageGroup === 5 && (hemoglobineContainerValue > 7.9 && hemoglobineContainerValue <= 10.4)):
      hemoglobineValue = 'AM';
      break;

    case (ageGroup === 5 && (hemoglobineContainerValue > 10.4 && hemoglobineContainerValue <= 12.9)):
      hemoglobineValue = 'AL';
      break;

    case (ageGroup === 5 && (hemoglobineContainerValue > 12.9 && hemoglobineContainerValue <= 18)):
      hemoglobineValue = 'HN';
      break;

    case (ageGroup === 5 && (hemoglobineContainerValue > 18)):
      hemoglobineValue = 'PL';
      break;

    default:
      hemoglobineValue = 'NONE';
  }    
});  
}  

I've thought that their might be a way to make a dictionary by assigning key/value objects, but I'm not sure how to do this correctly. All hints will be appreciated!

question from:https://stackoverflow.com/questions/65893102/replace-multiple-switch-case-when-checking-for-multiple-integer-ranges

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you make an array indexed by ageGroup, whose values are objects containing the hemoglobineValue and its associated lower threshold for that age group, you can iterate through the object's entries to .find the first matching entry:

const hValues = [
    // Age group 0:
    {
        AST: 6.9,
        AS: 7.9,
        AM: 11.4,
        AL: 13.4,
        HN: 19.8,
        PL: 100
    },
    // Age group 1:
    {
        AST: 6.9,
        AS: 7.9,
        AM: 9.9,
        AL: 10.9,
        HN: 13,
        PL: 100
    }
    // etc
]
function hemoglobine() {
    hemoglobineInput.addEventListener('input', () => {
        const input = Number(document.querySelector('#hemoglobina').value);
        const entry = Object.entries(hValues[ageGroup])
            .find(([, threshold]) => input <= threshold);
        const hemoglobineContainerValue = entry[0];
    });
}

You could also use an array of arrays instead of an array of objects in hValues to make things more concise, and have a separate array containing the hemoglobineValue strings, but IMO that data structure would be a bit harder to understand at a glance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...