Meira D.
Meira D.

Reputation: 1

Random Color Guessing Game (JavaScript)

I'm working through a course on Coursera and we are supposed to build a color guessing game. They give you the basic structure etc. for reference but I've hit a few snags. My debugger says I have 7 errors on line 27.

Basically, I can't get the alerts to pop up and I'm not sure if my code is running properly because of it. I'm also not sure if it is sorting the array and assigning a number value to each element to create the randomness of the game.

If anyone could help explain / solve the issues step by step, I would be really grateful.

//Global Variables //
var target;
var guess_input;
var guess_input_text;
var finished = false;
var guesses = 0;
var colors = ["blue", "greeen", "red", "yellow", "orange", "purple"];

var sorted_colors = random_color.sort();

//Function 1//
function do_game() {
  //Is this right? //
  target = sorted_colors[Math.floor(Math.random() * sorted_colors.length)];

  while (!finished) {
    guess_input_text = prompt(" I am thinking of one of these colors: \n\n" + colors.join(",") +
      "\n\n What color am I thinking of?");

    guess_input = guess_input_text;
    guesses += 1;
    finished = check_guess();
  }

  //Function 2 //
  function check_guess(guess_input) {
    if sorted_colors.indexOf(guess_input) == -1); {
    alert("Sorry, I don't recoginize your color. \n\n" + "Please try again.");
    return false;
  }
}
}

// If Statement 1 //
if (guess_input > target) {
  alert("Sorry, your guess is not correct!\n\n " + "Hint: your color is alphabetically higher than mine. \n\n " + "Please try again.");
  return false;
}

// If Statement 2 //
if (guess_input < target) {
  alert("Sorry, your guess is not correct! \n\n " + "Hint: your color is alphabetically lower than mine. \n\n" + "Please try again.");
  return false;
}

//If Statement 3 (positive) //
if (guess_input == target) {
  alert("Congratulations! You have guessed the color! \n\n" + "It took you " + guesses + " to finish the game! \n\n" + "You can the color in the background.");
  return true;
}
<!DOCTYPE html>

<head>
  <title>Color Guessing Game</title>
</head>

<body onload="do_game()">
  <script src="js_guessing_game.js">
  </script>
</body>

</html>

Upvotes: 0

Views: 2952

Answers (2)

Yalin
Yalin

Reputation: 355

to you put wrong ") ;"

put this one :

  function check_guess(guess_input) {
    if (sorted_colors.indexOf(guess_input) == -1) {
    alert("Sorry, I don't recoginize your color. \n\n" + "Please try again.");
    return false;
  }

instead this one:

  function check_guess(guess_input) {
    if sorted_colors.indexOf(guess_input) == -1); {
    alert("Sorry, I don't recoginize your color. \n\n" + "Please try again.");
    return false;
  }

Upvotes: 0

RyanM
RyanM

Reputation: 761

This is your code :

//Function 2 //
  function check_guess(guess_input) {
    if sorted_colors.indexOf(guess_input) == -1); {
    alert("Sorry, I don't recoginize your color. \n\n" + "Please try again.");
return false;
}

Line 27 :

if sorted_colors.indexOf(guess_input) == -1); {

you are missing a opening bracket before sorted and you need to remove the semi-colon after the closing bracket and your code should be like this now.

if (sorted_colors.indexOf(guess_input) == -1) {

don't worry things like these will happen while your learning their just little syntax errors goodluck man :)

Upvotes: 2

Related Questions