I have just started learning JavaScript and have just made my first program a simple guess a number game. To give the user feed back about there guesses, I used console.log(). It worked on the website that I used to learn JavaScript (http://www.codecademy.com/learn), but when I put it in Notepad, saved it an .htm file, and run it, I get the prompts and confirm, but no visible feed back from the console.log command.
How can I get the console.log command to work?
Here is the code:
<Script language="JavaScript">
confirm("Are you ready to play 'I CAN GUESS THAT'? A game where Player 2 tries to guess player 1 number?");
//find out names
var player1 = prompt("Player 1 what is your name?","Your name here");
var player2 = prompt("Player 2 what is your name?","Your name here");
//player 1 number
var place_holder = 0;
var p1 =place_holder;
while (p1 > 1000 || p1 == 0) {
p1 = prompt(player2 + "look away." + " " + player1 + " " + "what is your number?", "Your number from 1 to 1,000 here");
if (p1 != parseInt(p1)) {
p1 = 0;
console.log("Error: Invalled Number!" + " " +player1 + " " + "Please choose a number between 1 and 1,000");
}
else if(p1>1000) {
console.log("Error: Invalled Number!" + " " +player1 + " " + "Please choose a number between 1 and 1,000");
}
};
//set up used guess list
var listlow = [];
var listhigh = [];
var x = 0;
var p2 = place_holder;
//game
while (x < 11) {
//list used guesses
console.log("Your guess so far");
console.log("Your to low guesses:"+" " + listlow);
console.log("Your to high gusses:"+" " + listhigh);
//player 2 guess
var p2 = prompt("Player 2 what is your guess?");
//good guess
var test = p1/p2;
if (test === 1) {
console.log("Congrats" + " " + player2 +" "+ "You have guessed"+" " + " " + player1 +" "+ "number");
var x = 30;
//to low
}
else if (test > 1) {
console.log(player2 +" "+ "Sorry your guess is to low");
listlow.push(p2);
x=x+1;
//to high
}
else if (test <1) {
console.log(player2 +" "+ "Sorry your guess is to high");
listhigh.push(p2);
x=x+1;
//something went wrong
}
else {
console.log("Opps something went wrong");
}
};
if (x < 20) {
console.log("Sorry" + " " +player2+ " "+ "You are out of guesses." +" " + player1+ " " + "wins!");
}
console.log("Thanks for playing")
</SCRIPT>
f12append()method is an easy way to add something visible on your page. As a note, writing "interactive" applications like this, something you might find in a "how to code" book from the 1970s, is really contrary to how a web page works. It'd be much better to create a proper form and hook up buttons and input fields to your code.