Remember back to your time in the schoolyard when girls would take a flower and tear its petals, saying each of the following phrases each time a petal was torn:
- I love you
- a little
- a lot
- passionately
- madly
- not at all
When the last petal was torn there were cries of excitement, dreams, surging thoughts, and emotions.
Your goal in this kata is to determine which phrase the girls would say for a flower of a given number of petals, where nb_petals > 0
.
This challenge comes from nbeck 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!
Top comments (9)
JavaScript:
Very simple but effective answer!
Just two improvements:
(n + 6) % 6
seems a little bit redundant here,n % 6
does the same thing.not at all
, but this returnsI love you
.You're right about the (n+6)%6 (its correct, but the +6 does nothing).
However, SavagePixie cleverly rotated the items in the array down one position, to essentially pre-calculate the -1 offset for the first item - so its working correctly.
Ah, I see. Didn't pay attention there, my bad. Thanks for the explanation. π
Thanks for your feedback! I wasn't sure how the operator
β
would work when the result of the division is smaller than one and had no time to test it, so I went for an option I was sure would work. I'll change my solution accordingly.Here's one solution using scss:
πAllons y Clojure: π
JS
JS a little bit traditional. I used the array created by SavagePixie. Thanks
const responses = [
"not at all",
"I love you",
"a little",
"a lot",
"passionately",
"madly",
]
const loveLevel = function(n){
const rand = Math.floor((Math.random() * n) % 6)
return responses[rand]
}