loading...

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

thepracticaldev profile image dev.to staff ・1 min read

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!

Posted on by:

thepracticaldev profile

dev.to staff

@thepracticaldev

The hardworking team behind dev.to ❤️

Discussion

pic
Editor guide
 

JavaScript:

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

const getLove = n => responses[n % 6]
 

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.
 

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: 💙

(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))))
)) 

 

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';}}
 

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