I’ve been trying to fiddle around with this code for a while on this project, spending about an hour trying to figure out what’s wrong. There’s no syntax error yet the code doesn’t seem to function whereas the I put in a human guess and click “Make A Guess”, but nothing happens. I’ve been going back and forth between the game.js and script.js files to see if there’s something off between, but haven’t pinned anything down.
Here’s my code:
let humanScore = 0;
let computerScore = 0;
let currentRoundNumber = 1;
// Write your code below:
// Generates a random target number
const generateTarget = () => {
return Math.floor(Math.random() * 10)
}
/* This function compares the number choose from the human and computer
as well as which guess was closer to the secret target number
*/
const compareGuesses = (humanGuess, computerGuess, secret) => {
let user1 = Math.abs(humanGuess - secret)
let comp1 = Math.abs(computerGuess - secret)
if (userGuess >= 9 || userGuess < 0) {
return 'Please pick a number between 0 and 9!'
}
if (user1 < comp1 || user1 === comp1) {
return true;
} else if (comp1 < user1) {
return false;
}
}
// This updateScore function updates the score based on whether the human or computer wins
const updateScore = winner => {
if (winner === 'human') {
humanScore += 1;
} else if (winner === 'computer') {
computerScore += 1;
}
// Advances the round Number
const advanceRound = () => {
currentRoundNumber += 1;
}
Hello @gesro_dragon, welcome to the forums! It doesn’t appear that you’re calling your functions anywhere. Remember that functions must be called in order for the code to run:
function someFunc(){
console.log("This won't be printed unless the function is called");
}
someFunc();
//function called. output:
>>This won't be printed unless the function is called
That’s the thing though, the functions are called through the game.js file that is linked to the script.js file. The code shown there is the script.js file which is why I didn’t call any of the functions.
Haven’t said this enough, if we’re following the lesson to the letter, warts and all, there should be no need to mess with any code, apart from writing exactly what is asked for. That gets us through the checkpoints.
There are some warts, but one needs to consider every little fix very carefully. Don’t do it by adding code. Do it by adjusting the existing code.
From the first time I laid into this lesson it was with disappointment that there would be any errors. It should have been a walk in the park the way the lesson and provided code is structured. Instead, somebody decided it was good enough to go into production and ignored any review. The warts have been there since day one. Years ago.
The best thing that could happen would be someone present viable fixes for the existing code without adding to it. Make it possible for a learner to get an actual working version from the instructions in the lesson plan.