@sashakotov2

Не работает код JS. В чем дело?

Не могу понять, в чем дело. Хелп!

<!DOCTYPE html>
<html>
  <head>
    <title>1</title>
  </head>
  <body onload="do_game()">
  <script>

var target;
var guess_input_text;
var guess_input;
var finished = false;
var guesses = 0;
var colors=["Tomato", "Violet", "Snow","Salmon", "Purple", "Navy","OliveDrab", "Lime", "LightGray","HotPink", "Gold", "Gainsboro"];
var alphacolors = colors.sort();


function do_game(){
		 var random_number = Math.random() *(alphacolors.length-1);
		 var random_number_integer = Math.floor(random_number);
		 target = random_number_integer + 1;
		 while (!finished) {
		 	   guess_input = prompt("I am thinking of a color \n\n" +
			   "What is the color, huh? ");
			   guesses += 1;
			   finished = check_guess();
			   }
}
function check_guess() {
		 if (!alphacolors.indexOf(guess_input)) {
		 	alert("Sorry, I don't recognize your color \n\n" +
			"Please try again");
			return false;
}
 		 if (guess_input > target) {
         	alert("Sorry! Your guess is not correct. n\n\"+"But I can help you: n\"+"your color is alphabetically higher than mine n\n\"
         	+"Please try again");
         	return false;
}
 	     if (guess_input < target) {
		    alert("Sorry! Your guess is not correct. n\n\"+"But I can help you: n\"+"your color is alphabetically lower than mine n\n\"
			+"Please try again");
			return false;

}
 		 alert("You got it! The color was " + target +
		 ".\n\n It took you " + guesses +
		 " guesses to get the right color");
		 myBody = document.getElementsByTagName("body")[0];
		 myBody.style.background=target;
		 return true;
}
</script>
</body>
</html>
  • Вопрос задан
  • 197 просмотров
Решения вопроса 1
sakrab
@sakrab
Не будь побежден злом, но побеждай зло добром...
Лови

<!DOCTYPE html>
<html>
  <head>
    <title>1</title>
  </head>
  <body onload="do_game()">
  <script>

var target;
var guess_input_text;
var guess_input;
var finished = false;
var guesses = 0;
var colors=["Tomato", "Violet", "Snow","Salmon", "Purple", "Navy","OliveDrab", "Lime", "LightGray","HotPink", "Gold", "Gainsboro"];
var alphacolors = colors.sort();


function do_game(){
     var random_number = Math.random() *(alphacolors.length-1);
     var random_number_integer = Math.floor(random_number);
     target = random_number_integer + 1;
     while (!finished) {
     	   guess_input = prompt("I am thinking of a color \n\n" +
         "What is the color, huh? ");
         guesses += 1;
         finished = check_guess();
         }
}
function check_guess() {
     if (!alphacolors.indexOf(guess_input)) {
     	alert("Sorry, I don't recognize your color \n\n" +
      "Please try again");
      return false;
}
 		 if (guess_input > target) {
         	alert("Sorry! Your guess is not correct. \n\n" + "But I can help you: \n" + "your color is alphabetically higher than mine \n\n" + "Please try again");
         	return false;
}
 	     if (guess_input < target) {
        alert("Sorry! Your guess is not correct. \n\n"+"But I can help you: \n"+"your color is alphabetically lower than mine \n\n"
      +"Please try again");
      return false;

}
 		 alert("You got it! The color was " + target +
     ".\n\n It took you " + guesses +
     " guesses to get the right color");
     myBody = document.getElementsByTagName("body")[0];
     myBody.style.background=target;
     return true;
}
</script>
</body>
</html>


Перенос строки не n\ а \n :O
Ответ написан
Пригласить эксперта
Ответы на вопрос 2
oblomov_95
@oblomov_95
Junior Web-developer
if ((guess_input > target) {
alert("Sorry! Your guess is not correct. n\n\"+"But I can help you: n\"+"your color is alphabetically higher than mine n\n\"
+"Please try again");
return false;
}


Я конечно js только неделю учу, но что-то мне подсказывает, что там не должно быть две "(" у if.
Ответ написан
werty1001
@werty1001
undefined
Cразу бросается в глаза:

if (!alphacolors.indexOf(guess_input) {
Нужна еще скобка.

if ((guess_input > target) {
А тут не нужна.
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы