Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   вернуть property после method (https://javascript.ru/forum/misc/82432-vernut-property-posle-method.html)

OlesiaBOM 05.05.2021 21:22

вернуть property после method
 
Нужно что бы метод draw() удалял последний елемент масива cards.
function deckBuilder() {
  const values = [ "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", ];
  const suits = ["Hearts", "Diamonds", "Spades", "Clubs"];
  const cards = [];
  for (let s = 0; s < suits.length; s++) {
    for (let v = 0; v < values.length; v++) {
      const value = values[v];
      const suit = suits[s];
      cards.push({ value, suit });
    }
  }
  return cards;
}

const deckN = deckBuilder();

class Deck {
    constructor() {
        this.cards = deckN;
        this.count = this.cards.length;
    }

    shuffle() {

       let location1, location2, tmp;
       for (let i = 0; i < 1000; i++) {
           location1 = Math.floor((Math.random() * this.cards.length));
           location2 = Math.floor((Math.random() * this.cards.length));
           tmp = this.cards[location1];
           this.cards[location1] = this.cards[location2];
           this.cards[location2] = tmp;
        }
    }
    draw() {
      return this.cards.pop();

    }
}

let gameBoard = new Deck();
gameBoard.shuffle();
gameBoard.draw();
console.log(gameBoard.count);

рони 05.05.2021 21:27

OlesiaBOM,
Пожалуйста, отформатируйте свой код!

Для этого его можно заключить в специальные теги: js/css/html и т.п., например:
[html run]
... минимальный код страницы с вашей проблемой
[/html]

О том, как вставить в сообщение исполняемый javascript и html-код, а также о дополнительных возможностях форматирования - читайте http://javascript.ru/formatting.

OlesiaBOM 05.05.2021 21:31

Спасибо. Отформатировала.

рони 05.05.2021 21:40

OlesiaBOM,
https://learn.javascript.ru/property...tery-i-settery

function deckBuilder() {
    const values = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", ];
    const suits = ["Hearts", "Diamonds", "Spades", "Clubs"];
    const cards = [];
    for (let s = 0; s < suits.length; s++) {
        for (let v = 0; v < values.length; v++) {
            const value = values[v];
            const suit = suits[s];
            cards.push({
                value,
                suit
            });
        }
    }
    return cards;
}
const deckN = deckBuilder();
class Deck {
    constructor() {
        this.cards = deckN;
    }
    get count() {
        return this.cards.length
    };
    shuffle() {
        let location1, location2, tmp;
        for (let i = 0; i < 1000; i++) {
            location1 = Math.floor((Math.random() * this.cards.length));
            location2 = Math.floor((Math.random() * this.cards.length));
            tmp = this.cards[location1];
            this.cards[location1] = this.cards[location2];
            this.cards[location2] = tmp;
        }
    }
    draw() {
        return this.cards.pop();
    }
}
let gameBoard = new Deck();
gameBoard.shuffle();
gameBoard.draw();
console.log(gameBoard.count);


Часовой пояс GMT +3, время: 06:49.