Number guesser

hello

Can someone help me ? This Number Guesser project , i have finished and it was ok yesterday. WHen i tested numbers on real game.js one , it was running but today it was showing error. I am confused and i don’t know where I am wrong anymore as it was working before and now not working anymore.
Please have a look my code in below. Codeacademy error keeps on showing line 27 has syntax error but i don’t write anything in line 27. Also i cannot play the game anymore and it is not working anything. Could you help me why it is happened ?
thanks

let humanScore = 0;

let computerScore = 0;

let currentRoundNumber = 1;

function generateTarget() {

let round = Math.floor(Math.random()*9);

return round;

}

const compareGuesses = (human,computer,targetNum) => {

const userG= Math.abs (human-targetNum);

const computerG= Math.abs (computer-targetNum);

return (userG <= computerG);

function updateScore (winner) {

if ('winner' === 'human') {

humanScore +=1;}

else

{

computerScore +=1;

}

}

const advanceRound = () => {

currentRoundNumber += 1;

}

I would start by fixing the indent(ion) of your code. Without proper indention, its difficult to see if you might have missed a closing (curly) bracket somewhere.

You might want to consult step 3 of this topic:

[How-to] Create a topic that everyone will read

on how to format your code, so the indention actually shows on the forum.

also, this:

if ('winner' === 'human') {

is wrong. These strings will never equal.

1 Like

thank you , let me do as you advised step 3 of this topic in order to be seen by other people.
You are right , i took out ’ ’ but still not working. I will put again my codes in right order

I think you miss a closing (curly) bracket somewhere. That is general causes the kind of error you are having.

This is why having proper indent(ion) in your code is so important, it prevents and makes it easier to solve this kind of error

1 Like
Hello, you are so right, i missed { and i just found it now. OMG this made me 1 hour spending. Now the code is working. I am very happy i can move on now as it is a week for this project. Thank you 
1 Like
Hello , Although there is no error anymore, the game is not operating yet and i don't know where I am wrong. I can put numbers in user but it is not showing ' who wins' ! Also , ' Next Round' box is not opening and moving. It was working yesterday. Can someone  please help ? 
also when i put some numbers in console.log(1,3,4), it is showing 'false' but it should be showing 'true' right ? 
![Screenshot 2021-01-29 at 11.47.10|690x353](upload://ch18w7gCC7r8nH0mUFZBhJtP0nY.png) 
![Screenshot 2021-01-29 at 11.47.10|690x353](upload://ch18w7gCC7r8nH0mUFZBhJtP0nY.png) 


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

function  generateTarget() {
let round = Math.floor(Math.random()*9);
return round; 
}
const compareGuesses = (human,computer,targetNum) => {
const userG= Math.abs (human-targetNum);
const computerG= Math.abs (computer-targetNum);
return (userG <= computerG);}

function  updateScore (winner) {
 if (winner === human) {
  humanScore +=1;}
else 
{
computerScore +=1;
}
}
const advanceRound = () => {
 currentRoundNumber += 1;  
}
onsole.log(generateTarget); 
console.log(compareGuesses(1,3,4));
console.log(updateScore); 

generateTarget and updateScore are function, but you never call/execute the function like you did with compareGuesses (missing parentheses)

now i called all 3 functions with console.log but still not operating in game.js ? How do i do to operate in game.js ?
also the lastfunction is showing error , i cannot think why ?
thanks

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

function  generateTarget() {
let round = Math.floor(Math.random()*9);
return round; 
}

const compareGuesses = (human,computer,targetNum) => {
const userG= Math.abs (human-targetNum);
const computerG= Math.abs (computer-targetNum);
return (userG <= computerG);
}


function  updateScore (winner) {
 if (winner === human) {
  humanScore +=1;}
else 
{
computerScore +=1;
}
}
const advanceRound = () => {
 currentRoundNumber += 1;  
}

console.log(generateTarget(6));
console.log(compareGuesses(3,3,6));
console.log(updateScore(2));


here:

console.log(updateScore(2));

what is 2? what provide this value as argument?

the error seems fair, here:

 if (winner === human) {

human is an undefined variable. You had this correctly as string, so then when the function is called and 'human' is provided as string, the human score gets updated, otherwise computer score gets updated

1 Like

I am so sorry , i still don’t get right the last function . I am aware that human is undefined variable and i didn’t put human as string in the code. So what should i do about this function ? And what should i do to operate the game ?

I personally don’t agree with the updateScore function, why a string is used. Given the player either wins (true) or loses (false), so I much rather had seen something like:

function updateScore(won){
   won ? humanScore++ : computerScore++;
}

updateScore(true); // updates humanScore
updateScore(false) // updates computerScore

But I suppose, using a strings works too…

I am sorry, when you are saying about ’ a string is used’ in updateScore function but i suppose i didn’t use string and i used only normal parameter without ’ ’ , right? By looking at your example, you are using Booleans so it is more or less similar my codes (if…else) ? Then why the game.js is not operating then? I have no clue.

function updateScore (winner) {

if (winner === human) {

humanScore +=1;}

else

{computerScore +=1;
}
}

winner is a the parameter, human is not. human is just an undefined variable. 'human' should be string within the body of the function

this way, when you call the function with 'human' as argument, the humanScore is updated else the computerScore is updated.

1 Like

Thank you for your explanation. As my game is not working, i reset all my works and started again from no-1-7. Then i read your notes and put ’ ’ in human which now the game is working. Here is my code from no 1-7. So i should put ’ ’ ( string) in all arguments inside the function?

Also although the code is working in game.js, in printing place, it increased only ‘1’ but is it not increasing more numbers ?

let humanScore = 0;

let computerScore = 0;

let currentRoundNumber = 1;

// Write your code below;

function generateTarget() {

let round = Math.floor(Math.random()*9);

return round;

}

const compareGuesses = (human,computer,targetNum) => {

const userG= Math.abs (human-targetNum);

const computerG= Math.abs (computer-targetNum);

return (userG <= computerG);

}

function updateScore (winner) {

if (winner === 'human') {

humanScore +=1;}

else {

computerScore +=1;}

}

const advanceRound = () => {

currentRoundNumber += 1;

}

updateScore('human');

console.log(humanScore);

updateScore('computer');

console.log(computerScore);

console.log(compareGuesses(3,6,1));

printing :
1
1
true

no. as a demonstrated earlier we could even used Booleans to achieve the same effect:

function updateScore(won){
   won ? humanScore++ : computerScore++;
}

updateScore(true); // updates humanScore
updateScore(false) // updates computerScore

the computer instead uses a string. but you could also use a constant variable instead:

function updateScore (winner) {

if (winner === 'human') {

humanScore +=1;}

else {

computerScore +=1;}

}

const humanWinner = 'human';
const computerWinner = 'computer';

updateScore(humanWinner);

console.log(humanScore);

updateScore(computerWinner);

console.log(computerScore);

Should more numbers be increased? If you want to increase further, you will have to call the function again.

the problem with this exercise is that there is more code involved (provided by the exercise), which every time you play a game, will call the functions you wrote.

1 Like

thank you very much for step by step explanation and much appreciate that.