DEV Community staff 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 with your suggestions!

Discussion (13)

citizen428 profile image
Michael Kohl • Edited on

Since this exercise was too simplistic I decided to do it in GolfScript


You can call it like this (with 0 being false and 1 being true):

1000 0 bonus

1000 1 bonus


  • {} defines a block of code
  • :bonus assigns this block to a variable named bonus
  • ; pops the block off the stack since it's assigned to a variable anyway

The "function body":

  • \ swaps the top 2 stack arguments, so e.g. 1000 1 becomes 1 1000
  • . duplicates the top of the stack, so 1 1000 becomes 1 1000 1000
  • '0' pushes '0' on the stack, so 1 1000 1000 becomes 1 1000 1000 '0'
  • + concatenates the top two stack argument, automatically coercing to string if necessary. The stack is now 1 1000 10000
  • \ swap the top 2 stack arguments again, so 1 10000 1000
  • if pops 3 elements of the stack, if true (1) outputs the second (10000) otherwise the third (1000)
  • No explicit output is needed since a GolfScript program automatically prints the remaining stack when it finishes

In a more traditional stack-based language like Forth one could define the word bonus simply as :

: bonus if 10 * . else . then  ;

1000 true bonus
nickholmesde profile image
Nick Holmes

Obtuse, but fun!

citizen428 profile image
Michael Kohl

That’s a pretty apt summary of GolfScript. I like it for solving small problems like this, fun mental exercise.

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"
erezwanderman profile image


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

FUNCTION CALC$(salary, bonus)
    CALC$ = "$" + LTRIM$(STR$(salary * (bonus * 9 + 1)))
yechielk profile image
Yechiel Kalmenson • Edited on


def total_salary(base_salary, getting_bonus)
    "$#{getting_bonus ? (base_salary * 10).to_s : base_salary.to_s}"
citizen428 profile image
Michael Kohl • Edited on

String interpolation calls to_s by itself, no need to explicitly do it. :-)

"$#{getting_bonus ? base_salary * 10 : base_salary}"

But since multiplying by 10 is just adding a 0, you can also do this (not recommended though, more clever than useful):

"$#{base_salary}#{0 if getting_bonus}"
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!

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


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

Clearly, F# doesn't pay well enough.

nickyoung profile image
Nick Young • Edited on

Untested PHP attempt:

function get_salary( $salary, $bonus )  {
    return '$' . ( $bonus ? $salary * 10 : $salary );
rehmatfalcon profile image
Kushal Niroula

JS Quick and dirty :)

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