Number Guess project review and question about the challenge

I’d like a review of my code, and also check if there’s anything to improve.
My code for the project

I’d like to also know how to do this, I’m having a mind block on this one, since I can’t think of any way to do this one… I’m probably tired atm, but I would love to understand how to do this challenge part.
Screenshot 2020-12-09 154151

Hey @kazenshi! Hope you’re doing well.

For what my opinion is worth, your code looks good and is easy to read. I especially like how concise your functions are, skipping over unnecessary variables, and getting right to the return statement. That’s something I definitely will take from. The one suggestion I would make is pushing down the closing curly brace on your arrow functions to the next line, like so:

const generateTarget = () => {
    let randomNum = Math.floor(Math.random()* 10);
    return randomNum;
};

For me, this makes it easier to read.


As to the last step, I’m not sure if you mean the first or second part, but I’ll address my take on both:

  • The getAbsoluteDistance() function is really just a matter of replacing the beginning of the compareGuesses() function with new functionality. I don’t really see how it’s that vast of an improvement they’re suggesting, but I think the idea is that if things were more complicated, bringing in functionality from another function would simplify things. This is the best thing I could think of:
const compareGuesses = (humanGuess, compGuess, targetNum) => {
    const compV = getAbsoluteDistance(targetNum, compGuess);
    const humanV = getAbsoluteDistance(targetNum, humanGuess);
     //instead of:
    const humanNumber = Math.abs(targetNum - humanGuess);
    const computerNumber = Math.abs(targetNum- compGuess);
  • Now as for the second part of their suggestion, that’s a bit more involved. The only thing I could think of was to implement their suggestion in the other javascript file, game.js. Not sure if you did this project on Codecademy, or off-platform, but you can access that other js file if you’re doing the latter. Now the instructions kind of throw it at you out of the blue, but Window.alert() is a built-in javascript function all on its own (see what I found on the web here: https://developer.mozilla.org/en-US/docs/Web/API/Window/alert). The reason that I chose to edit game.js is because this file already makes reference to the user’s guess in the html file which is what we would need to do to reference it in any functional way on the website. I tried to just copy over the getelementbyid statements to just do the same thing in the file we’re already working in (script.js), but unfortunately it messed with the game, and I’m not quite experienced enough yet with integration between html and javascript to solve it. So in the end, I added a simple if statement to game.js:
// Alert if player's guess is outside the appropriate range
  if (currentHumanGuess > 9 || currentHumanGuess < 0) {
   alert("Please enter a number between 1 and 9!")
  };

I fiddled around with this for a while, and am not sure why Window.alert() didn’t work, but alert() did. If any more experienced developers can explain why, I’d love to know that…

Anyway, I hope this helped!

-Per53us

1 Like