Thursday, 15 July 2010

javascript - Adding values to an array when random numbers are generated -


I appreciate some help in adding a random number generator to an empty array.

Can you please tell me how I will do this in javascript. I already have random numbers output - random array of numbers - then the array is then randomly selected and outputs the number. How can I reserve the values ​​each time a random button is pressed?

  // suits var suites = new array (); Suites [0] = "heart"; Suit [1] = "Hukum"; Suites [2] = "diamond"; Suites [3] = "club"; // value value value = new array (); Value [0] = "Ace"; Value [1] = "king"; Value [2] = "queen"; Value [3] = "jack"; Value [4] = "2"; Value [5] = "3"; Value [6] = "4"; Value [7] = "5"; Value [8] = "6"; Value [9] = "7"; Value [10] = "8"; Value [11] = "9"; Value [12] = "10"; Function myFunction () {var x = document.getElementById ("demo") var y = document.getElementById ("demo2") x.innerHTML = value [Math.flur ((Math.reference ()) * 12) y y Html = suites [math.flur ((math.rendum ()) * 4)]}   

I would recommend a more clean OOP solution. Use a card object constructor to store each card, so you can pass it easily and store it in the array using push.

  Var dealistory = New Array (); // array that will create all the cards // card constructor function card (value, suite) {return ({'value': value, 'suite': suite}); } // Random Cards Generator Function Random Cards () {var suites = ["heart", "spades", "diamond", "club"]; Var "," 8 "," 6 "," 7 "," 5 "," 6 "," 7 "," 2 "," 3 "," 4 "," 9 "," 10 "]; Var s = value [Math.flur ((Math. Random ()) * 12)]; Var v = Suites [Math.floor ((Math.rendum ()) * 4)]; Return card (S, V); } // deal button action deal. Click on = function () {var newCard = randomCard (); Document.getElementById ("Demo"). InnerHTML = newcard.value; Document.getElementById ("demo2"). InnerHTML = newCard.suite; DealHistory.push (newCard); // add history history console.log (dealHistory); }   

dealHistory array example:
enter image details here


Edit:

I have a function printcard to generate HTML for each card and function printHist Added to display each item on the screen

.png "alt =" Enter image details here ">

  // One card function Create HTML for print card (card) {var cardHTML = ''; card html + = '& l T; div class = "card" & gt; & gt; card html + = 'div class = "suite" & gt; + Card.suite + '& lt; / div & gt; Card HTML + =' 
< / Pre>

No comments:

Post a Comment