Monsieur aravind
Monsieur aravind

Reputation: 1

javascript error:Uncaught SyntaxError: Unexpected identifier

I am beginner in javascript coding and i am got this error when i load my first program in chrome browser teach me what it is

code:

 var target;
 var select;
 var colors = ["brown", "cyan", "yellow", "red", "blue", "green", "black", "white", "purple", "pink"];
 var finished = false;

 function do_game() {
   var random_color = Math.floor(Math.random() * colors.length);
   target = colors[random_color];
 }
 while (!finished) {
   select = prompt("I am thinking of one of these colors\n\n brown,cyan,yellow,red,blue,green,black,white,purple,pink\n what color am i thinking of");
   finished = check_guess();
 }

 function check_guess() {
   if (select == target) {
     return true;
   } else {
     return false;
   }

 }

Upvotes: 0

Views: 1256

Answers (3)

Tiefan Ju
Tiefan Ju

Reputation: 520

<script>

var target;
var select;
var colors = ["brown", "cyan", "yellow", "red", "blue", "green", "black", "white", "purple", "pink"];
var finished = false;

function do_game() {
    var random_color = Math.floor(Math.random() * colors.length);
    target = colors[random_color];
    while (!finished) {
        select = prompt("I am thinking of one of these colors\n\n brown,cyan,yellow,red,blue,green,black,white,purple,pink\n what color am i thinking of");
        finished = check_guess();
    }
}
function check_guess() {
    if (select == target) {
        return true;
    } else {
        return false;
    }
}

do_game();

</script>

I think all raw statements must be enveloped in functions even if it is very simple.

Upvotes: 0

trincot
trincot

Reputation: 350147

That actual error you mention does not occur in the code you provided. I assume you have a call to do_game in an HTML attribute, like an onclick attribute, and you have a small typo there, like a misplaced comma or use of reserved word, ... or one of many other syntax issues.

You should:

  • Call do_game (it is never called in the code you provided)
  • Put the loop in that function

And improve further:

  • Detect when the user cancels the prompt
  • Use local variables instead of global variables, and pass the variables via function parameters when they are needed elsewhere. True constants can be global.
  • Make use of let and const instead of var
  • Avoid the if (comparison) return true else return false pattern. Just do return comparison.
  • Let the user know when they have guessed it right
  • As you already have an array of colors, don't repeat that again as a literal string in the question, but reuse that array.

// The list of colors can be global, but the rest should be locally defined
const colors = ["brown", "cyan", "yellow", "red", "blue", "green", "black", "white", "purple", "pink"];

function do_game() {
    const random_color = Math.floor(Math.random() * colors.length);
    // Make your variables local to the function
    const target = colors[random_color];
    let finished = false;
    
    while (!finished) {
        // Actually declare `select`, or it will be global.
        // Reuse the variable with colors to build the question 
        //  (the commas will be inserted by the automatic conversion to string) 
        const select = prompt("I am thinking of one of these colors\n\n"
            + colors + ".\n\n What color am I thinking of?");
        // Exit game when prompt was cancelled
        if (select === null) return; 
        // Pass the necessary info to the other function (instead of using globals):
        finished = check_guess(select, target);
    }
    // Let the user know that they guessed it
    alert('You guessed it: ' + target);
}

function check_guess(select, target) {
    // No need to if-else a true/false, just return the comparison's result
    return select == target; 
}

// Start the game 
do_game(); 

Upvotes: 3

Ferus7
Ferus7

Reputation: 727

Your problem is that you never call do_game function, so target variable is never initialized and the loop will never end, Check this:

var target;
var select;
var colors = ["brown", "cyan", "yellow", "red", "blue", "green", "black", 
"white", "purple", "pink"];
var finished = false;

do_game();
startGame();


function do_game() {
var random_color = Math.floor(Math.random() * colors.length);
target = colors[random_color];
console.log(target)
}

function startGame() {
while (!finished) {
select = prompt("I am thinking of one of these colors\n\n 
brown,cyan,yellow,red,blue,green,black,white,purple,pink\n what color am i 
thinking of");
finished = check_guess();
 } 
 }
  function check_guess() {
  if (select == target) {
  return true;
  } else {
   return false;
  }
  }

https://jsfiddle.net/4qops0br/1/

Upvotes: 0

Related Questions