2014-09-06 71 views
0

嗨,我已經做了99瓶啤酒歌,但我的代碼似乎並沒有工作我不知道,我無法弄清楚。我真的需要一些幫助,看看我哪裏出錯或有人告訴我哪裏出錯。我想要如果陳述,或者如果有不同的方式來做到這一點,但我需要設置它是如何不知道如何解決它,請幫助。 在此先感謝。「99瓶啤酒在牆上」的代碼不工作純js。如果聲明和while循環

這是我的JavaScript。

var bottles = 99 
bottle = "bottles"; 
text = ""; 

var output = document.getElementById('output'); 
while (bottles > 0) { 

    if (bottles == 1) { 
    bottle = "bottle"; 
    text = "<p>" + bottles + " " + bottle + " of beer on the wall, " + bottles + " " + bottle + " of beer. <br/>"; 
    bottles--; 

     output.innerHTML += text; 
    } 

    if (bottles == 0) { 
    bottles = "no more"; 
    text += "Take one down and pass it around, " + bottles + " bottles of beer on the wall. </p>" 

    output.innerHTML += text; 
} 

output.innerHTML += "<p> No more bottles of beer on the wall, no more bottles of beer. <br/> Go to the store and buy some more, 99 bottles of beer on the wall.</p>" 

這是我的HTML。

<title>My Title</title> 
<script src="javascript.js"></script> 
<div style="text-align: center;"> 
<h1>99 Bottles of Beer Song</h1> 

<div id="output"></div> 

我也有小提琴。 http://jsfiddle.net/Matt1990/1wg16qr5/46/

+0

你能提供的jsfiddle? – 2014-09-06 09:45:58

+0

我只是對不起 – Matthew 2014-09-06 09:46:43

+0

從哪裏開始?.....沒有事件啓動您的應用程序。你的變量聲明中的語法錯誤... – 2014-09-06 09:49:02

回答

2

你的代碼是完全錯誤的。

調試客戶端代碼像JavaScript你可以按CTRL + SHIFT +使用在Chrome/Firefox的開發者工具。它顯示類似於變量聲明部分中的語法錯誤。

如需進一步閱讀,請參閱Chrome DevTools Overview。 Chrome開發工具是遠遠比Firefox/IE /不管(恕我直言)更好

Chrome Developer tools


這裏是工作的代碼。請自行比較。

var bottles = 99, 
 
    bottle = "bottles", 
 
    text = "", 
 
    output = document.getElementById('output'); 
 
while (bottles > 0) { 
 
    if (bottles == 1) { 
 
     bottle = "bottle"; 
 
    } 
 

 
    text += bottles + " "; 
 
    text += bottle + " of beer on the wall, "; 
 
    text += bottles + " " + bottle + " of beer.<br>"; 
 
    
 
    bottles--; 
 
    text += "Take one down and pass it around, "; 
 
    text += + bottles + " bottles of beer on the wall.<hr>" 
 

 
    if (bottles == 0) { 
 
     bottles = "no more"; 
 
    } 
 
    output.innerHTML += text; 
 
    text = ''; 
 
} 
 

 
output.innerHTML += " 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.";
<div style="text-align: center;"> 
 
    <h1>99 Bottles of Beer Song</h1> 
 

 
    <div id="output"></div>


+0

好的,謝謝它是heaplful。 – Matthew 2014-09-06 10:27:28

+0

是的,我認爲你是非常有用的謝謝,我想添加更多的代碼寫在哪裏,我會把它。 – Matthew 2014-09-06 10:33:13

1

有語法錯誤 - 和邏輯錯誤:

var bottles = 99 
bottle = "bottles"; 
text = ""; 

var output = document.getElementById('output'); 
while (bottles >= 0) { 

    if (bottles == 1) { 
     bottle = "bottle"; 
    } 

    // for all > 0 
    text = "<p>" + bottles + " " + bottle + " of beer on the wall, " + bottles + 
          " " + bottle + " of beer. <br/>"; 

    bottles--; 

    if (bottles == 0) { 
     bottles = "no more"; 
     text += "Take one down and pass it around, " + 
        bottles + " bottles of beer on the wall. </p>"; // ; missing 
    } // } missing 

    output.innerHTML += text; // needs to be done always! 

} 

output.innerHTML += "<p> No more bottles of beer on the wall, no more bottles of beer. <br/> Go to the store and buy some more, 99 bottles of beer on the wall.</p>" 

Fiddle here

+2

'瓶 - ;'需要移出'if'塊 – 2014-09-06 09:52:50

+0

@CubedEye錯過了那一個,謝謝。 – 2014-09-06 09:54:08

+0

瓶子在使用後被定義。在'text = ...之前移動'if(bottles == 1){...}' – idmean 2014-09-06 09:55:42

0

你錯過了一些括號

var bottles = 99; //You need to declare each var like this var x = "x"; var y = "y" 
var bottle = "bottles"; // or you can declare it like this var x = "x", y ="y" 
var text = ""; 

var output = document.getElementById('output'); 
while (bottles > 0) { 

    if (bottles == 1) { 
    bottle = "bottle"; 
    }//Missed Parenthesis 
    text = "<p>" + bottles + " " + bottle + " of beer on the wall, " + bottles + " " + bottle + " of beer. <br/>"; 




    if (bottles == 0) { 
    bottles = "no more"; 
    text += "Take one down and pass it around, " + bottles + " bottles of beer on the wall. </p>" 
    } 
    bottles--;//The decreasment is always at the end 
    output.innerHTML += text; 
}//Missed parenthesis 

output.innerHTML += "<p> No more bottles of beer on the wall, no more bottles of beer. <br/> Go to the store and buy some more, 99 bottles of beer on the wall.</p>" 

http://jsfiddle.net/1wg16qr5/49/