문제

  • 꽃잎 수가 주어지면, 해당 꽃잎의 문구를 반환한다.

Phrases

  1. I love you
  2. a little
  3. a lot
  4. passionately
  5. madly
  6. not at all

풀이 01

function howMuchILoveYou(nPetals) {
  const phrases = [
    'not at all',
    'I love you',
    'a little',
    'a lot',
    'passionately',
    'madly'
  ];

  return phrases[nPetals % phrases.length];
}

howMuchILoveYou(1);  // I love you
howMuchILoveYou(2);  // a little
howMuchILoveYou(3);  // a lot
howMuchILoveYou(4);  // passionately
howMuchILoveYou(5);  // madly
howMuchILoveYou(6);  // not at all
howMuchILoveYou(7);  // I love you
howMuchILoveYou(8);  // a little

풀이 02

function howMuchILoveYou(nPetals) {
  const phrases = {
    0: 'not at all',
    1: 'I love you',
    2: 'a little',
    3: 'a lot',
    4: 'passionately',
    5: 'madly'
  };

  return phrases[nPetals % 6];
}

howMuchILoveYou(1);  // I love you
howMuchILoveYou(2);  // a little
howMuchILoveYou(3);  // a lot
howMuchILoveYou(4);  // passionately
howMuchILoveYou(5);  // madly
howMuchILoveYou(6);  // not at all
howMuchILoveYou(7);  // I love you
howMuchILoveYou(8);  // a little