Number Guesser Code

I am trying to code the Number Guesser for independent practice - numberGuesser
I have written the code as I believe it should be and am trying to check it. on the codecademy page it has a browser that updates when I save the code but it hasn’t changed.

I did download VS Code I followed the article about setting up a text editor for web development and the video. I tried to put my code into the editor and still haven’t found out how to check this. What am I missing? My code is below. Thanks!

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

// Write your code below:

const genertateTarget = () => {Math.floor(Math.random() * 10)};


const compareGuesses = (humanGuess, computerGuess, secretTarget) => {
if (Math.abs(humanGuess - secterTarget) <= Math.abs(computerGuess - secretTarget)){
  return true;
} else if (Math.abs(humanGuess - secterTarget) > Math.abs(computerGuess - secretTarget)){
  return false;
}
}
const updateScore = (winner) => {
if (winner === 'human') {humanScore ++} else if (winner === 'computer') {computerScore++}
};

function advanceRound(){currentRoundNumber += 1}

this project contains more files, in the lesson you see a directory/folder icon:

image

once you click that, you can see the other files. You can download/copy paste them, then you should be able to open the html file locally and project hopefully runs.

if you run into issues you need help with, can you share the the whole directory with the project through google drive/dropbox or something else you prefer? Then i can help you further a lot faster.

  if (Math.abs(humanGuess - secterTarget) <= Math.abs(computerGuess - secretTarget)){

Is such an insane line, split it up:

  if (humanDist <= compuDist) {
    return true
  } else {
    return false
  }

Except, you would never return true if true and false if false, you would return the value itself:

  return humanDist <= compuDist

Same here:

if (winner === 'human') {humanScore ++} else if (winner === 'computer') {computerScore++}
  if (winner === 'human') {
    humanScore++
  } else if (winner === 'computer') {
    computerScore++
  }

But since they’re exclusive to each other then there’s no reason to test the other:

  if (winner === 'human') {
    humanScore++
  } else {
    computerScore++
  }

This was actually the original way I wrote the compareGuesses function… Then I looked on other threads and changed it to what others had coded… I was trying to get something to happen. obviously I am missing the way to actually test the code. Thanks for the confirmation of my original idea to write the code simple!

Did you read my answer as well? Where i explained this:

Thanks for the response. I am still unsure how to do this. I created a project folder in VS and then created four files. I named them all the same as on the project page and copied and pasted the code from each. I tried to find out how to run it (open the html file) but couldn’t get it to work. I was unsure what the ‘whole directory’ meant but I uploaded the project folder with the files to google drive. Here is the link - VSC Number Guesser Thanks again!

that’s what browsers do. the address bar is literally a file path to an html file

the html file in turn refers to other files, and the browser will read those as well from the same location (which is why keeping them in the same directory makes them available to the browser)

also, you only uploaded a 113 byte text file

If the problem is that the number isn’t updated then a good idea would be to test your functions to see if they’re well behaved.
What you posted isn’t.

If you load the html in the browser then you can call your functions from your browser’s console. you’d also be able to see any error messages in the console.

codecademy’s environment doesn’t show error messages from what I can tell, so they would get silently ignored while nothing seemingly happens - definitely a good idea to run things locally instead

You can try your code here too:
https://repl.it/repls/AdoredVioletMaintenance
(I already pasted it in, plus added a call to your function at the bottom. if you press RUN, there’ll be an error)

i don’t use VScode, can’t you share the directory/folder with a something that we can open it with an editor of our own preference?

I guess this is all over my head. I just started 3 days ago thanks.

surely the files are stored somewhere in a folder/directory on your computer? You can upload this directory to dropbox or google drive for example, then we can access it as well

the file you currently upload does not contain the project/files.

Number Guesser

one question; do the parameters (humanGuess, computerGuess, secretTarget) need to be defined somewhere. I am a bit confused. thanks for any help.

check the function lesson:

https://www.codecademy.com/courses/introduction-to-javascript/lessons/functions/exercises/parameters?action=resume_content_item

which explains parameters and arguments.

Hey thanks, ionata! I have tried to update my code as you explained. I was able to open the html file in my browser from VS and the number counter still looks the same with the question mark on the computer side. Does this mean some of the code in the other files was somehow erased? I don’t see any errors yet I don’t see anything working. When I ran it in the link you provided it returns => undefined… any help is appreciated! Here is my new code:

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

// Write your code below:

const genertateTarget = () => {Math.floor(Math.random() * 10)};


function compareGuesses(humanGuess, computerGuess, secretTarget) {
  const humanVariant = Math.abs(humanGuess - secretTarget);
  const computerVariant = Math.abs(computerGuess - secretTarget);
  
if (humanVariant <= computerVariant){
  return true;
} else {
  return false;
}
};

const updateScore = (winner) => {
if (winner === 'human') {
  humanScore ++
} else {
  computerScore++
}
};

function advanceRound(){currentRoundNumber += 1}

I downloaded your 4 files, replaced the content of script.js with what you posted last.

Then, I ran firefox (chrome is fine too, no difference for this purpose), told it to open index.html

I opened firefox’s console.

Clicked the “Make a Guess” button (the big blue one)

That caused an error to show up in the console:

(the last error, ignore the first error/warning, not relevant)
ReferenceError: generateTarget is not defined
(and your code does indeed not define this, it should though)

console.log will also send text to that console, so you can use it to see if your function gets called etc

So you’d fix that (another spelling mistake), save the file, refresh the page, and click the button again and see if something shows up in the console or if anything happens on the page.

You might get another error
You might see the page updating in some way
You might see nothing

If you see nothing then you may want to start inserting console.log calls in your code to get an idea of what did and didn’t happen.
If it’s an error, read it and try to make sense out of it.
If the page updated, did it do its job? Otherwise, more console.log’s

To determine whether your code ran at all you could add a console.log call outside all functions, since that part of your code should run as soon as it the browser reads it.

You can also call your functions / look at variables from the console.

I had it defined but when I would run/save it in codecademy it would come up with the error saying that secretTarget is already defined… confusing.

If you do this:

let a = 2
let a = 2

Then you’d get an error for trying to create a variable where one already exists, maybe you did that with secretTarget.

But you fixed that spelling mistake in the last one.
There’s still the generateTarget spelling mistake.

After that, it still won’t set the target, and then you might test your function, and if you do, you would discover that generateTarget doesn’t return anything

after that I think I was able to make guesses and get comparisons to happen


Hey there, I am also having similar problems with the Number Guessing Code. Since I didn’t get the code to execute as intended I looked up the solution to see, what I did wrong. Yet my code (on the left side) looks almost identical to the solution (right side in the picture above) and I am lost as to what is missing. Can you help me?

looking at screenshots of code is dull. What error did you get? Can you copy paste your code so i can actually run it?