Number guesser help

Now you should be able to pick the game.js in the opened file explorer -> js

You don’t need to edit anything in the game.js file for the project. You only have to write the functions described in the steps. From the instructions:

In this project, you’ll write four functions in script.js . We’ve provided some additional JavaScript code in game.js that will call your functions based on user interactions, but you don’t need to look at game.js and shouldn’t edit it if you want your project to work as intended

how do i get to the game.js file? i think that if i saw it it would help me understand this project better

i got into the inner workings of the game.js file and pressed save and this came up :
/home/ccuser/workspace/independent-practice-js-number-guesser/game.js:3
const humanGuessInput = document.getElementById(‘human-guess’);
^

ReferenceError: document is not defined
at Object. (/home/ccuser/workspace/independent-practice-js-number-guesser/game.js:3:25)
at Module._compile (module.js:571:32)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)
at bootstrap_node.js:542:3

Since there are only three units covered up to this point, there is nothing to be gained by opening the game.js file. The instructions are enough to guide us through this project. Close the tab and forget it exists, except remember that that is the script that the SCT is running to control the flow of the game.

One doubts the author (and possibly the engineers) ever intended for us to see that script. It’s one thing to teach a kid about pellets and BB’s but quite another to explain munitions.

We’re still at the pellets and BB’s stage, so keep your station and stay on course.

To wit:

We’ve provided some additional JavaScript code in game.js that will call your functions based on user interactions, but you don’t need to look at game.js and shouldn’t edit it if you want your project to work as intended.


Finally got around to checking out this project, and can see now why my earlier, speculative answers were not of much use. My bad.

Below is my own take on the instructions just to prove that if we follow them the program will work. Your code will definitely look different, but if it conforms, it too should work.

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

// Write your code below:
const generateTarget = () => Math.floor(Math.random() * 10);
const compareGuesses = (h, c, t) => Math.abs(h - t) <= Math.abs(c - t);
const updateScore = w => {
  switch (w) {
    case 'computer': computerScore++; break;
    default: humanScore++;
  }
}
const advanceRound = () => {
  currentRoundNumber++;
}
2 Likes

Consider also that we can reassign an object.

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

// Write your code below:
const floor = Math.floor;
const random = Math.random;
const abs = Math.abs;
const generateTarget = () => floor(random() * 10);
const compareGuesses = (h, c, t) => abs(h - t) <= abs(c - t)
const updateScore = w => {
  switch (w) {
    case 'computer': computerScore++; break;
    default: humanScore++;
  }
}
const advanceRound = () => {
  currentRoundNumber++;
}

here is the code that i made:

function compareGuesses(humanGuess, computerGuess, secretNumber) {
secretNumber = generateTarget();
if (humanGuess === secretNumber && computerGuess === secretNumber) {
return true;
} else if Math.abs(humanGuess - secretNumber) > Math.abs(computerGuess - secretNumber) {
return false;
} else {
return true;
}
}

how can i make it work and improve it? it got this error message:
/home/ccuser/workspace/independent-practice-js-number-guesser/script.js:17
} else if Math.abs(humanGuess - secretNumber) > Math.abs(computerGuess - secretNumber) {
^^^^
SyntaxError: Unexpected identifier
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)

i fixed the code so it does not have errors, yet even though it works it does not run it to the html.
function compareGuesses(humanGuess, computerGuess, secretNumber) {
secretNumber = generateTarget();
if (humanGuess === secretNumber && computerGuess === secretNumber) {
return true;
} else if (humanGuess < secretNumber) {
return true;
} else {
return false;
}
}

Could you please post your entire code using the </> button in the menu bar above where you type your post? Click the button, then paste your code in the space indicated, so we can see it in a similar fashion to how it appears in the Codecademy editor. The function you’ve posted appears incomplete, but if you’ll post all of your code, I can see for sure.

okay:

function generateTarget() {
  randomTarget = Math.floor(Math.random()* 10);
  return randomTarget;
}

function compareGuesses(humanGuess, computerGuess, secretNumber) {
secretNumber = generateTarget();
var userDistance = Math.abs(humanGuess - secretNumber)
var computerDistance = Math.abs(computerGuess - secretNumber)
if (humanGuess === secretNumber && computerGuess === secretNumber) {
return true;
} else if (userDistance < computerDistance) {
return true;
} else {
return false;
}
}

These 2 functions appear to be okay. Have you written the updateScore() or advanceRound() functions?

do you mean i need to write these two functions for the compare guesses function to make it run on the html?

Yes. The game isn’t functional in the browser window until all 4 functions are complete. You should be able to click on a few things, but the game won’t display the results of who won until you finish the updateScore() function, and you can’t play the next round without the advanceRound() function.

2 Likes

Bottom line, this project has us create four functions. These four functions will be called from the game controller, game.js, as mentioned in the narrative. All we need to be sure of is that our functions return or do what is specified in the instructions.

Don’t try to play the game until all four functions are complete. For all intents and purposes concentrate on the narrative and instructions, and the functions you write for each step; do this without looking at game.js.

Only when you have the program working as expected should you consider looking at the two support files, index.html and game.js. It will be a sneak peek at what is to come, so don’t give more than a cursory inspection. In a few weeks you will be able to understand the whole thing.

2 Likes

Just noticed a mistake in your compareGuesses() function. See the comment I added. You don’t need to assign a value to the secretNumber variable. It will already have a value assigned from the arguments passed by the function call coming from the game.js file.

Recommended…

let userDistance = Math.abs(humanGuess - secretNumber)
let computerDistance = Math.abs(computerGuess - secretNumber)

Agreed. I had only copied the op’s code. Figured we’d save the const, let, var discussion for after the project is working :slightly_smiling_face:

1 Like

@midlindner exactly what I said ;). I don’t know the exercise but this got my attention.

1 Like