A whole team is competing. Use a for loop over the arrays to find the most productive employee, then print their 15% bonus.
You start with:
const names: string[] = ["John Doe", "Mary Jane", "Bob Smith", "Alice Chen", "Carlos Diaz"];
const productivities: number[] = [83, 133, 97, 150, 120];
const salaries: number[] = [135000, 335000, 210000, 250000, 180000];
Build:
Alice Chen Bonus: $37500
Rules:
- Index
iin all three arrays refers to the same employee. - The winner is whichever employee has the highest productivity. If two employees share the highest productivity, the one listed first wins.
- The winner's bonus is 15% of their salary, rounded down to a whole number.
- Print the winner's name on the first line, then
"Bonus: $<amount>"on the second.
Start with
let bestIndex: number = 0 and loop over the other indices with for (let i: number = 1; i < names.length; i++). Inside the loop, update the best index only when productivities[i] > productivities[bestIndex]: a strict comparison keeps the first employee on a tie.
Use
Math.floor to round the 15% down to a whole number: const bonus: number = Math.floor(salaries[bestIndex] * 15 / 100);
+ turns the number into text: console.log("Bonus: $" + bonus);
const names: string[] = ["John Doe", "Mary Jane", "Bob Smith", "Alice Chen", "Carlos Diaz"];
const productivities: number[] = [83, 133, 97, 150, 120];
const salaries: number[] = [135000, 335000, 210000, 250000, 180000];
let bestIndex: number = 0;
for (let i: number = 1; i < names.length; i++) {
if (productivities[i] > productivities[bestIndex]) {
bestIndex = i;
}
}
const bonus: number = Math.floor(salaries[bestIndex] * 15 / 100);
console.log(names[bestIndex]);
console.log("Bonus: $" + bonus);
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
