Match from an array as a reference
11 February 2022
JavaScript
Web
Development
main.js
function getNumberOfSticks(items) {
const reference = [
{ lvl: "A0", barlvl: 1 },
{ lvl: "A1", barlvl: 1 },
{ lvl: "A2", barlvl: 2 },
{ lvl: "A2+", barlvl: 2 },
{ lvl: "B1", barlvl: 3 },
{ lvl: "B1+", barlvl: 4 },
{ lvl: "B2", barlvl: 5 },
{ lvl: "B2+", barlvl: 5 },
{ lvl: "C1", barlvl: 6 },
{ lvl: "C1+", barlvl: 6 },
{ lvl: "C2", barlvl: 6 },
];
const MAX_LVL = 6;
const result = [];
const sticks = [];
items.forEach((item) => {
reference.find((reference) => {
if (
item === reference.lvl &&
sticks.length <= MAX_LVL &&
sticks.indexOf(reference.barlvl) == -1 &&
result.indexOf(reference.lvl) == -1
) {
result.push(item);
sticks.push(reference.barlvl);
}
});
});
return Math.max.apply(Math, sticks);
}
const sticks = getNumberOfSticks([
"A0",
"A0",
"A2",
"A3",
"B1+",
"A2",
"A1",
"K5",
"B2",
]);
console.log(sticks);