DEV Community

dev.to staff
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!

Top comments (10)

Collapse
 
nickholmesde profile image
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"
Collapse
 
erezwanderman profile image
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
Collapse
 
yechielk profile image
Yechiel Kalmenson • Edited

Ruby:

def total_salary(base_salary, getting_bonus)
    "$#{getting_bonus ? (base_salary * 10).to_s : base_salary.to_s}"
end
Collapse
 
yechielk profile image
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 :)

Clever on adding a zero!

Collapse
 
nickyoung profile image
Nick Young • Edited

Untested PHP attempt:

function get_salary( $salary, $bonus )  {
    return '$' . ( $bonus ? $salary * 10 : $salary );
}
Collapse
 
kushal-niroula profile image
Kushal Niroula

JS Quick and dirty :)

function salary(salary, bonus) {
    return ${bonus? salary * 10 : salary}`;
}
Collapse
 
vaibhavyadav1998 profile image
Vaibhav Yadav

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)
}
Collapse
 
raucoustortoise profile image
Michael Mather

Python:

def get_salary(salary, bonus):
    return f'${salary * 10**bonus}'
Collapse
 
nickholmesde profile image
Nick Holmes

Obtuse, but fun!

Collapse
 
nickholmesde profile image
Nick Holmes

Clearly, F# doesn't pay well enough.