Today, your challenge is to help Bob with his coffee shop!
Bob's coffee shop is really busy, so busy in fact that if you do not have the right amount of change, you won't get your coffee as Bob doesn't like waiting for people to sort change. Drinks avaialble are Americano £2.20, Latte £2.30, Flat white £2.40 and Filter £3.50
Write a function that returns, "Here is your "+type+", have a nice day!" if they have the right change or returns "Sorry, exact change only, try again tomorrow!"
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
This challenge comes from victoriabate on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Oldest comments (30)
Well, here it is:
Which outputs:
I like it
I also like it, and decided to rewrite it in c++
What is the name for the
notation? I seem to have managed to avoid seeing this for years!
The {} operator would define a map. In the solution that would be a map of float type keys for string type values. The [ ] is then used to access a particular key.
Awesome, thank you!
You're welcome!
Good approach. What if the function is called like
I did it three different ways, but here's what I ended up with as probably the cleanest:
JS
You can watch me solve it here, plus see the other two solutions (and a bonus discussion about why not to use floats for currency in JS 😂): youtu.be/HtvBlId7tig
A simple Javascript solution using nested ternary operators.
Works fine
Haskell
Python solution
My take at the challenge written in Haskell this time!
Try it online.
I feel it isn't super explicit that people can only order one drink here, so maybe the function should account for that, eh?
In Ruby
This would expect
drinksto be an array of strings. We could also do something were we could accept either a string or an array with something like drinks = [drinks].flatten where we put the result in an array and then flatten it each time. That way we don't have to worry about the type we get (since we aren't checking for it due to this being Ruby).I had actually thought about that myself but chose not to consider it at the moment. I might go back and tweak mine to take a collection of drinks as well.
You are correct. I edited my original comment.
Here's mine: