LoginSignup
0
0

More than 5 years have passed since last update.

js 花占 好き嫌い無関心

Last updated at Posted at 2017-01-12

お題

女の子がある人を思い浮かべながら、"好き""嫌い"と花びらを一枚一枚千切っていく花占い、その関数をつくる。
・つぶやく言葉 
'I love you',
'a little',
'a lot',
'passionately',
'madly',
'not at all',
・与えられる花びらはnb_petals > 0

function howMuchILoveYou(nbPetals) {
    // write your code
}

出力結果 例

howMuchILoveYou(7)//"I love you"
howMuchILoveYou(3)//"a lot"
howMuchILoveYou(6)//"not at all"

使ったもの

switch文

考え方

引数の余りを求めて場合分けする

コード

function howMuchILoveYou(nbPetals) {
    switch (nbPetals % 6) {
        case 1:
            return 'I love you'
            break
        case 2:
            return 'a little'
            break
        case 3:
            return 'a lot'
            break
        case 4:
            return 'passionately'
            break
        case 5:
            return 'madly'
            break
        case 0:
            return 'not at all'
            break
    }
}

ES6

const howMuchILoveYou=n=>['not at all','I love you','a little','a lot','passionately','madly'][n%6];
const phrases = [
    'I love you',
    'a little',
    'a lot',
    'passionately',
    'madly',
    'not at all',
];

function howMuchILoveYou(n) {
     return phrases[(n - 1) % phrases.length] 
}

他にもコードが浮かんだ方、コメントお待ちしております

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0