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!
Discussion
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 beingtrue
):Explanation:
{}
defines a block of code:bonus
assigns this block to a variable namedbonus
;
pops the block off the stack since it's assigned to a variable anywayThe "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 1000if
pops 3 elements of the stack, if true (1) outputs the second (10000) otherwise the third (1000)In a more traditional stack-based language like Forth one could define the word
bonus
simply as :Obtuse, but fun!
That’s a pretty apt summary of GolfScript. I like it for solving small problems like this, fun mental exercise.
Here's an F# version of the function without obvious args, and really without any multiplication;
But it works...
QBasic!
Ruby:
String interpolation calls
to_s
by itself, no need to explicitly do it. :-)But since multiplying by 10 is just adding a 0, you can also do this (not recommended though, more clever than useful):
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!
In Go.
Python:
Clearly, F# doesn't pay well enough.
Untested PHP attempt:
JS Quick and dirty :)