Reputation: 4149
I'm building the code for a "Number Guesser Game". How can I change the score after each play?
It's my first unassisted assignment for an online course. I don't have the basic understanding of what to use to achieve certain outcomes so I apologize in advance for the noob question.
I got stuck at task number 5:
Create an updateScore() function. This function will be used to correctly increase the winner’s score after each round.
This function:
Has a single parameter. This parameter will be a string value representing the winner. Increases the score variable (humanScore or computerScore) by 1 depending on the winner passed in to updateScore. The string passed in will be either 'human' or 'computer'. Does not need to return any value.
First of all: Why do I need to create a function to change the scoreboard in the first place? Couldn’t I just write code to change it after I set the winner in the “compareGuesses” function?
This is what I have so far:
// Write your code below:
let humanScore = 0;
let computerScore = 0;
let currentRoundNumber = 1;
const generateTarget = Math.floor(Math.random() * 9)
const compareGuesses = (humanGuess, computerGuess,targetGuess) => {
if (Math.abs (humanGuess-targetGuess() )<Math.abs( computerGuess-targetGuess() )){ return true;
} else if (Math.abs (humanGuess-targetGuess() )> Math.abs( computerGuess-targetGuess() )){
return false;
}
}
const updateScore = (winner) => {
if (compareGuesses= true) {
}
Upvotes: 1
Views: 464
Reputation: 50664
First of all: Why do I need to create a function to change the scoreboard in the first place? Couldn’t I just write code to change it after I set the winner in the “compareGuesses” function?
Yes, you could do that, but to help with code maintainability (and a number of other things), it is often a good idea to split separate chunks or modules of your larger application/problem into smaller functions. This idea is known as decomposition. Thus, by creating separate functions such as updateScore
and compareGuesses
you're breaking your bigger problem into smaller more maintainable problems.
Now for your updateScore
function. Your description says that it will accept a string, either being "human"
or "computer"
. You need to check whether the winner is either "human"
or "computer"
and update the associated score variable. Here is an example on how you can achieve this:
const updateScore = (winner) => {
if(winner === 'human') {
humanScore += 1; // humanScore++; will also work here
} else if(winner === 'computer') {
computerScore += 1; // computerScore++; will also work here
}
}
Upvotes: 3
Reputation: 281
Try something like this:
class Game {
humanScore = 0;
computerScore = 0;
currentRoundNumber = 1;
target = 0;
generateTarget() {
this.target = Math.floor(Math.random() * 9)
}
nextRound(humanGuess, computerGuess) {
this.currentRoundNumber++;
if (Math.abs(humanGuess-this.target) < Math.abs(computerGuess-this.target))
this.humanScore++;
else if (Math.abs(humanGuess-this.target) > Math.abs(computerGuess-this.target))
this.computerScore++;
}
}
const game = new Game();
game.generateTarget();
game.nextRound(333, 42);
PS: StackOverflow is not here to do your homework. :)
Upvotes: 0
Reputation: 377
So if you wold like to increase player score if he guess the number correctly and increase computer score if the guess is not correct, the function may look like this:
function score(humanScore, computerScore) {
if (humanGuess - computerGuess === 0) {
humanScore += 1
}
else {
computerScore += 1
}
alert(humanScore)
alert(computerScore)
}
Of course, you need to call a function like this:
score(humanScore, computerScore)
Upvotes: 0