how to make a deck of cards in javascript
class Deck{
constructor(){
this.deck = [];
this.reset();
this.shuffle();
}
reset(){
this.deck = [];
const suits = ['Hearts', 'Spades', 'Clubs', 'Diamonds'];
const values = ['Ace', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King'];
for (let suit in suits) {
for (let value in values) {
this.deck.push(`${values[value]} of ${suits[suit]}`);
}
}
}
shuffle(){
const { deck } = this;
let m = deck.length, i;
while(m){
i = Math.floor(Math.random() * m--);
[deck[m], deck[i]] = [deck[i], deck[m]];
}
return this;
}
deal(){
return this.deck.pop();
}
}
const deck1 = new Deck();
console.log(deck1.deck);
deck1.reset();
console.log(deck1.deck);
Also in JavaScript:
- js select option value when selected
- check how many of a word is in a string
- convert camelcase to sentence case javascript
- json.stringify stack overflow
- JSX not allowed in files with extension '.js'
- how to import jquery in js file
- how to change css with js
- how to run method in method vue js on load
- How to write on a web page javascript
- conditional style prop react
- chartjs lineTension
- how to cast to javascript executor
- reduce method in javascript array of bjects
- javascript get hour from date
- javascript get element position relative to document
- javascript assign value to input using name
- import library react js
- file input disable open file picker javascript
- storybook react router
- js remove json value duplicates
- angularjs
- props and state react
- read and save excel with react
- js script