Number Guesser won't proceed to the next round, + 1 more issue

I’ve gotten quite far in this project that combines JavaScript with some premade CSS and HTML; I know nothing of neither. Nevertheless, I got the correct feedback, creating a target, inputting a player and computer value and a comparison (with a mistake still). But I didn’t got the round to advance and I can’t figure out why, it looks like a tiny mistake. I do know that currentRoundNumber is recognised by function advanceRound(). I even found a workspace to test my code.

To summarise:

The code almost works, but I can’t get the round to advance and something in function advanceRound() is to blame.

The code compares playerScore with computerScore and assigns the correct winner, but assigns the score for that win always to the player.

The code is below, but You’ll probably need this: 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

let humanScore = 0;

let computerScore = 0;

let currentRoundNumber = 1;

// Write your code below:

function generateTarget() {

  return Math.floor(Math.random() * 10)

};

// console.log(generateTarget());

function compareGuesses(human, computer, target) {

  // let target = generateTarget()

  let playerScore = (human - target)

  if (playerScore < 0) {

    playerScore = playerScore * -1;

  }

  let computerScore = (computer - target)

  if (computerScore < 0) {

    computerScore = computerScore * -1;

  }

  if (playerScore <= computerScore) {

    return true;

  } else {

    return false;

  }

}; 

function updateScore() {

  if (compareGuesses = true) {

    humanScore = humanScore + 1;

  } else {

    computerScore = computerScore + 1;

  }

};

function advanceRound() {

  for (let i = 0; i < 100; i++) {

    generateTarget();

    compareGuesses();

    updateScore();

    currentRoundNumber = currentRoundNumber + 1;

  }

};

I see a problem in the updateScore function, in the if-statement:

The = should be === since you’re checking if they’re equal, not assigning a new value to compareGuesses

Also, you’re missing a parameter for the updateScore function, and compareGuesses is probably not the variable you should be using here.

like this:

function updateScore(winner) {

Here’s a link to a different post about that: post 5 (about task 6-7)

Thanks. Gah, I hate missing the simple things. And thanks for obscuring the rest of the solution. It’s good that I get to figure that out.

Thanks for the link to your other post. Curiously enough, I was not alone in not adding a parameter to updateScore(). More to the point, I do not understand why a parameter is required here. Is it because it is not possible to pass a boolean to another function, or is it because compareGuesses ‘returns’ a boolean, instead of generating a boolean value, much like the human, computer and target values are generated somehwere else, and then fed into the compageGuesses function.

As you can probably tell, how functions pass on information to one another is a weak link of mine here. I don’t think Codecademy does a good job of explaining this of if it does, I have missed the (point of) the explanation entirely. Is there a good source for picking up the rules of passing on values from one function to the next in general?

You can have a variable store the information in order to get information from one function to another. You can also have nested functions (like this double(triple(5)) would do the function triple to 5, and then it would take the result and do double to that).
Or you can call a function inside the body of another function.

Be careful not to mix up a variable storing the function with the variable that stores the result of the function being called. We normally have to use () to call a function.

const checkIsEven = (num) => {
  /* body of the function here */
  return (num % 2) === 0;  // returns true or false
}

// The checkIsEven variable now contains a function.

let n = 5;
const isEven = checkIsEven(n); // function call: called checkIsEven with argument n
// isEven contains either true or false

const multipleOf2 = checkIsEven;
// multipleof2 now contains a reference to the checkIsEven function
/* you can now use multipleof2 as a function (same as the checkIsEven function) */
n = 6
const sixIsMultipleOf2 = multipleof2(n);
// sixIsMultipleOf2 contains either true or false
1 Like

You don’t see how all those functions pass information to each other, and how they change the stuff on the webpage easily because that is done in the other JavaScript file (the one you’re not changing in this project).
The functions you make for the project are called in that other file.

Thank you very much for this reply. I am still working through it but gotten a few things from it already. For example, to figure stuff out, I started to write random code to see what my approach to functions is. It turns out I write nested functions. That’s what the last line of my practice code looks like to me, I appreciate a confirmation on that; code below.

const noOfHouses = 20;
// console.log(noOfHouses);
//function maintenance () {
  // multiply the number of houses by 100, import noOfHouses
//}
// const maintenance = noOfHouses * 100;
// console.log(maintenance);
// I want to apply maintenance of 100 when less than 15 houses, when more, maintenance is 80
function manyMaintenance (howManyHouses) {
  if (howManyHouses < 15) {
    return howManyHouses * 100;
  } else {
    return howManyHouses * 80;
  }
};
console.log(manyMaintenance(noOfHouses));
// So I can pass a constant into another function. I should be able to take the result of a function (not just a constant) and pass it into another function.
function applyForSubsidy (maintenanceCost) {
  if (maintenanceCost > 1500) {
    return maintenanceCost * 0.9;
  } else {
    return 'No subsidy is granted.';
  }
};
console.log(applyForSubsidy(manyMaintenance(noOfHouses)));

By the way, that final line of nested functions, is that bad coding practice?

Most importantly, the phrase // The CheckIsEven variable now contains a function. points to where I should look. I assumed the function started when I made a const. I guess it doesn’t I need to work through the implications of this, but with the examples of code that you gave and the resources at my disposal, I think I can manage.

Also thanks for pointing out that there’s another JavaScript file that I am not changing to contend with. I figured as much, but it’s good to have confirmation.

No, nested function calls are not bad practice. They are a common tool.

Remember that a const variable does not have to be a function, it can be something else:

const pi = 3.14159;

A variable that is const just can’t be reassigned to a new value or object.