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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:08:07+00:00 2026-05-23T16:08:07+00:00

in this fiddle, http://jsfiddle.net/urfXq/79/ , if you click on the black button, it randomly

  • 0

in this fiddle,http://jsfiddle.net/urfXq/79/, if you click on the black “button”, it randomly chooses an element (red, green or blue) from an array. The array is graphically represented by the colored boxes at the top. Obviously the colored boxes at the top don`t need to be there.

That part seems to work fine. Indeed, here`s a fiddle that shows the first part works jsfiddle.net/urfXq/46 . Assming this is the correct way to assign the random result to a variable (after converting it to lower case)

$("#button").click(function() {
var ran = getRandom(myArray, true).toLowerCase();
});

the problems seem to start in the second part. The user is supposed to guess which color was chosen. The user guesses by clicking on one of the colored boxes at the bottom. The guess is assigned to the variable “guess”

It is at this point–after the user guesses–that I want the ternary operator to be triggered and to compare variable “ran” with “guess”. If they are the same, then box pink fades out. If they are different, box lime-green fades out.

This whole last function seems to be full of problems that I can`t figure out.

//user clicks button to guess
$("#red,#blue, #green").click(function() {
  //if click blue is blue being assigned to guess?
    var guess = $(this);
 //ternary to compare if guess and random are equal
   $("guess===ran") ? results.fadeOut(1000) : wrong.fadeOut(1000);
  });
  • 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-23T16:08:07+00:00Added an answer on May 23, 2026 at 4:08 pm

    Arrggg there’re so many things to improve, but at least it works:

    var myArray = [],
        ran, guess;
    
    myArray[0] = "RED";
    myArray[1] = "BLUE";
    myArray[2] = "GREEN";
    
    //function getRandom to choose a random element from array
    function getRandom(array, getVal) {
        var key = Math.floor(Math.random() * array.length);
        if (getVal) {
            return array[key];
        }
        return key;
    }
    
    //click the black button to call the random function
    $("#button").click(function() {
        ran = getRandom(myArray, true).toLowerCase();
    });
    
    //user clicks button to guess
    $("#red, #blue, #green").click(function(e) {
      //if click blue is blue being assigned to guess?
        guess = $(this).attr('id');
        //ternary to compare if guess and random are equal
    
        if (guess === ran) {
            $('#results').fadeOut(1000)
        } else {
            $('#wrong').fadeOut(1000);  
        }   
     });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In this fiddle http://jsfiddle.net/5L8Q8/28/ , if you click the black button, it randomly selects
In this fiddle http://jsfiddle.net/CBxbT/29/ , if you click the black box, a random color
Here is my fiddle: http://jsfiddle.net/jamesbrighton/wxWgG/4/ HTML: <div> <p class=click>Click 1</p> <p>This should be ignored</p>
I have the following javascript code found also in this fiddle: http://jsfiddle.net/periklis/k4u4c/ <button id
Ooops, fiddle updated now correct In this fiddle, http://jsfiddle.net/SySRb/118/ once you click .start an
Have a look at this fiddle: http://jsfiddle.net/h4VS7/ How do I make the yellow element
On this fiddle: http://jsfiddle.net/TechplexEngineer/nFZ8e/1/ How could one get the blocks element to fill the
Please look at this fiddle: http://jsfiddle.net/uNbYu/1/ When the submit button is pressed once, It
Please see this fiddle: http://jsfiddle.net/ZWw3Z/5/ My code is: p { position: relative; background-color: blue;
http://jsfiddle.net/ksHuz/ I'm trying to use a background-image with the <a> element in this fiddle,

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.