How can I get a random result from an array, then based on that result get another random result from another array?

jmorris

I have an array

firstArray = [ "Blue", "Red", "Green" ];

Red

I want to click a button and display a random result from that array, let's say it's "Red". Then based on that result "Red" I want to click a second button and get another random result from another array.

blueArray = [ "Sky", "Water", "Jeans" ];
redArray = [ "Apple", "Firetruck", "Rose" ];
greenArray = [ "Grass", "Money", "Leaves" ];

Red - Rose

Then I want to click the first button again, get a new result and clear the second button's result.

ibrahim mahrir

You can group the three arrays in an object whose keys will be in the first array:

var colors = [ "Blue", "Red", "Green" ];                     // colors (keys from items object)

var items = {                                                // each color from colors has an array of items in this object
  "Blue": ["Sky", "Water", "Jeans"],                         // items for color "Blue"
  "Red": ["Apple", "Firetruck", "Rose"],                     // ...
  "Green": ["Grass", "Money", "Leaves"]                      // ...
};


var color = null;                                            // should be here in the outside so it will be accessed by both event listeners (you can initialize it to a color if you want)
$("#first-button").click(function() {                        // when clicking the first button ...
  color = colors[Math.floor(Math.random() * colors.length)]; // ... choose a random color from colors and assign it to the variable color
  $("#color-span").text(color);                              // set the text of the color span
});
$("#second-button").click(function() {                       // when clicking the second button ...
  if(color) {                                                // check if we had chosen a color (using the first button)
    var item = items[color][Math.floor(Math.random() * items[color].length)]; // then choose an item from the equivalent array (if color is "Blue" then items.Blue will be chosen)
    $("#sentence-span").text(color + " " + item);            // set the text of the sentence span (using both color and item
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="first-button">First Button</button>
<button id="second-button">Second Button</button>
<p><b>Color is: </b><span id="color-span"></span></p>
<p><b>Sentence is: </b><span id="sentence-span"></span></p>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How do I get the result from login action to check in another component Menu after login success

From Dev

how can i get a temp table result from a string?

From Dev

I can not get result from AsyncTask?

From Dev

How can I get the TOP 1 from this GROUP BY result?

From Dev

Get a random search result from Amazon CloudSearch

From Dev

How can I get result from curl in Apache Camel?

From Dev

How to Get the filter result from array object in javascript?

From Dev

How can get array random from another array string?

From Dev

How to use result from a hash function to get an array index?

From Dev

It is possible to get make array in cell from another tables result in mysql

From Dev

How to get another Calendar result from same instance?

From Dev

How do I process this array to get this result?

From Dev

How get result from method using another thread

From Dev

Get the result of setImageResource from random and set it to string

From Dev

get a random query result and then sort it

From Dev

Javascript - Get checkbox values from a form and get a random result

From Dev

how to get an array from a php explode result?

From Dev

JQuery : Get a value from the result object array

From Dev

get first object from result array php

From Dev

Get random result from JPQL query over large table

From Dev

How to get a distinct result from a single column and array from another with eloquent?

From Dev

how can i perform a query on a hash, and get result another hash?

From Dev

How to check if a result array element is present in another result array

From Dev

Call another php from another domain and get the result

From Dev

Django, How to get random posts list result?

From Dev

How can I shuffle a result I get from a Postgresql?

From Dev

get separate result from returning array in codeigniter

From Dev

how to get the required result, the result is array list?

From Dev

How to get a result from a single array PHP

Related Related

  1. 1

    How do I get the result from login action to check in another component Menu after login success

  2. 2

    how can i get a temp table result from a string?

  3. 3

    I can not get result from AsyncTask?

  4. 4

    How can I get the TOP 1 from this GROUP BY result?

  5. 5

    Get a random search result from Amazon CloudSearch

  6. 6

    How can I get result from curl in Apache Camel?

  7. 7

    How to Get the filter result from array object in javascript?

  8. 8

    How can get array random from another array string?

  9. 9

    How to use result from a hash function to get an array index?

  10. 10

    It is possible to get make array in cell from another tables result in mysql

  11. 11

    How to get another Calendar result from same instance?

  12. 12

    How do I process this array to get this result?

  13. 13

    How get result from method using another thread

  14. 14

    Get the result of setImageResource from random and set it to string

  15. 15

    get a random query result and then sort it

  16. 16

    Javascript - Get checkbox values from a form and get a random result

  17. 17

    how to get an array from a php explode result?

  18. 18

    JQuery : Get a value from the result object array

  19. 19

    get first object from result array php

  20. 20

    Get random result from JPQL query over large table

  21. 21

    How to get a distinct result from a single column and array from another with eloquent?

  22. 22

    how can i perform a query on a hash, and get result another hash?

  23. 23

    How to check if a result array element is present in another result array

  24. 24

    Call another php from another domain and get the result

  25. 25

    Django, How to get random posts list result?

  26. 26

    How can I shuffle a result I get from a Postgresql?

  27. 27

    get separate result from returning array in codeigniter

  28. 28

    how to get the required result, the result is array list?

  29. 29

    How to get a result from a single array PHP

HotTag

Archive