Start learning free

LESSON 185 OF 289 · LOOPING OVER ARRAY INDICES

Employee of the Year V4 in TypeScript: Loop for the Best

Practice Read the full explanation

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 i in 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);

STEP 1 OF 6
    Ctrl + Enter to check

    TYPESCRIPT · LOOPING OVER ARRAY INDICES

    Employee Of The Year V4 in TypeScript, explained

    Problem overview

    Version 4 removes the limit on the number of employees. The data is now three parallel arrays, names, productivities and salaries, and index i in all three describes the same person.

    The rules are unchanged. The winner has the highest productivity, a tie goes to the employee listed first, and the bonus is 15% of the winner's salary, rounded down. For the five employees in the starter code, the highest productivity is 150, which belongs to Alice Chen at index 3. Her salary is 250000, so the output is:

    Alice Chen
    Bonus: $37500

    Approach

    Instead of copying a name and a salary around, the program remembers only a position: bestIndex, the index of the best employee seen so far. One index gives access to the name, the productivity and the salary, because the arrays line up.

    The idea is a running maximum. Assume employee 0 is the best, then visit each other employee in turn. When someone has strictly higher productivity than the current best, the best index moves to them. After the last employee, bestIndex points at the winner. The loop works for any number of employees, from two to thousands, with the same code.

    Tracing the example, bestIndex starts at 0 (productivity 83). Index 1 has 133, which is higher, so bestIndex becomes 1. Index 2 has 97, no change. Index 3 has 150, so bestIndex becomes 3. Index 4 has 120, no change. The loop ends with 3.

    Step by step

    1. Create bestIndex at 0. It is a let of type number, because it changes as better employees appear. Starting at 0 means the first employee is the best until someone beats them.
    2. Write the loop from 1. A for loop with i starting at 1 and running while i < names.length visits every other employee. It starts at 1 because employee 0 is already the best so far, and comparing them with themselves would do nothing.
    3. Compare inside the loop. If productivities[i] is strictly greater than productivities[bestIndex], set bestIndex to i. Strictly greater matters: on a tie, the employee who is already best, and who is listed earlier, keeps the lead.
    4. Compute the bonus after the loop, from salaries[bestIndex], as Math.floor(salaries[bestIndex] * 15 / 100).
    5. Print the name with names[bestIndex].
    6. Print the bonus line by joining "Bonus: $" and bonus with +.

    The solution in TypeScript

    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);
    

    TypeScript details to keep in mind:

    • Array types. string[] is an array of strings and number[] an array of numbers. The annotations are erased when the code is compiled, so at run time these are ordinary JavaScript arrays.
    • Loop variable. let i in the for header is scoped to the loop, and each pass has its own binding. i++ adds one after every pass.
    • Numbers can be fractions. The division by 100 may leave decimals, so Math.floor is still needed.

    Complexity

    Time: O(n). The loop visits each employee once, doing a constant amount of work. The bonus and the printing are constant.

    Space: O(1). The program stores one index and one bonus, regardless of how many employees there are.

    Edge cases and common mistakes

    • Ties. If Alice Chen and Carlos Diaz both have 150, Alice wins, because Carlos's 150 isn't strictly greater than hers. Using >= hands the win to the last employee with the top value: with Carlos Diaz's productivity changed to 150, the output becomes Carlos Diaz and Bonus: $27000.
    • Starting the loop at 0. It gives the same answer, because comparing employee 0 with themselves changes nothing, but it does one wasted pass. The lesson starts at 1 to make the idea clear.
    • Keeping the best productivity instead of the best index. A running maximum of the value tells you how high it is, but not who has it. The index gives the name and the salary too.
    • Mixing up the arrays. The comparison uses productivities; the bonus uses salaries; the name comes from names. All three share bestIndex.
    • The winner is the first employee. If employee 0 is the most productive, the loop never changes bestIndex, and the output is correct.

    Testing

    • The starter data prints Alice Chen and Bonus: $37500.
    • Productivities [150, 120, 150, 10, 5] print John Doe and Bonus: $20250: the later 150 doesn't replace the first.
    • Productivities [10, 20, 30, 5, 5] print Bob Smith and Bonus: $31500.
    • Productivities [83, 133, 97, 150, 150] print Alice Chen and Bonus: $37500.
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free