I would start by leaving the unnecessary condition brackets to make the program flow more obvious :
const getFirstCategory = (bonusPlus, prizeCategories) => {
if (bonusPlus) {
if (prizeCategories[0].playLevel === 2) {
return [prizeCategories[0]];
} else {
return [prizeCategories[1]];
}
} else {
if (prizeCategories[0].playLevel === 1) {
return [prizeCategories[0]];
} else {
return [prizeCategories[1]];
}
}
};
Then use a variable for the result :
const getFirstCategory = (bonusPlus, prizeCategories) => {
let firstCategory;
if (bonusPlus) {
if (prizeCategories[0].playLevel === 2) {
firstCategory = [prizeCategories[0]];
} else {
firstCategory = [prizeCategories[1]];
}
} else {
if (prizeCategories[0].playLevel === 1) {
firstCategory = [prizeCategories[0]];
} else {
firstCategory = [prizeCategories[1]];
}
}
return firstCategory;
};
Then mutualise the use of [prizeCategories[i]]
:
const getFirstCategory = (bonusPlus, prizeCategories) => {
let index;
if (bonusPlus) {
if (prizeCategories[0].playLevel === 2) {
index = 0;
} else {
index = 1;
}
} else {
if (prizeCategories[0].playLevel === 1) {
index = 0;
} else {
index = 1;
}
}
return [prizeCategories[index]];
};
Then mutualise the index double initialisation :
const getFirstCategory = (bonusPlus, prizeCategories) => {
let lvlToCheck;
if(bonusPlus) {
lvlToCheck = 2;
} else {
lvlToCheck = 1;
}
let index;
if (prizeCategories[0].playLevel === lvlToCheck) {
index = 0;
} else {
index = 1;
}
return [prizeCategories[index]];
};
You can then make the code shorter (but not necessaraly more readable) with a ternary operator :
const getFirstCategory = (bonusPlus, prizeCategories) => {
const lvlToCheck = bonusPlus ? 2 : 1;
let index;
if (prizeCategories[0].playLevel === lvlToCheck) {
index = 0;
} else {
index = 1;
}
return [prizeCategories[index]];
};
Or even two :
const getFirstCategory = (bonusPlus, prizeCategories) => {
const lvlToCheck = bonusPlus ? 2 : 1;
let index = (prizeCategories[0].playLevel === lvlToCheck) ? 0 : 1;
return [prizeCategories[index]];
};
You can shorter the code even more but I would stop there (if not before).
playLevel === (bonusPlus ? 2 :1)
? \$\endgroup\$