constpay=(amount)=>{constbeverage={2.2:'Americano',2.3:'Latte',2.4:'Flat white',3.5:'Filter'}[amount];returnbeverage?`Here is your ${beverage}, have a nice day!`:'Sorry, exact change only, try again tomorrow!';}
Which outputs:
pay(2.2)"Here is your Americano, have a nice day!"pay(2.3)"Here is your Latte, have a nice day!"pay(2.30)"Here is your Latte, have a nice day!"pay(2.31)"Sorry, exact change only, try again tomorrow!"
(a,b={2.2:'Americano',2.3:'Latte',2.4:'Flat white',3.5:'Filter'}[a])=>b?`Here is your ${b}, have a nice day!`:`Sorry, exact change only, try again tomorrow!`
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.
constautopay=[](floatcash){constautocoffee=std::unordered_map<float,std::string>{{2.2,"Americano"},{2.3,"Latte"},{2.4,"Flat white"},{3.5,"Filter"},}[cash];returncoffee.empty()?"Sorry, exact change only, try again tomorrow!":"Here is your "+coffee+", have a nice day!";};
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Well, here it is:
Which outputs:
I like it
Good approach. What if the function is called like
Here is it smaller :)
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!
I also like it, and decided to rewrite it in c++