Challenge Project: Number Guesser

Hello,
I can’t find out why the Round Number and Scores won’t change with each respective guess.

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

// Write your code below:
function generateTarget() {
  return Math.floor(Math.random() * 10);
}

let hg
let cg
let stg = generateTarget
function compareGuesses(hg, cg, stg) {
  let firstDif = stg - hg
  let seconDif = stg - cg 
 if (Math.abs(firstDif) < Math.abs(secondDif)) {
  return true
 } else if (Math.abs(firstDif) > Math.abs(secondDif)) {
  return false
 } else if (hg == cg) {
  return true
 }
}

function updateScore(winner) {

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

function advanceRound () {
  ++currentRoundNumber ;
  //return ++currentRoundNumber//
  console.log(++currentRoundNumber) ;
  
}

It appears that you’re not calling any of the functions, meaning none of the code within them will run—are you calling these functions in a separate file, or within some HTML code?

It’s been many years since doing this exercise, but as I recall, the listeners are provided, as well as the HTML/CSS. If we can track down a link to the exercise we can take a closer look.

2 Likes

Ahh; good point! @byte6656439851, if you could provide us with a link to the exercise, we’d be more able to help!

2 Likes

My apologies. Here is the link:
https://www.codecademy.com/journeys/full-stack-engineer/paths/fscj-22-building-interactive-websites/tracks/fscj-22-javascript-syntax-part-i/modules/wdcp-22-number-guesser-75517b97-cc5e-4579-a3ad-898b87826534/projects/number-guesser-independent-practice

My apologies. Here is the link:

https://www.codecademy.com/journeys/full-stack-engineer/paths/fscj-22-building-interactive-websites/tracks/fscj-22-javascript-syntax-part-i/modules/wdcp-22-number-guesser-75517b97-cc5e-4579-a3ad-898b87826534/projects/number-guesser-independent-practice

1 Like

Hello! It appears that you’re not calling the generateTarget() function, but rather assigning the function itself to stg, here:

(Note what happens when you console.log(stg).)

Similarly, note how you’ve spelt secondDif when defining it here:


Once you’ve changed those, you may notice something odd; the round numbers are increasing by two each round, not one! That’s because you’re incrementing the value of currentRoundNumber twice in the advanceRound() function:

Even though the second incrementing is within a console.log(), it still actually adds one to the currentRoundNumber variable.

I hope this helps!

3 Likes

Thank you for these very good corrections…especially about the incrementing!

2 Likes

Aside

For best results, once you pass the required tests, copy all of your code, and the provided code over to your local environment. Notepad++ is enough software to have installed on your computer. Create the folder structure and run the code offline.

Create a folder in your User Documents folder, and use that as the Root of all your projects.

C:\\Users\%user\Documents\project_root_folder\

PRF would be just as adequate for the purpose. The point being to keep our projects in a safe storage environment that doesn’t conflict with other user operations of our machine.

Given that we now have a ‘gated community’ where we can store our projects, it falls to us to name the projects within.

Also given in our gated community is relative path addressing. We never have to, nor should, call the volume root (C: D: E: …). Everything is based on the location of the index file and the css file (and possibly the JS file).

Plan and build in these terms and your local code will be portable to external locations.

1 Like

Who can say what kind of projects we will embark upon as the learning unfolds? This root folder is going to be where they all will reside, regardless of language. For that reason I would suggest a, 'sites` folder where all of your HTML/CSS/JavaScript content will reside.

PRF/sites/
sites/
+ %site/    <= the site root
  + css/
    + skin/
      - bgd.png
      - logo.svg
    - style.css
  + images/
    - picture.png
  + js/
    - script.js
  - favicon.png    <= the only image file in the site root
  - index.html
  - robots.txt
<link rel="icon" type="image/png" href="favicon.png">

I made some changes to my code to see the alert() functionality but I am not seeing an alert box pop up . Can you help me fix what is wrong with my code?

https://www.codecademy.com/journeys/full-stack-engineer/paths/fscj-22-building-interactive-websites/tracks/fscj-22-javascript-syntax-part-i/modules/wdcp-22-number-guesser-75517b97-cc5e-4579-a3ad-898b87826534/projects/number-guesser-independent-practice

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

// Write your code below:
function generateTarget() {
  return Math.floor(Math.random() * 10);
}

let hg
let cg
let stg = generateTarget()
let firstDif = stg - hg
let secondDif = stg - cg 
function compareGuesses(cg, hg, stg) {
  function getAbsoluteDistance (firstDif,secondDif) {
 return firstDif & secondDif;
} 
 
 if (Math.abs(firstDif) < Math.abs(secondDif)) {
  return true
 } else if (Math.abs(firstDif) > Math.abs(secondDif)) {
  return false
 } else if (hg == cg) {
  return true
 } else if (hg > 9 || hg < 0) {
  alert("Bro, your number is out of range.  Choose a number between 0 and 9") ;
 }

}

function updateScore(winner) {

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

function advanceRound () {
  ++currentRoundNumber ;
    }