Number Guesser - step 5 inquiry and computer Score problem

Heya

Im doing the fullstack engineer course at the the Number Guesser project
under the javascript syntax 1 unit at functions

got 2 questions

  1. At step 5 I’m instructed as follows:

" 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."

Given compareGuesses() function we did at step 4 is obviously needed to be passed as the parameter for this function
yet we instructed to compareGuesses() only return true or false

what do they mean here?
pass in the result from step 4s function but also ‘human’ or 'computer?

As i didnt understand the intention i went with using step 4’s function return value true/false
to be the one passed to the function to be created at step 5 updateScore()
which leads to my next question

  1. I finished all the steps till step 7

https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-i/modules/fecp-challenge-project-number-guesser/projects/number-guesser-independent-practice

I tested the code and all functions under Vcode
everything works at the debug level
but when i run the game both my browser (downloaded the files) and in the codecademy environment
the computer score doesnt update when the computer wins

Any help would be appreciated

My code is below

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

// Write your code below:
//generates secret target number at start of new round
const generateTarget = () =>{
    let num = Math.floor(Math.random()*10);
    return num;
}

//compares guesses to determine who wins
const compareGuesses = (user,computer,target) =>{
    if( user<0 || computer<0 ) {
        return 'Invalid inputs numbers must be between 0 and 10';
    }
    let userRange = (target-user);
    let computerRange = (target-computer);
    if( target - user < 0 ) {   
        userRange = (userRange*(-1));    
    }else{
        userRange;
    }
    if( target - computer < 0 ) {   
       computerRange =(computerRange*(-1));    
    }else{
         computerRange;
    }
    if( userRange < computerRange || userRange === computerRange) {
        return true;
    }else {
        return false;
    }  
}      
// function updates score depending who wins
const updateScore = (str) =>{
    if(str){
        humanScore += 1;
    }else{
        computerScore += 1;
    }       
}
//advances round by 1
const advanceRound =() => {
   return currentRoundNumber += 1;
}

That’s because you don’t call your function updateScore.

You don’t have to necessarily call your updateScore function by directly passing compareGuesses as an argument to it. According to the instruction, they want you pass a string, which would require that you write another function that returns a string.
Yet your updateScore function would work with a boolean passed to it. But in order to call it with compareGuesses, you should make sure that it only returns a boolean. That means you should check if the user and the computer numbers are between 0 and ten before you pass the user and computer to compareGuesses function.

Aint sure if people have access to this lesson so ill mention other few things

First there’s another js script that does call the functions build above thats already preset
as at this point we only learning js basics and not yet doing the connectivity to the html etc

I’ll attach that code below and bold the part that calls the function
though i dont know what it all means,seems like they check if compareGuesses returns a boolean which mine does

second yes my compareGuess does return a boolean, therefore my 1st question not understanding what they mean with another string human computer,and if so how and why do it if true or false are sufficient :thinking: :thinking:

let target;

const humanGuessInput = document.getElementById('human-guess');

const roundNumberDisplay = document.getElementById('round-number');

const computerGuessDisplay = document.getElementById('computer-guess');

const humanScoreDisplay = document.getElementById('human-score');

const computerScoreDisplay = document.getElementById('computer-score');

const targetNumberDisplay = document.getElementById('target-number');

const computerWinsDisplay = document.getElementById('computer-wins');

const guessButton = document.getElementById('guess');

const nextRoundButton = document.getElementById('next-round')

guessButton.addEventListener('click', () => {

  // Generate the target value

  target = generateTarget();

  // Retrieve the player's guess

  const currentHumanGuess = humanGuessInput.value;

  // Make a random 'computer guess'

  const computerGuess = Math.floor(Math.random() * 10);

  // Display the computer guess and the target

  computerGuessDisplay.innerText = computerGuess;

  targetNumberDisplay.innerText = target;

  

  // Determine if the human or computer wins:

  **const humanIsWinner = compareGuesses(currentHumanGuess, computerGuess, target)**

  const winner = humanIsWinner ? 'human' : 'computer'

 // Update the correct score:

**updateScore(winner)**;

  // Display the winner

  if (humanIsWinner) {

    guessButton.innerText = 'You Win!!!!!';

    guessButton.classList.toggle('winning-text')

  } else {

    computerWinsDisplay.innerText = 'Computer Wins!!!';

  }

  // winnerDisplay.innerText = humanIsWinner ? 'You win!' : 'Computer wins!';

  // Display the current scores:

  humanScoreDisplay.innerText = humanScore;

  computerScoreDisplay.innerText = computerScore;

  

  // Set the correct disabled state for the buttons

  guessButton.setAttribute('disabled', true)

  nextRoundButton.removeAttribute('disabled');

});

