dev.to staff

Posted on

# Daily Challenge #138 - Do I get a Bonus?

It's bonus time in the big city! The fatcats are rubbing their paws in anticipation... but who is going to make the most money?

Build a function that takes in two arguments (salary, bonus). Salary will be an integer, and bonus a boolean.

If bonus is true, the salary should be multiplied by 10. If bonus is false, the fatcat did not make enough money and must receive only his stated salary.

Return the total figure the individual will receive as a string prefixed with "£" (= `\u00A3`, JS, Go, and Java), "\$" (C#, C++, Ruby, Clojure, Elixir, PHP and Python, Haskell, Lua) or "¥" (Rust).

This challenge comes from A.Partridge on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Nick Holmes

Here's an F# version of the function without obvious args, and really without any multiplication;

``````let payDay = function | true -> sprintf "€%d0" | _ -> sprintf "€%d"
``````

But it works...

``````let pay = payDay true 100
// pay = "€1000"
``````

erezwanderman

QBasic!

``````DECLARE FUNCTION CALC\$ (salary!, bonus!)
CONST False = 0
CONST True = 1
CLS
PRINT CALC(32, True)
PRINT CALC(32, False)

FUNCTION CALC\$(salary, bonus)
CALC\$ = "\$" + LTRIM\$(STR\$(salary * (bonus * 9 + 1)))
END FUNCTION
``````

Yechiel Kalmenson • Edited

Ruby:

``````def total_salary(base_salary, getting_bonus)
"\$#{getting_bonus ? (base_salary * 10).to_s : base_salary.to_s}"
end
``````

Yechiel Kalmenson

lol, haven't used Ruby in a while so I wanted to be sure (though, now that you mention it, implicitly converting to string in interpolation makes sense :)

Nick Young • Edited

Untested PHP attempt:

``````function get_salary( \$salary, \$bonus )  {
return '\$' . ( \$bonus ? \$salary * 10 : \$salary );
}
``````

Kushal Niroula

JS Quick and dirty :)

``````function salary(salary, bonus) {
return `£\${bonus? salary * 10 : salary}`;
}
``````

In Go.

``````
import "fmt"

func finalSalary(salary int, bonus bool) string {
if bonus {
return fmt.Sprintf("\u00A3 %d", salary*10)
}
return fmt.Sprintf("\u00A3 %d", salary)
}
``````

Michael Mather

Python:

``````def get_salary(salary, bonus):
return f'\${salary * 10**bonus}'
``````

Nick Holmes

Obtuse, but fun!

Nick Holmes

Clearly, F# doesn't pay well enough.