Random number guesser project

I have been working on this project for a good while and after having completed it I have a question…I understand mostly everything about how my code works except for this (it is probably what took me the longest to figure out):

In my function “compareGuesses” I was having trouble with the ‘target’ parameter. At first it was coming back as undefined but now it works, but my question is: when I call the function, how does it know that ‘target’ is the value taken from the math randomizer function that came before it? This is a concept that confuses me a bit…

let humanScore = 0; let computerScore = 0; let currentRoundNumber = 1; // Write your code below: const generateTarget = () => { return Math.floor(Math.random() * 10); } const compareGuesses = (human,computer,target) => { const humanDifference = Math.abs(target-human); const computerDifference = Math.abs(target - computer); if (humanDifference <= computerDifference) { return true } else { return false; } } const updateScore = winner => { if (winner === 'human') { return humanScore++; } else if (winner === 'computer') { return computerScore++; } } const advanceRound = () => { currentRoundNumber++; }

There is another file in the project [ game.js ] that calls these functions.
In that file, the result of calling the generateTarget function is stored as a variable,
and the function compareGuesses is called using that variable as an argument.

1 Like