DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #140 - I love you, a little, a lot, passionately ... not at all

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)

Collapse
 
savagepixie profile image
SavagePixie • Edited

JavaScript:

const responses = [
    "not at all",
    "I love you",
    "a little",
    "a lot",
    "passionately",
    "madly",
]

const getLove = n => responses[n % 6]
Collapse
 
wheatup profile image
Hao

Very simple but effective answer!
Just two improvements:

  • (n + 6) % 6 seems a little bit redundant here, n % 6 does the same thing.
  • Flower with only one petal should return not at all, but this returns I love you.
Collapse
 
nickholmesde profile image
Nick Holmes

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.

Thread Thread
 
wheatup profile image
Hao

Ah, I see. Didn't pay attention there, my bad. Thanks for the explanation. πŸ˜€

Collapse
 
savagepixie profile image
SavagePixie • Edited

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.

Collapse
 
wheatup profile image
Hao

Here's one solution using scss:

Collapse
 
not_jffrydsr profile image
@nobody

πŸ’šAllons y Clojure: πŸ’™

(ns affairs)

(defonce ^:private Love
 "cause affairs on the heart are private and unique"  
  (fn [n]
    (let [petals ["yes" ;; changed from "I love you" for semantics 
                  "a little" 
                  "a lot" 
                  "passionately" 
                  "madly" 
                  "not at all"]]
   (when (> (int n) 0)
    (get petals (mod  (+ (int n) (count petals))
                     (count petals))))
)) 

Collapse
 
erezwanderman profile image
erezwanderman

JS

f = nb_petals => {switch(nb_petals % 6) { case 0: return 'not at all'; case 1: return 'I love you'; case 2: return 'a little'; case 3: return 'a lot'; case 4: return 'passionately'; case 5: return 'madly';}}
Collapse
 
vctr1005 profile image
Victor Lazo Dominguez

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]
}