Can you help me fix my high score function in a simple game I made

here is the code:

function generateRandomNumber(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;

function playGame() {
  const targetNumber = generateRandomNumber(1, 100);
  let attempts = 0;
  let guessedNumber = 0;
  let highScore = Infinity; // Initialize high score as infinity

  while (guessedNumber !== targetNumber) {
    guessedNumber = parseInt(prompt("Guess a number between 1 and 100:"));

    if (isNaN(guessedNumber)) {
      alert("Invalid input. Please enter a valid number.");
      continue;
    }

    attempts++;

    if (guessedNumber < targetNumber) {
      alert("Too low! Try again.");
    } else if (guessedNumber > targetNumber) {
      alert("Too high! Try again.");
    } else {
      alert("Congratulations! You guessed the correct number in " + attempts + " attempts!");

      if (attempts < highScore) {
        highScore = attempts;
        alert("New high score! Your current high score is " + highScore + " attempts.");
      }
else {alert("The least attempts used " +highScore+ "attempts!")
//when you don't achieve a new score it still says you did.
}
    }
  }
}

I had never seen infinity in javascript before, so I did a quick search. It looks (to me) like a bogus implementation and is an overflow value which breaks all comparisons.

You can try and use this instead

let highScore =  Number.MAX_VALUE;

Or just set it to a ridiculously high number

let highScore =  9999; 

I would prefer to initialize Highscore with null and do a slightly change on the code

if (!highScore || attempts < highScore) {
3 Likes

Thank you!

Could also… yaknow… properly close your GenerateRandomNumber function.

Also… the problem has nothing to do with the value assigned to highScore, and everything to do with WHERE it’s assigned.

Consider what happens when you try to play the game a second time.

2 Likes

I believe m_hutley is referring to lexical scoping.

A couple of examples for you.

Start reading from inside of the inner function. The inner function is trying to console.log an ‘x’ value, but the function doesn’t have an ‘x’ of it’s own.

// global execution context

// 3. x is found here, inner can use this ↓
let x = 5;

function outer () {
  // 2. outer doesn't have 'x' either
  // so the search continues to the parent global context ↑

  // Start here!
  function inner () {
    // 1. inner doesn't have an 'x'
    // so looks to it's parent outer function for it ↑
    console.log(x); // 5
  }

  inner(); // outer calls inner
}

outer(); // 5

More applicable to your task, you can make changes to the outer or scoped variables from inside of a function.

// global execution context

let counter = 0; // global counter found here ↓

function increaseCounter () {
  // cannot find 'counter' in here
  // so searches it's parent global context ↑
  counter += 1; // changes the global counter
  console.log(counter);
}

increaseCounter(); // 1
increaseCounter(); // 2
increaseCounter(); // 3

// global counter has been changed by increaseCounter
console.log(counter) // 3

Note: For a simple game this is probably fine, but using globals is not good practice and can make it difficult to debug errors, especially in larger programs. The next step might be to explore closures.

Oh and yes, as m_hutley pointed out your function needs closing

function generateRandomNumber(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
} // ← missing this closing curly brace
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.