Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8317973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:47:40+00:00 2026-06-08T21:47:40+00:00

I have created this javascript which generates a random number ranging from 0-20. What

  • 0

I have created this javascript which generates a random number ranging from 0-20. What I want to do is create a text field where the user has 4 attempts to guess the generated number. How would I do this?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>
<body>
<form><input name="code" id="code" type="text" value="" ></input>
<script type="text/javascript">
function makeid() {
    return Math.floor(Math.random() * 20)
}
</script>
<input type="button" style="font-size:9pt" value="Generate Code" onclick="document.getElementById('code').value = makeid()">
</input>
</form>
</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T21:47:42+00:00Added an answer on June 8, 2026 at 9:47 pm

    You could use a global to track state. However, the security of this is weak, as the user could simply reload the page to circumvent the limit. What you really need to do then is track state on the server side (e.g. database) and then use e.g. an Ajax Call on the browser to do the checking 🙂

    <script type="text/javascript">
    var count = 0;
    function makeid() {
        count++;
        if (count <= 4) {
          return Math.floor(Math.random() * 20);
        }
        alert('Out of attempts');
    }
    </script>
    

    Edit
    Apologies, I’ve only answered half your question.

      <script type="text/javascript">
            var attempts = 0;
            var secretValue = makeid(); // Calculate the secret value once, and don't vary it once page is loaded
            function makeid() {
                return Math.floor(Math.random() * 20);
            }
            function checkId(userValue) {
                if (parseInt(userValue) == secretValue) {
                    alert('Correct');
                }
                else {
                    attempts++;
                    if (attempts <= 4) {
                        alert('Wrong - try again');
                    }
                    else {
                        alert('Out of attempts');
                    }
                }
            }
      </script>
    

    And then change your click handler to

    onclick="checkId(document.getElementById('code').value);"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok I have created a JavaScript page where the user can add text, they
I have created this Fiddle with this code: This is my problem I want
To explain my question I have created this http://jsfiddle.net/Jams/XNVB2/ here I want when div1
I have just created this table: create table dbo.OrderTypes ( ID smallint primary key
I have created my database by this sql query: (CREATE DATABASE + DBName +
I have a javascript function which get datagrid's header into THEAD tags. This code
I have created a javascript in which when we click Add button, it adds
I have some javascript which will create some sort of widget on a page.
I have this JavaScript code that creates a table, and puts in the rows
I have created this simple program to learn shared_ptr using namespace std; #define Yes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.