[CodeWars] 8kyu#3 - Get Planet Name By ID
■ Description:
The function is not returning the correct values. Can you figure out why?
getPlanetName(3); // should return 'Earth'
■ 문제
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | function getPlanetName(id){ var name; switch(id){ case 1: name = 'Mercury' case 2: name = 'Venus' case 3: name = 'Earth' case 4: name = 'Mars' case 5: name = 'Jupiter' case 6: name = 'Saturn' case 7: name = 'Uranus' case 8: name = 'Neptune' } return name; } | cs |
■ 해결
:: break; 사용!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | function getPlanetName(id){ var name; switch(id){ case 1: name = 'Mercury'; break; case 2: name = 'Venus'; break; case 3: name = 'Earth'; break; case 4: name = 'Mars'; break; case 5: name = 'Jupiter'; break; case 6: name = 'Saturn'; break; case 7: name = 'Uranus'; break; case 8: name = 'Neptune'; break; } return name; } | cs |
■ Best
:: 뭐.. 문제랑 전혀 다르니까.. 이건 반................................칙?
1 2 3 4 | function getPlanetName(id){ var names = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptun']; return names[id - 1]; } | cs |
'Happly Coding > CodeWars' 카테고리의 다른 글
[CodeWars] 8kyu#5 - Broken Greetings (0) | 2016.05.29 |
---|---|
[CodeWars] 8kyu#4 - Square(n) Sum (0) | 2016.05.28 |
[CodeWars] 8kyu#2 - Return to Sanity (0) | 2016.05.25 |
[CodeWars] 8kyu#1 - Basic variable assignment (0) | 2016.05.14 |
실력 향상을 위한 CodeWars (0) | 2016.05.14 |