patternjavascriptModerate
Selecting a prize category based on two parameters
Viewed 0 times
prizetwobasedselectingparameterscategory
Problem
I get an object with categories, first category has 2 types. Both types are in the first and second position but sorting is not constant.
I have to return category 1 with
Here is the code:
How can I reduce / beautify this code?
I have to return category 1 with
playLevel === 2 when bonusPlus is true and category 1 with playLevel === 1 otherwise.Here is the code:
const getFirstCategory = (bonusPlus, prizeCategories) => {
if (bonusPlus) {
if (prizeCategories[0].playLevel === 2) {
return [prizeCategories[0]];
}
return [prizeCategories[1]];
}
if (prizeCategories[0].playLevel === 1) {
return [prizeCategories[0]];
}
return [prizeCategories[1]];
};How can I reduce / beautify this code?
Solution
To make @kfx's answer more readable:
const getFirstCategory = (bonusPlus, prizeCategories) => {
const bonusPlusLevel = bonusPlus ? 2 : 1;
if (prizeCategories[0].playLevel === bonusPlusLevel) {
return [prizeCategories[0]];
}
return [prizeCategories[1]];
};Code Snippets
const getFirstCategory = (bonusPlus, prizeCategories) => {
const bonusPlusLevel = bonusPlus ? 2 : 1;
if (prizeCategories[0].playLevel === bonusPlusLevel) {
return [prizeCategories[0]];
}
return [prizeCategories[1]];
};Context
StackExchange Code Review Q#158994, answer score: 12
Revisions (0)
No revisions yet.