nextRoundButton.addEventListener('click', () => {

  // Increase the round number

  advanceRound();

  // Display the new round number

  roundNumberDisplay.innerText = currentRoundNumber;

  // Set the correct disabled state for the buttons

  nextRoundButton.setAttribute('disabled', true);

  guessButton.removeAttribute('disabled');

  // Reset the guess input box and the target number display:

  targetNumberDisplay.innerText = '?';

  guessButton.innerText = 'Make a Guess';

  humanGuessInput.value = '';

  computerGuessDisplay.innerText = '?';

  computerWinsDisplay.innerText = '';

  guessButton.classList.remove('winning-text');

});

const addButton = document.getElementById('add');

const subtractButton = document.getElementById('subtract');

addButton.addEventListener('click', () => {

  humanGuessInput.value = +humanGuessInput.value + 1;

  handleValueChange(humanGuessInput.value);

});

subtractButton.addEventListener('click', () => {

  humanGuessInput.value = +humanGuessInput.value - 1;

  handleValueChange(humanGuessInput.value);

});

const handleValueChange = value => {

  if (value > 0 && value <= 9) {

    subtractButton.removeAttribute('disabled');

    addButton.removeAttribute('disabled');

  } else if (value > 9) {

    addButton.setAttribute('disabled', true);

  } else if (value <= 0) {

    subtractButton.setAttribute('disabled', true);

  }

}

humanGuessInput.addEventListener('input', function(e) {

  handleValueChange(e.target.value);

});

Ok, I see. But that means that it is called with the values mentioned in the exercise which are these:

  • 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' .

Then you have to check whether the argument passed to the updateScore function is 'human' or 'computer'.

Edit:
This is the string passed to updateScore():

From game.js:

  // Update the correct score:
  updateScore(winner);

:thinking: :thinking:

I see what your saying
I adjusted my code accordingly to add a ‘human’ ‘computer’
values yet still computer doesn’t register
wonder whats might be the mistake :thinking:

let humanScore = 0;
let computerScore = 0;
let currentRoundNumber = 1;
// Write your code below:
//generates secret target number at start of new round
const generateTarget = () =>{
    let num = Math.floor(Math.random()*10);
    return num;
}
//compares guesses to determine who wins
const compareGuesses = (user,computer,target) =>{
    let userRange = getAbsoluteDistance(user,target);
    let computerRange = getAbsoluteDistance(computer,target);
    if( user< 0 || user >9) {
         alert('Invalid inputs numbers must be between 0 and 9');   
    }
//step 8 insert new function to replace separate if statements    
/*
    let userRange = (target-user);
    let computerRange = (target-computer);
    if( target - user < 0 ) {   
        userRange = (userRange*(-1));    
    }else{
        userRange;
    }
    if( target - computer < 0 ) {   
       computerRange =(computerRange*(-1));    
    }else{
         computerRange;
    }
*/
    if( userRange < computerRange || userRange === computerRange) {
        return 'human',true;
    }else {
        return 'computer',false;
    }  
}      

// function updates score depending who wins

const updateScore = (str) =>{
    if(str){
        humanScore += 1;
    }else{
        computerScore += 1;
    }       
}

//advances round by 1
const advanceRound =() => {
   return currentRoundNumber += 1;
}
//calculates absolute distance from target 
const getAbsoluteDistance = (num1,num2) =>{
    let distance = (num2-num1);
    if( distance < 0 ) {   
        distance = (distance*(-1));    
    }
    return distance;
}

You’re still looking for a boolean or just any string that evaluates to truthy here.
In that function the condition will always evaluate to truthy, no matter if the string is ‘human’ or ‘computer’.

If the string passed to the function is ‘human’ humanScore has to be increased. If it is computer, computerScore has to be increased.

extra hint

if(str === ‘human’)

yep missed that!
now works thanks!:slight_smile: