Three employees compete for the award. Use a chained if / else if / else to pick the most productive one and print their 15% bonus.
You start with:
let name1 = "John Doe";
let productivity1 = 83;
let salary1 = 135000;
let name2 = "Mary Jane";
let productivity2 = 133;
let salary2 = 335000;
let name3 = "Bob Smith";
let productivity3 = 97;
let salary3 = 210000;
Build:
Mary Jane Bonus: $50250
Rules:
- The winner is whichever of the three employees has the highest productivity.
- If two or more 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.
Declare
let winner_name; let winner_salary; before the if, then use an if / else if / else chain to assign them, so both are still in scope for the prints. Employee 1 wins when its productivity is at least that of both others; if not, employee 2 wins when it is at least employee 3's; otherwise employee 3 wins.
Dividing two whole numbers (
i32) drops the fraction, so winner_salary * 15 / 100 is already rounded down: let bonus = winner_salary * 15 / 100;
{} in the format string is replaced by the value: println!("Bonus: ${}", bonus);
let name1 = "John Doe";
let productivity1 = 83;
let salary1 = 135000;
let name2 = "Mary Jane";
let productivity2 = 133;
let salary2 = 335000;
let name3 = "Bob Smith";
let productivity3 = 97;
let salary3 = 210000;
let winner_name;
let winner_salary;
if productivity1 >= productivity2 && productivity1 >= productivity3 {
winner_name = name1;
winner_salary = salary1;
} else if productivity2 >= productivity3 {
winner_name = name2;
winner_salary = salary2;
} else {
winner_name = name3;
winner_salary = salary3;
}
let bonus = winner_salary * 15 / 100;
println!("{}", winner_name);
println!("Bonus: ${}", bonus);
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
