본문 바로가기
Java Script/JS basic

JS] 99 bottles of beer 퀴즈

by CodeMia 2021. 8. 15.

99 Bottles of Beer 

 

아래와 같은 노래 가사 만들기 

 

99 bottles of beer on the wall, 99 bottles of beer. 

Take one down and pass it around, 98 bottles of beer on the wall. 

 

98 bottles of beer on the wall, 98 bottles of beer.

Take one down and pass it around, 97 bottles of beer on the wall. 

:

:

1 bottle of beer on the wall, 1 bottle of beer.

Take one down and pass it around, no more bottles of beer on the wall. 

 

No more bottles of beer on the wall, no more bottles of beer.

Go to the store and buy some more, 99 bottles of beer on the wall. 

 

 

 


 

내가 한 코딩 

 

 

....

 

 

다른 사람이 한 코딩 

케이스 1

function cheers(){
    var count = 100;
    while(count>2) {
       count--;
       console.log(count + " bottles of beer on the wall, " + count + " bottles of beer. Take one down and pass it around, " + (count-1) + " bottles of beer.");
    }
 
    count[1] = console.log("1 bottle of beer on the wall, 1 bottle of beer. Take one down and pass it around, 1 bottle of beer.");
    count[0] = console.log("No more bottles of beer on the wall, no more bottles of beer. Go to the store and buy some more, 99 bottles of beer. Cheers :-)!");     
}
 
cheers();

 

 

 

 

케이스 2

  1. function song99Bottles(){
  2. var bottles = 99;
  3. while(bottles>0){
  4. console.log(bottles + " bottles of beer on the wall, " + bottles + " bottles of beer. Take one down and pass it around, " + --bottles + " bottles of beer on the wall.");
  5. }

 

--bottles로 따로 빼는 것 없이 문장 안에 들어가서 신선함

1개, 0개 일 때 안되어 있어 아쉬움

 

 

 

케이스 3

  1. var count = 99;
  2. function beer() {
  3. while (count >= 2) {
  4. console.log(count + " bottles of beer on the wall, " + count + " bottles of beer. Take one down pass it around, " + (count-1) + " bottles of beer on the wall.")
  5. count--;
  6. }
  7.  
  8. if (count === 1) {
  9. console.log(count + " bottle of beer on the wall, " + count + " bottle of beer. Take one down pass it around, no bottles of beer on the wall.")
  10. count--;
  11. }
  12. if (count === 0) {
  13. console.log("No more bottles of beer on the wall, no more bottles of beer. Go to the store and buy some more, 99 bottles of beer on the wall.")
  14. }
  15. }
  16.  
  17. beer();

 

내 경우는 0보다 큰 수로 했는데, 이 사람은 2보다 큰 수로 해서 바로 스트링이 나올 수 있게 했음 

 

 

 

 

 

'Java Script > JS basic' 카테고리의 다른 글

JS] 웹사이트에 자바스크립트 추가하는 법  (0) 2021.08.17
JS] 피보나치 퀴즈  (0) 2021.08.16
JS] While Loops, For Loops  (0) 2021.08.15
JS] 누가 점심낼까? 퀴즈  (0) 2021.08.15
JS] FizzBuzz 퀴즈  (0) 2021.08.14

댓글