我创建了一个构造函数,您可以在其中创建卡。我正在尝试在此函数内创建一个方法,以检查一个卡对象的任何值或西装是否与另一个卡对象相同。如果仅此而已,我只想发送一条警报,说值或西服相同。 例如,
card1 =新卡(8,“俱乐部”)
card2 =新的Card(8,'钻石')
这将返回,表示卡1和卡2的值相同。
我不确定如何检查每个Card对象并将其与另一个Card对象进行比较。
function Card(value, suit){
this.value = value;
this.suit = suit;
this.check = function(){
// checking the card objects goes here
}
}
let card1 = new Card(8, 'diamonds');
let card2 = new Card(2, 'clubs');
答案 0 :(得分:1)
这是一种创建Deck对象的方法。您也应该可以使用普通数组来执行此操作,但是卡组实际上应该是知道卡组中有哪些牌的对象,这意味着它应该是卡组对象的函数。
通常,您可以只使用数组方法some()
并将其传递给确定是或否的函数:
function Card(value, suit){
this.value = value;
this.suit = suit;
}
function Deck(cards){
this.cards = [...cards]
}
// the function to tell if card value OR suit is in deck
Deck.prototype.includesAny = function(card){
return this.cards.some(item => item.suit === card.suit || item.value === card.value)
}
let card1 = new Card(8, 'diamonds');
let card2 = new Card(2, 'clubs');
let card3 = new Card(8, 'diamonds')
let card4 = new Card(3, 'hearts')
// a deck with only two cards
let deck = new Deck([card1, card2])
console.log(deck.includesAny(card3)) // deck has both 8 and diamonds
console.log(deck.includesAny(card4)) // no hearts or threes in deck
答案 1 :(得分:1)
这是我唯一的思考方式-
function Card(value, suit, list){
console.log("ritesh");
if(check(value, suit, list)){
this.value = value;
this.suit = suit;
} else {
console.log("duplicate car found!!");
return;
}
function check(value, suit, list){
for (var i=0; i < list.length; i++) {
if (list[i].value === value || list[i].suit === suit) {
return false;
}
}
return true;
}
this.list = list;
}
let list = [];
list.push(new Card(8, 'diamonds',list));
list.push(new Card(2, 'clubs',list));
list.push(new Card(8, 'clubs',list));
答案 2 :(得分:0)
您可以尝试以下解决方案:
function Card(value, suit){
this.value = value;
this.suit = suit;
this.check = function(...cards){
for(var card of cards){
if((this.value == card.value) || (this.suit == card.suit)) return true;
}
return false;
}
}
let card1 = new Card(8, 'diamonds');
console.log(card1.check(new Card(2, 'clubs'), new Card(3,'hearts')));
注意:-您可以在此处传递N张卡片进行比较。
答案 3 :(得分:0)
在前面的答案的基础上,这是执行检查的正确方法,这还可以确保传递的对象具有相同的“ Card”类型, 您还可以传递一个数组而不是单个对象,并利用foreach功能遍历该数组:
function Card(value, suit){
this.value = value;
this.suit = suit;
this.check = function(card2){
var match = false;
if(card2 && typeof card2=== 'object' && value.constructor === Card){
match = ((this.value == card2.value) || (this.suit == card2.suit));
}
return match;
}
}
let card1 = new Card(8, 'diamonds');
let card2 = new Card(2, 'clubs');
console.log(card1.check(card2));
答案 4 :(得分:0)
问题尚不清楚,但我想您将把卡片传递给函数,所以这是我的解决方法
function Card(value, suit){
this.value = value;
this.suit = suit;
this.check = function(cardToCheckAgainst){
let matchOnSuit = 0;
let matchOnValue = 0;
this.suit === cardToCheckAgainst.suit ? matchOnSuite = 1 : matchOnSuite = 0;
this.value === cardToCheckAgainst.value ? matchOnValue = 1 : matchOnValue = 0;
if(matchOnSuit == 1){
alert('both cards have the suit:', this.suit );
}
if(matchOnValue == 1){
alert('both cards have the value:', this.value );
}
}
}
let card1 = new Card(8, 'diamonds');
let card2 = new Card(2, 'clubs');