Coming from the front end path, it felt like I was thrown into this project without much explanation to what node was or how to use it and how it works with VSC.
Somehow I’m supposed to use this new program on my own to do this project. It was very confusing, but I figured it out with the help of someone on the discord.
I wrote all of mine as functions and did a whole load of console logging at every point to verify it was working. I’ve left them in as I think to see that process is just as relevant as the answer.
Hi, I believe I have this working so I believe my code is correct - now for the real test - if someone could take a look at this and let me know if and where I can improve/correct things, much appreciated
Hi all, please could I ask for some advice. on where I’m going wrong? From the logs I"m getting ‘1, undefined, true’ I don’t think the issue is with my updateScore. Thank you in advance!
let humanScore = 0;
let computerScore = 0;
let currentRoundNumber = 1;
// to generate the target number
const generateTarget = () => {
return Math.floor(Math.random() * 10)};
//generateTarget ();
//to create human, computer and secret target number comparison
const compareGuesses = (humanTarget, computerTarget, targetNumber) => {
if (humanTarget < 0 || humanTarget > 9) {
return alert `number out of range`;
} else if (Math.abs(humanTarget - targetNumber) < Math.abs (computerTarget-targetNumber)) {
return true;
} else if
(Math.abs (humanTarget-targetNumber > Math.abs (computerTarget - targetNumber))) {
return false;
} else {
return true;
}
}
//compareGuesses ();
const updateScore = winner => {
if (winner === 'human') {
return humanScore +=1;
} else if (winner === 'computer') {
return computerScore +=1;
}
}
const advanceRound = () => {
return currentRoundNumber++;
}
console.log(advanceRound());
console.log(updateScore());
console.log(compareGuesses());
Hello everyone I’m trying to add the alert function to my code in the CodeCademy workspace for the moment before I move it over to VSCode. How would I go about creating a function to check the range of the users guess since it is a parameter in the compareGuesses function? The hint provided makes sense I just don’t know if I can even use the userGuess or humanGuess parameter to compare its value. Your help would be greatly appreciated thank you.
Hey, I see the issue with the updateScore function, when you return humanScore and computerScore it should be like this “return humanScore++” and "return “computerScore++”. This will increment the scores by 1. The way you have it written adds 1 and assigns the variables to that new value which we don’t want to happen since both variables change as you progress through the game.
Hey! No sure if your question was answered already. But I actually just added an additional line of code to the compareGuesses function. Essentially, i have the computer check that humanGuess is in the range, if not, the alert is triggered. If the number is in range, the rest of the if/else-if will run.
I know someone said the issue was how you wrote the increments as += vs ++, both will work. Your logs are printing correctly. The computer is printing to the console what you are asking, the round should be 1 because the game will initialize there, the updated score is undefined because you dont have a winner , and the compare guesses will return true because at the initializing of the game neither the human or computer have a guess, making the guesses “the same” and that returns true.