In school and trying to create the classic guess a number game. What did I do wrong?

Ronald Cross :

This code is supposed to be a guess the number game. The object is to get JS to display an alert box that asks the user to guess a number. When they guess a number, the code is supposed to return the values of what they have guessed until they get it correct.

When I run the play key, an alert box does pop up, and says:

"Enter a guess between 1 and 100"

After that, even if I enter all 100 numbers, the box re-loads forever.

Here's what I have:

const game = {
  title: 'Guess the Number!',
  biggestNum: 100,
  smallestNum: 1,
  playerChoice: null,
  secretNum: null,
  //1. Add a prevGuesses property to the game object initialized to an empty array.
  prevGuesses: [],
  //2. Add a getGuess method to game that prompts the player to enter a guess with a message formatted as: Enter a guess between [smallestNum] and [biggestNum]: Hint- Use a template literal for the prompt message.

  //3. Ensure that the getGuess method returns a value that is:
  //-is a number, not a string
  // -is between smallestNum and biggestNum, inclusive
  // -Hints: This is a great use case for a while loop. parseInt returns NaN if the string cannot be parsed into a number
  getGuess: function() {
    while (this.playerChoice !== this.secretNum) {
   var playerChoice = prompt(`Enter a guess between ${this.smallestNum} and ${this.biggestNum}: `);
    var wholeNumber = parseInt(this.playerChoice);
    if (this.wholeNumber > this.biggestNum) {
      var wholeNumber = 100;
    } 
    if (this.wholeNumber < this.smallestNum) {
     var wholeNumber = 1;
    }
    if (typeof this.wholeNumber !== "number")) {
      var wholeNumber = Math.floor(Math.random() * 100)
    }
   } 
  },
//4. From within the play method, invoke the getGuess method and add the new guess to the prevGuesses array
    play: function() {
    this.secretNum = Math.floor(Math.random() * 
      (this.biggestNum - this.smallestNum + 1)) + this.smallestNum;
      game.getGuess();
     this.prevGuesses.push(this.wholeNumber);
        //6 the play method should end(return) when the guess matches the secretNum
      if (typeof(this.wholeNumber) === "number" && this.wholeNumber !== this.secretNum) {
        game.render();
      }
   
      
  },
  // 5. Add a render method to game that play will call after a guess has been made that alerts:
// -if the secret has been guessed: "Congrats! you guessed the number in [x] guesses!"

// otherwise

// -"Your guess is too [high|low]"
// "Previous guesses: x, x, x, x"

// Hints: render wont be able to access any of play's local variables, e.g., guess, so be sure to pass render any arguments as needed. Template literals not only have interpolation, they honor whitespace - including line breaks! The list of previous guesses can be generated using the array join method.
    render: function() {
      if (this.wholeNumber === secretNum) {
          alert(`Congrats! You guessed the number in ${game.prevGuesses.length} guesses!
          Previous guesses: ${prevGuesses}`);
      } else if (this.wholeNumber > this.secretNum) {
          alert(`Your guess is too high!
          Previous guesses: ${this.prevGuesses}`);
          game.guess();
      } else if (this.wholeNumber < this.secretNum) {
          alert(`Your guess is too low!
          Previous guesses: ${this.prevGuesses}`);
          game.guess();
      }
    }
  
};


game.play();
/*
Allow the player to continually be prompted to enter their guess of what the secret number is until they guess correctly
If the player has an incorrect guess, display an alert message that informs the player:

Whether their guess is too high, or too low, and...
A list of all the previously guessed numbers (without showing the square brackets of an array)
If the player has guessed the secret number:

Display an alert message that congrats the player and informs them of how many guesses they took
End the game play*/

/*


Bonus: when play is run, immediately prompt the player to enter the smallest and biggest numbers instead of having them pre-set*/

The research that I've done: I've googled as many pages as I could find and youtube tutorials on functions, play object, loops within arrays, anything that I could think of that's related to the types of objects in this code.

the comment text is from the assignment. While I was working on it, I put the comments next to the code that I was working on.

I really am only a few weeks into coding and appreciate any feedback.

This is the original code that I was provided:

const game = {
  title: 'Guess the Number!',
  biggestNum: 100,
  smallestNum: 1,
  secretNum: null,
  play: function() {
    this.secretNum = Math.floor(Math.random() * 
      (this.biggestNum - this.smallestNum + 1)) + this.smallestNum;
  }
};
trincot :

There are several issues with your code:

  • this.wholeNumber never receives a value, so that will not work. Also, it does not seem the purpose of this exercise to have such a property. You should not have to store the user's guess in a new property this.wholeNumber. See next point:
  • The goal of getGuess is to return a number, but there is no return statement. You seem to want to store the number in a property, but that is not the assignment.
  • Related to this: in play you should get the guessed number as a return value from the call to getGuess
  • The goal of getGuess is not to wait for the user to guess right, only for the user to make a valid guess (within range and not NaN). So the condition of your while loop is not correct.
  • getGuess does not verify that parseInt returned NaN. Note that the type of NaN is "number", so the last if's condition will never be true. You should use the isNaN function, or better Number.isNaN.
  • play should always call render, not only when the guess is right.
  • render should be called by passing it an argument (the guessed number), and thus the render method should define a parameter for it.
  • The render method should not call game.guess(). That is not its job.
  • The play method should not put a new value in secret, otherwise the user will be playing against a moving target :) The secret should be set once in the main program
  • The main program -- where you currently just have game.play() -- should have the looping logic, which you had tried to implement elsewhere. Here is where you should repeat calling game.play() until the guess is right. It is also here where you should display alert messages about guesses that are too low, too high, ...etc.
  • There is no clear instruction where the main program should get the latest guess from. It can get it from game.prevGuesses, but I would suggest that the play method also returns the latest guess, just like getGuess should do.
  • Take note of the other instructions that are given in comments for the main program. For instance, when the user has guessed right you should alert after how many attempts they got it right. Think of which property can help you to know this number of attempts. Hint: length.

I will not provide the corrected code here, as I think with the above points you have enough material to bring this exercise to a good end. Happy coding.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Trying to create an array in Java. What did I just do?

I was trying to access "UserController@edit" but it doesn't found, what did i do wrong?

What did I do wrong with mxGetPr

What did i do wrong in this JavaScript code

What did I do wrong? Trying to get user to enter numeric values for different variables

I don't know what to do with my Number Guess Code

I am trying to read the JSON file and print the values of it, but I am having some errors. What did I do wrong?

What did I do wrong when I installed this theme?

Trying to create n number of Tasks and run them in parallel. What am I doing wrong?

I am trying to create a c program to get the factors of a number with array , what is going wrong?

What did I do wrong in my dual drive partitioning?

It always says Correct! What did I do wrong?

What did I do wrong in OOP in C++

While (true) problems, what did i do wrong?

My quickSort is not working correctly. What did I do wrong?

Project Euler: Largest palindrome (Python). What did I do wrong?

What did I do wrong with my 301 redirects?

What did I do wrong ....? ( calculating the amount of cupcakes per person )

program does not work, what did I do wrong?

What did I do wrong? UPDATE with condition and LIKE postgres

Guess the number game

Guess the number game Python

Guess that number game

Implementing "Guess the number" game

random number generator, number guess. What is wrong with my code?

Hi! I am trying to center this code but it is not centering. I can not figure out what I did wrong

Wrong number of commits in Git? What do I do wrong?

I created cpt in WP and did not show the taxonomy, what did I do wrong?

button triggers frame.repaint() even when I did not tell it to. What did I do wrong?