Hello everyone, I have just completed the Guess the Numbers Challenge project (JavaScript) and I would like to share the code.
The number-guessing challenge project (JavaScript) has been completed and the code is shared. Guess the Numbers Challenge project (JavaScript)

Here is my code. I have been exposed to JS back in college so I did use some techniques that some may not have been exposed to as of yet. Hopefully it is still legible to all.

let humanScore = 0;
let computerScore = 0;
let currentRoundNumber = 1;
// Generate the target number
const generateTarget = () => {
return Math.floor(Math.random() * 10);
};
// Determine the winner of the round
const compareGuesses = (humanGuess, computerGuess, targetNumber) => {
const humanPoints = Math.abs(humanGuess - targetNumber);
const computerPoints = Math.abs(computerGuess - targetNumber);
return humanPoints <= computerPoints ? true : false;
};
// update the score based on the winner
const updateScore = (winner) => {
if (winner === "human") humanScore++;
else computerScore++;
};
// advance the round
const advanceRound = () => {
currentRoundNumber++;
};
// simple test
let testTarget = generateTarget();
console.log(`Human #: 8
Computer #: 3
Target #: ${testTarget}
Human Won?: ${compareGuesses(8, 3, testTarget)}`);

I think you did great for your first time! I have two suggestions.

1.) You can use Math.abs to get the absolute value of the range of the user and computer guesses. This allows you to then use a single if…else to compare by checking if Math.abs(user - secret) <= Math.abs(computer - secret). If it is, return true, if not, return false. Too many if statements can cause you a great deal of mental frustration later when you revisit the code and have to try to remember what your if statements are accomplishing.

2.) In your update, winner should be a parameter of the function like this: const updateScore = (winner) => { … }. This will allow you to remove the line let winner = (‘human’ || ‘computer’);.

You did great and remember that there are many ways to solve the problem. Once you find a solution, go back and try to refactor to improve your functions without changing functionality. There are a few more optimizations that your could do to your code. As you learn more, try to go back and see if you can refactor this code for better optimization! You got this!

The parentheses can only be omitted if the function has a single simple parameter. If it has multiple parameters, no parameters, or default, destructured, or rest parameters, the parentheses around the parameter list are required.

If there is only one simple parameter in an arrow function, then the parentheses are optional.

let humanScore = 0;
let computerScore = 0;
let currentRoundNumber = 1;// Write your code below:
//Create a new random secret target number at the start of each round.function generateTarget() {
return Math.floor(Math.random() * 9);
}//Determines which player wins based on which guess is closest to the target.
//If both guesses tie, human should win.function compareGuesses(human, computer, target){
human < 0 || human > 8 > alert('The number you\'ve provided is out of range. Please enter a number between 0 and 9.');
return Math.abs(human - target) <= Math.abs(computer-target) ? true : false;
}//Update the score for the winner by 1. Accepts 'human' or 'computer'.function updateScore(winner) {
winner == "human" ? humanScore += 1 : computerScore += 1;
}//Update the round number after each round.function advanceRound() {
currentRoundNumber += 1;
}

I am having some trouble with this one. Is anyone able to take a gander at my code and let me know what they thing. It does not appear to be working at the moment.

let humanScore = 0;
let computerScore = 0;
let currentRoundNumber = 1;