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

  • Home
  • SEARCH
  • 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 6199211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:09:02+00:00 2026-05-24T04:09:02+00:00

In this fiddle http://jsfiddle.net/5L8Q8/28/ , if you click the black button, it randomly selects

  • 0

In this fiddle http://jsfiddle.net/5L8Q8/28/, if you click the black button, it randomly selects one of two values (red or blue) from an array. The randomly selected value is assigned to ran. In my real life application, there will be 16 elements in that array.

If you the pink “playagain” button, it chooses a random element from the same array but I want to make sure it’s not the same one chosen as last time.

Therefore, when I click playagain, I assign ran to lastran and compare it to the next randomly chosen value from the array and, if they are the same, choose randomly again. However, the way I have it isn’t guaranteeing that (upon the completion of playagain) ran is different.

I think I need a recursive function where comment 2 is in the code below, but I keep breaking my code when I try to create it.

Can you comment on the 3 comments in the code below?

Note, I’m a relative newbie, so this code is probably awful…

$("#playagain").click(function(){
    lastran = ran; 


    ran = getRandom(myArray, true);

    if (ran === lastran) { 

        ran = getRandom(myArray, true); //1. do I need to return this?

           //2. want to test ran === lastran again.. How to set up recursive function?

    } else {

       return; //3.is this the right thing to do here?
    }  

});
  • 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-24T04:09:03+00:00Added an answer on May 24, 2026 at 4:09 am

    You can use a while loop instead of the if.

    while(ran == lastran)
    {
      ran = getRandom(myArray, true);
    }
    

    It’ll keep trying until it gets a different value.

    • 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/52/ if you click the black box, it randomly selects either
In this fiddle http://jsfiddle.net/CBxbT/29/ , if you click the black box, a random color
In this fiddle http://jsfiddle.net/SySRb/40/ , I created an array with two elements, #f-ONE and
I have the following javascript code found also in this fiddle: http://jsfiddle.net/periklis/k4u4c/ <button id
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
If you look at this fiddle: http://jsfiddle.net/kamilszokaluk/uhmYg/ I have 3 divs, the upper one
I have this fiddle : http://jsfiddle.net/XjeCf/1/ that works like I want and this one
In this fiddle - http://jsfiddle.net/zyUkd/26/ I am attempting to enable the div Hello 01
Referencing this Fiddle ( http://jsfiddle.net/j5uGN ), how can I toggle a clicked image's border

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.