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!
Top comments (30)
Well, here it is:
Which outputs:
I also like it, and decided to rewrite it in c++
Here is it smaller :)
I like it
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
I over engineered this but I felt like having a bit of fun with this one so lets have a Go shall we!?
coffee.go
coffee_test.go
I used this challenge as my "hello world" with Go. At first I tried a map thinking that it might be easy to have float64 type as key. Can you please post a solution using a map? Here's mine:
Hello Cherny!
You can most certainly use floats as keys in a map, you just need to indicate what type the keys are in the map declaration.
For your code above you have:
If you want to use floats as the keys, you simply need to indicate as so:
Then you could so something like this:
That would also enable you to pass in a float as an argument to the func instead of a string as well.
Don't forget to include logic for checking if they have exact change!
If you would still like me to post a solution using a map I would be more than happy to.
Note
In Golang, if you attempt to access a value of a map with a key that doesn't exist, you will get the zero value of the type stored in the map. That may be useful for the exact change logic check in your solution.
If we use your map for example:
would return "", since the key 5.0 doesn't exist and "" is the zero value for a string in Golang.
Tweaked to allow purchasing multiple drinks, inspired by Ben Halpern's solution.
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
drinks
to 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.
Haskell
Python solution
My take at the challenge written in Haskell this time!
Try it online.
A simple Javascript solution using nested ternary operators.
Works fine
My solution in js
Erlang solution with tests.