Calculating the salary based on salary – JS

// Utregning av ansiennitet
function activeYears(year) {
const currentYear = new Date().getFullYear();
const yearsSince = currentYear - year;
return yearsSince;
}
// Finn lønnstrinn og lønn basert på ansiennitet
function salaryEmployee(year) {
const yearsSince = activeYears(year);
let level;
let salary;

if (yearsSince >= 0 && yearsSince < 2) return { level: 12, salary: 503118 };
if (yearsSince >= 2 && yearsSince < 4) return { level: 14, salary: 514020 };
if (yearsSince >= 4 && yearsSince < 6) return { level: 16, salary: 526214 };
if (yearsSince >= 6 && yearsSince < 8) return { level: 18, salary: 536828 };
if (yearsSince >= 8 && yearsSince < 10) return { level: 20, salary: 549884 };
if (yearsSince >= 10 && yearsSince < 12) return { level: 22, salary: 564086 };
if (yearsSince >= 12 && yearsSince < 14) return { level: 24, salary: 578861 };
if (yearsSince >= 14 && yearsSince < 16) return { level: 29, salary: 622194 };
if (yearsSince >= 16 && yearsSince < 18) return { level: 30, salary: 630515 };
if (yearsSince >= 18 && yearsSince < 20) return { level: 32, salary: 648016 };
if (yearsSince >= 20 && yearsSince < Infinity)
return { level: 34, salary: 666234 };
}

console.log(salaryEmployee(2014));
// Utregning av ansiennitet
function activeYears(year) {
const currentYear = new Date().getFullYear();
const yearsSince = currentYear - year;
return yearsSince;
}
// Finn lønnstrinn og lønn basert på ansiennitet
function salaryEmployee(year) {
const yearsSince = activeYears(year);
let level;
let salary;

if (yearsSince >= 0 && yearsSince < 2) return { level: 12, salary: 503118 };
if (yearsSince >= 2 && yearsSince < 4) return { level: 14, salary: 514020 };
if (yearsSince >= 4 && yearsSince < 6) return { level: 16, salary: 526214 };
if (yearsSince >= 6 && yearsSince < 8) return { level: 18, salary: 536828 };
if (yearsSince >= 8 && yearsSince < 10) return { level: 20, salary: 549884 };
if (yearsSince >= 10 && yearsSince < 12) return { level: 22, salary: 564086 };
if (yearsSince >= 12 && yearsSince < 14) return { level: 24, salary: 578861 };
if (yearsSince >= 14 && yearsSince < 16) return { level: 29, salary: 622194 };
if (yearsSince >= 16 && yearsSince < 18) return { level: 30, salary: 630515 };
if (yearsSince >= 18 && yearsSince < 20) return { level: 32, salary: 648016 };
if (yearsSince >= 20 && yearsSince < Infinity)
return { level: 34, salary: 666234 };
}

console.log(salaryEmployee(2014));
In this calculation, which logs { level: 20, salary: 549884 }, how can you get the amount from salary, 549884 and times it with 38,6%? Basically: salary * 1,368 ... To be clear: how do I draw out the salary amount to use in further calculations ...
6 Replies
MarkBoots
MarkBoots14mo ago
think it would be easier to use a lookup tabel. here an array of objects with the active years, the level and salary. based on the "yearsSince" you can find the row that matches and destruct the level and salary from it
function salaryEmployee(year) {
const yearsSince = activeYears(year);
const salaryScale = [
{ years: [ 0, 2], level: 12, salary: 503118 },
{ years: [ 2, 4], level: 14, salary: 514020 },
{ years: [ 4, 6], level: 16, salary: 526214 },
{ years: [ 6, 8], level: 18, salary: 536828 },
{ years: [ 8, 10], level: 20, salary: 549884 },
{ years: [10, 12], level: 22, salary: 564086 },
{ years: [12, 14], level: 24, salary: 578861 },
{ years: [14, 16], level: 29, salary: 622194 },
{ years: [16, 18], level: 30, salary: 630515 },
{ years: [18, 20], level: 32, salary: 648016 },
{ years: [20, Infinity], level: 34, salary: 666234 },
];
const {level, salary} = salaryScale.find((row) => {
return yearsSince >= row.years[0] && yearsSince < row.years[1];
});

console.log(level, salary); // 20, 549884
return salary * 1.368;
}

console.log(salaryEmployee(2014)); // 752241.312
function salaryEmployee(year) {
const yearsSince = activeYears(year);
const salaryScale = [
{ years: [ 0, 2], level: 12, salary: 503118 },
{ years: [ 2, 4], level: 14, salary: 514020 },
{ years: [ 4, 6], level: 16, salary: 526214 },
{ years: [ 6, 8], level: 18, salary: 536828 },
{ years: [ 8, 10], level: 20, salary: 549884 },
{ years: [10, 12], level: 22, salary: 564086 },
{ years: [12, 14], level: 24, salary: 578861 },
{ years: [14, 16], level: 29, salary: 622194 },
{ years: [16, 18], level: 30, salary: 630515 },
{ years: [18, 20], level: 32, salary: 648016 },
{ years: [20, Infinity], level: 34, salary: 666234 },
];
const {level, salary} = salaryScale.find((row) => {
return yearsSince >= row.years[0] && yearsSince < row.years[1];
});

console.log(level, salary); // 20, 549884
return salary * 1.368;
}

console.log(salaryEmployee(2014)); // 752241.312
Å Marlon G
Å Marlon GOP14mo ago
Damn! This is where I started this morning, but I never figured out the array set up and how to extract the salary. This makes sense in terms of set up, but could you explain what happens in this part yearsSince >= row.years[0] && yearsSince < row.years[1] ? The way I understand it is that you find the row (salaryScale.find ...) based on the indeces of the year array. Is this correct ...? So: 2014 gives a value of 9, and 9 falls in between [8, 10].
MarkBoots
MarkBoots14mo ago
.find() is an array method. Almost the same as filter() but it will only give the first match and indeed the logic is actually the same as you did before.
Å Marlon G
Å Marlon GOP14mo ago
Right, and instead of creating two let variables of level and salary and running if statements on them, you're running the .find() and "populating" level and salary with the values from the first row that matches the indeces of years. Or ... @MarkBoots ? 😅
Choo♚𝕂𝕚𝕟𝕘
To answer your original question, which wasn't about how to optimize your code, you can do something like this:
const salaryData = salaryEmployee(2014);
console.log((salaryData.salary * 1.386).toFixed(2));
const salaryData = salaryEmployee(2014);
console.log((salaryData.salary * 1.386).toFixed(2));
Å Marlon G
Å Marlon GOP14mo ago
Ah. So simply creating a variable ... jeez. I was lost inside of the calculations, and never really thought of that. 🥴
Want results from more Discord servers?
Add your server