Number Guesser Challenge Project (JavaScript)

let humanGuess = humanGuess - targetNumber;

We cannot re-declare the parameter let alone reference it in the same statement it is being declared in. All this means is remove, let from that reassignment statement and all will be well, we would hope.

1 Like

I just realize that i’ve been using arrow function a lot? is it okay?

I have a question. This is the first part of my code and it seems to not produce the correct output of true and false that it should. Can you check it out and tell me why?

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

// Write your code below:

// 1:
generateTarget =()=> 
Math.floor(Math.random() * 10);
// test 1:
console.log(generateTarget())

// 2:
function compareGuesses (userGuess, compGuess, targetNum) {
  if (Math.abs(userGuess - targetNum) < Math.abs(compGuess - targetNum)) {
    return true;
  } else if (Math.abs(userGuess - targetNum) === Math.abs(compGuess - targetNum)) { 
    return true;
  } else {
    return false;
  }
}

// test 2:
console.log(compareGuesses(4, 7, generateTarget()));

Hi

Please see my code below. I reviewed the solution code and my compareGuesses function is quite different to that in the solution code.

I’d really appreciate if members of the community could review my code, point out any errors an explain why the solution code is better.

many thanks in advance.

Hello, @dannygoler6382924880, and welcome to the forums.

I ran your code, and it seems to return true or false as expected. My only suggestion would be to consolidate your if and else if into a single conditional. Could you not combine < and === ? Also, consider your concise body arrow function that you used for generateTarget (really should preface that with const, but that aside), you could do something similar with your compareGuesses function. Consider what is returned from the following function:

//we don't actually need to literally return true or false
//we can return the expression
//it will be evaluated, and the result returned:
const lessThanOrEqual = (param1, param2) => param1 <= param2;

console.log(lessThanOrEqual(1, 2)); //true
console.log(lessThanOrEqual(3, 3)); //true
console.log(lessThanOrEqual(5, 2)); //false

Just suggestions. Happy coding!

1 Like

From your code:

Other than my comment to your code above, you might consider the suggestions I made to @dannygoler6382924880 above.

2 Likes

Thank you so much @midlindner, this is actually super helpful. Deeply appreciate it.

1 Like

My Solution,

let humanScore = 0;

let computerScore = 0;

let currentRoundNumber = 1;

// Write your code below:

const generateTarget = () =>{

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

}

const compareGuesses = (human, comp, target) =>{

const result = Math.abs(target-comp) - Math.abs(target - human);

return (result >= 0 ? true : false);

}

const updateScore = (winner) =>{

winner === ‘human’ ? humanScore++ : computerScore++;

}

const advanceRound = () =>{

currentRoundNumber++;

}

This is how I did it: https://gist.github.com/48fe5f29b538d1f4c4b6a5dbed11dbb9

Long I was wondering why it didn’t work and then I saw that I wrote generateTarget not as a Arrow function but simple as variable. I’m honestly still not sure why it didn’t worked. I stored the formula to create a random number inside the variable, why didn’t it work?

Hi @sarmstead01. I came to a similar conclusion, but couldn’t figure out how to stop the rest of the game from executing after clicking out of the alert. How can one cease execution after the alert and reset the guessing button so that the round doesn’t complete and the player is forced to put in a valid number to continue?

https://rantoskonstantinos.github.io/numberGuesserGame/ (the actual website for the “game”)
https://github.com/RantosKonstantinos/numberGuesserGame (the source code)

I would be gratefull if you could point me to a direction about the last 2 steps.
If you have any recommendations, make a pull request.
Thanks!!!

Hello! I did this and it works, although I find their solution is much nicer, shorter and smarter in general.

Hello,

I guess my question starts around line 14 in my code. The directions ask to return true and false, but I noticed the sample answer does not do that. So line 12 of the solution show 'return humanDifference <= computerDifference, does this then return a true/false answer?

Also I see I should have used ++ to increase the integers, but does added 1 to it also work?

Lastly can someone explain how the advanceRound function is working to increase the round.

First time posting here so hopefully the link works.

Anytime we write an expression as the return value, the expression is evaluated and the outcome is what is returned. A comparison yields a boolean outcome, so yes the return value will be true or false.

Thank you. That makes sense.

1 Like

Hi everyone,

First post here on the Codecademy discuss forums. I am learning JavaScript but have some mixed programming background t-sql, C#, PowerShell etc.

I would like some constructive feedback on my code. The preetified syntax was made to make the code easier to read and of course deviate from the structure taught by the JS course. The Alert() function should be in game.js however I considered that to be outside of the scope of the assignment and kept it in script.js.

I recommend checking the code out on GitHub if you want to see the correct code structure I used.

My solution. In all transparency I did have to peek at the solution because I got stuck on the compareGuesses function.