JavaScript Debugging


#1

So I have a problem where I cannot seem to figure out why updateCount() is not functioning as I intended
So instead of first to 5 it goes to like first to 1
PS. After this code, you can run the code in your browser and find the problem

// Global Variables
var TAILS_KEYCODE = 84;
var HEADS_KEYCODE = 72;
var PLAYER_SCORE = 0;
var COMPUTER_SCORE = 0;
var SCORE_GOAL = 5;
var GAME_END = false; 
var PLAYER_CHOICE = "blank";
var COMPUTER_CHOICE = "blank";
var COMPUTER_NUMBER = 0;
var GENERATED_CHOICE = "blank";
var GENERATED_NUMBER = 0;

// StartUp
console.log(`Get Ready`);

// Sensors
window.addEventListener('keydown', keyStroke);

// Function
function keyStroke(event) {
if (event.keyCode === TAILS_KEYCODE && !GAME_END) {
console.log(`You picked Tails`);
PLAYER_CHOICE = "Tails";
computerPick();
answerGenerate();
checkPlayer();
checkComputer();
updateCount();
} else if (event.keyCode === HEADS_KEYCODE && !GAME_END) {
console.log(`You picked Heads`);
PLAYER_CHOICE = "Heads";
computerPick();
answerGenerate();
checkPlayer();
checkComputer();
updateCount();
} else if (GAME_END) {
console.log(`Game is already finished!`);
} else {
console.log(`Press h(Heads) or t(Tails)`);
}
}

function computerPick() {
COMPUTER_NUMBER = Math.floor(Math.random() * 2) + 1;
if (COMPUTER_NUMBER === 1) {
COMPUTER_CHOICE = "Heads"
} else if (COMPUTER_NUMBER === 2) {
COMPUTER_CHOICE = "Tails"
} else {
console.log(`Unknown Error: computerPick`);
}
} 

function answerGenerate() {
GENERATED_NUMBER = Math.floor(Math.random() * 2) + 1;
if (GENERATED_NUMBER === 1) {
GENERATED_CHOICE = "Heads";
} else if (GENERATED_NUMBER === 2) {
GENERATED_CHOICE = "Tails";
}
}

function checkPlayer() {
if (PLAYER_CHOICE === GENERATED_CHOICE) {
PLAYER_SCORE = PLAYER_SCORE + 1;
console.log(`You got a point!`);
} else {
console.log(`Wrong!`);
}
}

function checkComputer() {
if (COMPUTER_CHOICE === GENERATED_CHOICE) {
COMPUTER_SCORE = COMPUTER_SCORE + 1;
console.log(`The computer got a point!`);
} else {
console.log(`The computer failed!`);
}
}

function updateCount() {
if (PLAYER_SCORE >= 5 && PLAYER_SCORE > COMPUTER_SCORE && COMPUTER_SCORE >= 5) {
console.log(`You WIN!`);
GAME_END = true;
} else if (PLAYER_SCORE >= SCORE_GOAL && PLAYER_SCORE === COMPUTER_SCORE || COMPUTER_SCORE >= SCORE_GOAL) {
console.log(`TIE!`);
GAME_END = true;
} else if (PLAYER_SCORE >= SCORE_GOAL && PLAYER_SCORE < COMPUTER_SCORE || COMPUTER_SCORE >= SCORE_GOAL) {
console.log(`You LOSE!`);
} else if (!PLAYER_SCORE >= SCORE_GOAL && !COMPUTER_SCORE >= SCORE_GOAL) {
console.log(`You(${PLAYER_SCORE}) vs Computer(${COMPUTER_SCORE})`);
} else {
console.log(`Error on updating count`)
}
}

// Extra Tools

// KeyStroke Recorder
function record(event) {
console.log(event.keyCode);
}

Open a text editor and save this code below as .html and run in your browser to test

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Coin Flip</title>
</head>
<body>
<script>
// Global Variables
var TAILS_KEYCODE = 84;
var HEADS_KEYCODE = 72;
var PLAYER_SCORE = 0;
var COMPUTER_SCORE = 0;
var SCORE_GOAL = 5;
var GAME_END = false; 
var PLAYER_CHOICE = "blank";
var COMPUTER_CHOICE = "blank";
var COMPUTER_NUMBER = 0;
var GENERATED_CHOICE = "blank";
var GENERATED_NUMBER = 0;

// StartUp
console.log(`Get Ready`);

// Sensors
window.addEventListener('keydown', keyStroke);

