2014-09-06 1 views
0

Привет, я сделал 99 бутылок пивной песни, но мой код, похоже, работает не уверен, и я не могу понять это. Мне действительно нужна помощь, чтобы увидеть, где я ошибаюсь, или кто-то, чтобы показать мне, где я ошибаюсь. Я хочу, чтобы инструкции if или если есть другой способ сделать это, но мне нужна настройка того, как я не уверен, как это исправить, пожалуйста, помогите. Спасибо заранее."99 бутылка пива на стене" код не работает чистый js. if statement и while loop

Это мой 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? –

+0

Мне просто жаль – Matthew

+0

С чего начать? ..... Нет приложения, запускающего ваше приложение. Синтаксические ошибки в объявлении переменной ... –

ответ

2

Ваш код полон ошибок.

Для отладки кода на стороне клиента, как JavaScript, вы можете использовать инструменты разработчика в Chrome/Firefox, нажав CTRL + SHIFT + я. Он показывает синтаксические ошибки, подобные тому, который указан в части объявления переменных.

Для дальнейшего ознакомления см. Chrome DevTools Overview. Chrome Dev инструменты гораздо лучше, чем 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

Хорошо, спасибо, что это куча. – Matthew

+0

Да, я думаю, что вы очень помогаете, спасибо, и я хочу добавить больше написания кода, где бы я это сказал. – Matthew

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

'bottles -;' нужно вывести из блока 'if' –

+0

@CubedEye пропустил это, спасибо. –

+0

'бутылка' определяется после ее использования. Переместить 'if (bottles == 1) {...}' before 'text = ...' – idmean

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/