NEED HELP! I started on the JavaScript projekt "Number Guesser"!

Hi guys,

I’m a totally newbie into coding. It’s hard but i love it! - I’m just started the “Number Guesser” project and i’m stuck at task number 3.

Here’s the task:

Create a generateTarget() function. This function will be called at the start of each new round in order to generate the new secret target number.

***This function should return a random integer between 0 and 9 .

My answer so far is: const generateTarget(Math.floor(Math.random() * 10));

But i’m not sure that’s the right way to put it?

Furthermore task number 4 says:

Create a compareGuesses() function. This function will be called each round to determine which guess is closest to the target number.

This function:

  • Has three parameters representing the user (human) guess, a computer guess, and the secret target number to be guessed.
  • Determines which player (human or computer) wins based on which guess is closest to the target. If both players are tied, the human user should win.
  • Return true if the human player wins, and false if the computer player wins.

My question to you guys is how would you write the first block of code and why so?

My second question is how would you write the code block for task 4 and why?

Hope some of you got the time to help a fellow coder who’s completly blank at the moment!

best regards.
Andreas.

A code block is any statement written inside curly braces.

{
    // statement or statements
}

That is valid syntax but does not have much meaning since the code executes immediately upon encounter. Handy for pasting code snippets into the console, though. Something for another discussion.

Code blocks appear in several places in JavaScript, such as:

  • for
  • while
  • if
  • switch

and most importantly,

  • function
for (initial, while condition, increment) {
    // statements
}

while (condition) {
    // statements
}

do {
    // statments
} while (condition)

if (condition1) {
    // statements
} else if (condition2) {
    // statements
} else {
    // statements
}

switch (expression) {
case expression: // statements
// ...
default: // statements
}

// and finally,

function funcName (parameters) {
    // statements
    return // value
}

See if you cannot glean some insight from these syntax examples and rethink your code.

3 Likes

Thanks for the reply! It was a big help!