Number Guesser Project

Hi there.

This project is giving me a hard time. is my first time coding so is already a challenge.
I am on Step 5. I run the code and I didn’t get the Target Number on the web preview show the number just “undefined”.
I add this line generateTarget(targetNumber); the number is showing now but I got error on the code.

/home/ccuser/workspace/independent-practice-js-number-guesser/script.js:10
generateTarget(targetNumber);
               ^

ReferenceError: targetNumber is not defined
    at Object.<anonymous> (/home/ccuser/workspace/independent-practice-js-number-guesser/script.js:10:16)
    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
 

Here is my code in full.

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

// Write your code below:

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

const compareGuesses = (userGuess, computerGuess, targetNumber) => {
const human = Math.abs(targetNumber - userGuess);
const computer = Math.abs(targetNumber - computerGuess);
if(human <= computer){
  true
}else{
  false
}
};
const updatedScore = winner => {
if (winner === 'human'){
  humanScore + 1;
} else if (winner === 'computer') {
  computerScore + 1;
}
};

Thank you,
Gus

The way you define your function, it doesn’t need any extra input, it just generates a random number.

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

So when you invoke it with generateTarget(targetNumber);, targetNumber is not necessary as a parameter (by your own definition of your function). If you wanted to store it in a variable targetNumber that would be different.

Example

const sayHi= () => {
  return "hi";
};

sayHi(hi); // <------ this would throw an error because hi is undefined
console.log(sayHi()) // < ----- this would log 'hi'
let greeting = sayHi() // < ------this would set greeting to be equal to the string 'hi'
console.log(greeting) // < ----- this would log 'hi'


Example with parameter

const saySomething = (word) => {
   return word;
}

let greeting = saySomething("hi"); 
console.log(greeting); // outputs 'hi'

@gusfavoreto676611631 just to highlight in this last example i put, when I put a (word) parameter, logically we expect function code will have that parameter used.

Thank you toastedpitabread.

I need to revisit the function section to understand better.
I added the targetNumber as a parameter for the generateTarget function.

const generateTarget = targetNumber => {
  return Math.floor(Math.random()*10);

Gus

It’s me again,

I did the step 6 but the score and the rounds didn’t change. We have the “winner” there but I didn’t see it anywhere else.

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

// Write your code below:

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

const compareGuesses = (userGuess, computerGuess, targetNumber) => {
const human = Math.abs(targetNumber - userGuess);
const computer = Math.abs(targetNumber - computerGuess);
if(human <= computer){
  true
}else{
  false
}
};
const updatedScore = winner => {
if (winner === 'human'){
  humanScore + 1;
} else if (winner === 'computer') {
  computerScore + 1;
}
};
const advanceRound = () => {
  return currentRoundNumber + 1;
};

I left some comments in the code. The examples I gave above are still relevant to these lines of code.

const generateTarget = targetNumber => { <-----
  return Math.floor(Math.random()*10); 
 /* targetNumber is not used in this code block so 
 it's not serving a purpose here */
};
const compareGuesses = (userGuess, computerGuess, targetNumber) => {
    const human = Math.abs(targetNumber - userGuess);
    const computer = Math.abs(targetNumber - computerGuess);
    if(human <= computer){
        true /* <---- this statement doesn't do anything, 
        for it to make a change you would have
        to return true or set something to "= true",
        same for below with false*/
     }else{
        false
     }
};

I will back study more and do more exercises for functions because I am no getting a fully understanding of how the code is working, or my logic is not that sharp, hehe. I don’t know how targetNumber will link to generateTarget.

O took off the targetNumber from the function. And add return true and return false to if/else, but is not working yet.

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

// Write your code below:

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

const compareGuesses = (userGuess, computerGuess, targetNumber) => {
const human = Math.abs(targetNumber - userGuess);
const computer = Math.abs(targetNumber - computerGuess);
if(human <= computer){
  return true;
}else{
  return false;
}
};
const updatedScore = winner => {
if (winner === 'human'){
  humanScore + 1;
} else if (winner === 'computer') {
  computerScore + 1;
}
};
const advanceRound = () => {
  return currentRoundNumber + 1;
};

Thank you,
Gus

Here’s an example of a simple use-case:

//defines random number function for 1-10
const generateTarget = () => {
  return Math.floor(Math.random()*10);
};

//defines a simple print function
function printValue(value){
    console.log(value);
}

// creates an instance of a random number, let's say it's 6
test = generateTarget()

//logs 6 to the console
printValue(test)

//output: 6

test2 = generateTarget() // this one picks 5
printValue(test2)
// output: 5
1 Like