// Function
function keyStroke(event) {
if (event.keyCode === TAILS_KEYCODE && !GAME_END) {
console.log(`You picked Tails`);
PLAYER_CHOICE = "Tails";
computerPick();
answerGenerate();
checkPlayer();
checkComputer();
updateCount();
} else if (event.keyCode === HEADS_KEYCODE && !GAME_END) {
console.log(`You picked Heads`);
PLAYER_CHOICE = "Heads";
computerPick();
answerGenerate();
checkPlayer();
checkComputer();
updateCount();
} else if (GAME_END) {
console.log(`Game is already finished!`);
} else {
console.log(`Press h(Heads) or t(Tails)`);
}
}

function computerPick() {
COMPUTER_NUMBER = Math.floor(Math.random() * 2) + 1;
if (COMPUTER_NUMBER === 1) {
COMPUTER_CHOICE = "Heads"
} else if (COMPUTER_NUMBER === 2) {
COMPUTER_CHOICE = "Tails"
} else {
console.log(`Unknown Error: computerPick`);
}
} 

function answerGenerate() {
GENERATED_NUMBER = Math.floor(Math.random() * 2) + 1;
if (GENERATED_NUMBER === 1) {
GENERATED_CHOICE = "Heads";
} else if (GENERATED_NUMBER === 2) {
GENERATED_CHOICE = "Tails";
}
}

function checkPlayer() {
if (PLAYER_CHOICE === GENERATED_CHOICE) {
PLAYER_SCORE = PLAYER_SCORE + 1;
console.log(`You got a point!`);
} else {
console.log(`Wrong!`);
}
}

function checkComputer() {
if (COMPUTER_CHOICE === GENERATED_CHOICE) {
COMPUTER_SCORE = COMPUTER_SCORE + 1;
console.log(`The computer got a point!`);
} else {
console.log(`The computer failed!`);
}
}

function updateCount() {
if (PLAYER_SCORE >= 5 && PLAYER_SCORE > COMPUTER_SCORE && COMPUTER_SCORE >= 5) {
console.log(`You WIN!`);
GAME_END = true;
} else if (PLAYER_SCORE >= SCORE_GOAL && PLAYER_SCORE === COMPUTER_SCORE || COMPUTER_SCORE >= SCORE_GOAL) {
console.log(`TIE!`);
GAME_END = true;
} else if (PLAYER_SCORE >= SCORE_GOAL && PLAYER_SCORE < COMPUTER_SCORE || COMPUTER_SCORE >= SCORE_GOAL) {
console.log(`You LOSE!`);
} else if (!PLAYER_SCORE >= SCORE_GOAL && !COMPUTER_SCORE >= SCORE_GOAL) {
console.log(`You(${PLAYER_SCORE}) vs Computer(${COMPUTER_SCORE})`);
} else {
console.log(`Error on updating count`)
}
}

// Extra Tools

// KeyStroke Recorder
function record(event) {
console.log(event.keyCode);
}
</script>
</body>
</html>

#2

updateCount, first condition:

if (PLAYER_SCORE >= 5 || PLAYER_SCORE > COMPUTER_SCORE || COMPUTER_SCORE >= 5)

if i pick heads, and the computer pick tails, PLAYER_SCORE is 1 and COMPUTER_SCORE is 0, so then PLAYER_SCORE > COMPUTER_SCORE is true, so the if clause will run (given you use or operator, only one condition has to be true.


#3

Oh yes facepalm
It’s quite annoying getting mixed up


#4

Also since I’m new to forums
Can you lock your posts?


#5

I should lock my post, what do you mean by this? You made a topic, i replied to it. Should i do something with my reply?


#6

I meant topic
PS. I fixed it but it still works normally yet it still doesn’t flow over to logging the points. I added a error if it didn’t work and it gone to error


#7

so far so good

what?

what changes did you make? You made changes, please post an updated version of your code if its still not right


#8

Ok so I did update it
Expected:

Reality:

I’m not the best at JavaScript. Does ! mean not?


#9

Have you thought through all the conditions and comparisons you have in updateCount function? I showed there was a mistake in it, which was a clue from me for you, that you have to check and ensure your comparisons are correct


#10

What is the comparison operator for NOT


#11

!true is false, and !false is true, so ! is the not operator


#12

I debugged it, sorry for not looking at the replies

Also thanks

Mail](https://go.microsoft.com/fwlink/?LinkId=550986) for Windows 10