r/javaScriptStudyGroup • • Nov 09 '17

Anyone know how to do this javascript assignment? Need of help

I need help with a javascript assignment. If anyone knows how to do this, then it would be highly appreciated. I am completely lost in this. Thank you.

Develop a number guessing game using Javascript and simple HTML. The computer will select a number between 1 and 20. The player will have five tries to attempt and guess the correct number. The program will need to accept input (the player's guess) through a text field and trigger the program when the user presses a submit button. The output should display on the document object via innerHTML, textContext, or other similar method. Use your own code and do not use images from existing programs on the Web. You will be graded down heavily if you do not use your own work.

The HTML5 document will need to validate for HTML5. This does not mean you need to use HTML5 semantic tags explicitly, but the HTML5 document will need to validate the W3C validation. https://validator.w3.org/ (Links to an external site.)Links to an external site.

We will have an opportunity to build parts of the number guessing game during class exercises.

Details:

An html document and an external Javascript file. You may embed some of your Javascript into the page as necessary, but the majority of your code should be in an external file.

The computer will create a random number between 1-20. The game should start with a web page that has a text input field that asks the user to guess a number between 1-20.

The game will evaluate the user's guess and determine if it is less than, equal to, or greater than the computer's number. If it is equal to the computer's number than the user wins and the game ends.

The user will be allowed five guesses. After that point then the computer wins and the game ends. You can end a game by causing a submit button to be disabled.

Accept the input by using input text fields and buttons. Output should appear to the document object via innerHTML or other similar method.

Remember that if the player has selected the correct number, or if the player has exceeded five guesses, then the game will end. To show an "end" You will want to write text to the document object saying that the game is over.

Comment the Javascript code to describe how it is working. Include your name and date of completing the project at the top of your Javascript comments. Use your own code and do not use images from existing programs on the Web.

1 Upvotes

1 comment sorted by

1

u/atreyoukilis Nov 09 '17

I'll give you some pointers to get started:

  • Try using Math.random to pick the number, you will have to multiply the result by 20 however since Math.random picks a number between 0 and 1. I would probably do something like having this function run on page load, and then saving it as a variable.

  • As for the user's input, try using something like document.getElementById('input_id').value to access the number the use has entered in the input box after they submit the input.

  • The user's input should then be compared to this number, with some sort of counter in your javascript file keeping track of the user's guesses. I'll let you work out your own logic here, but the counter should increase each time the user presses the 'submit' button. I would also probably give the use certain prompts based on the number of guesses - try using the css attribute "visibility" to show and hide various elements.