0

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;
   }

 }
4
  • 3
    Your while loop is floating outside of a function.... Do you want to loop in do_game? Commented Aug 1, 2017 at 7:27
  • 1
    Like that, there seems there is no issue (i copied and pasted it in the chrome console and it works) but as @Rick Bronger said maybe your while loop should be inside do_game function Commented Aug 1, 2017 at 7:35
  • yes i inserted loop in do_game but after that also it showing same error Commented Aug 1, 2017 at 7:37
  • Do you have any code in your HTML, like in onclick attributes? Please add that code in your question too. Or did you type code in the dev tools? Commented Aug 1, 2017 at 8:09

3 Answers 3

3

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(); 

Sign up to request clarification or add additional context in comments.

1 Comment

Nice answer, imho, the const initialization and the game loop should be in different functions, for cleaner code, but nice answer
0

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/

Comments

0
<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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.