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 7612033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:48:39+00:00 2026-05-31T01:48:39+00:00

im just starting out with javascript so go ez on me. i have an

  • 0

im just starting out with javascript so go ez on me. i have an exercise where i have to create 4 divs that are a box’s. and each time you refresh the page one div is the correct one to select. the main question im wondering is how can match a div to random number ?

my divs in the html are as follows

<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
<div id="div4"></div>

for javascript all i have so far is

var randomNumber = Math.ceil(Math.random()*4);

but im not sure how i can make a function to say if randomNumber == div# ? then do this.

  • 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-05-31T01:48:40+00:00Added an answer on May 31, 2026 at 1:48 am

    All right so you have four divs as you explained. We’re going to want to add code to them that processes the user’s clicks so we’ll add what’s called an event handler to them.

    <div id="div1" onclick="process(1)"></div>
    <div id="div2" onclick="process(2)"></div>
    <div id="div3" onclick="process(3)"></div>
    <div id="div4" onclick="process(4)"></div>
    

    Now in a script tag above these divs in the code put the following function:

    function ( divNum ) {
        // Define box as the div on which the user clicked
        var box = document.getElementById('div' + divNum);
    
        // Compare the div the user clicked on to the random number
        if ( divNum === randomNumber ) {
            // The user chose right
            box.style.backgroundColor = "black";
        }
        else {
            // The user chose incorrect
            box.style.backgroundColor = "red";
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just starting out in asp.net. Have just created a login.aspx page in my site
OK I am just starting out with jQuery. I have a page with a
Just starting out here, I have a Article.rb model that stores articles. So it
I am just starting out learning JavaScript and I have just reach the DOM
Just starting out with subversion, have set up repos for 3 current projects and
I am just starting out with DI & unit testing and have hit a
So I am just starting out developing PHP web applications and have finished setting
I'm just starting to learn Javascript; I do however have experience in programming using
I am just starting to learn javascript, so I don't have the skills to
I'm just starting out with Grails, transitioning from Javascript In a controller, I'm trying

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.