How does JavaScript know what information to extract?

In the code below inside the compareGuesses() function we are calculating the distance between target and computerGuess & the distance between target and humanGuess and depending on which one is closer to the target number they will win. My question is how does javaScript know that the parameter ‘target’ is the same number as the function generateTarget()? The reason I ask the question is because we are not directly passing an argument through the target parameter. How are they linked?

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

// Write your code below:
function generateTarget(){
return Math.floor(Math.random() * 10);
}
const getAbsoluteDistance = (num)

function compareGuesses(humanGuess, computerGuess, target){
const computerG = Math.abs(target - computerGuess);
const userG = Math.abs(target - humanGuess);
return userG <= computerG;
}

let updateScore = (win) => {
if(win === ‘human’){
humanScore += 1;
} else{
computerScore += 1;
}
}

let advanceRound = () =>{
currentRoundNumber += 1;
}

updateScore(‘human’);
console.log(humanScore);

Yea, as it is, there is no call to generateTarget() to produce it. There’s also no logic to start game-play.

So you’re completely right in that to run compareGuesses() you would either need to provide some dummy target, or generate a random one with generateTarget() to then provide to the compareGuesses() call.

Thank you for the quick reply.
Follow up question. So, the reason this code works is because the course creators have some code running in the background that ties everything together? This is the number Guesser project.

Yea, if you’re seeing some behavior that’s not in the code it’s probably in a supplemental script provided by the platform. Sometime if you click on the file tabs you can see it, and if you’re more adventorous sometimes you can find it with inspect element.

Ahhh lol. Thank you so much! I was super confused and almost researched myself to death. But again, thank you for the assistance.

1 Like