I love you, a little , a lot, passionately ... not at all

Lee·2022년 6월 27일
0

Algorithm

목록 보기
30/92
post-thumbnail

❓ I love you, a little , a lot, passionately ... not at all

Q. Who remembers back to their 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.

✔ Solution

//#1 my solution
function howMuchILoveYou(nbPetals) {
    // your code
  const arr = [
     "I love you",
     "a little",
     "a lot",
     "passionately",
     "madly",
     "not at all",
   ];
  
  for (let i = 0; i <= nbPetals-1; i += 1) {
    
    if (i === nbPetals-1) {
      return arr[i%6];
    }
  }
}


//#2 other solution
function howMuchILoveYou(nbPetals) {
  let phrase = {    
    0: "not at all",
    1: "I love you",
    2: "a little",
    3: "a lot",
    4: "passionately",
    5: "madly"
  }
  return phrase[nbPetals%6]
}
profile
Lee

0개의 댓글