Javascript If语句来匹配数组 var myImages=newarray(); myImages[0]=“./img/10_of_clubs.png”; myImages[1]=“./img/2_of_clubs.png”; myImages[2]=“./img/3_of_hearts.png”; myImages[3]=“./img/4_of_spades.png”; myImages[4]=“./img/5_of_clubs.png”; myImages[5]=“./img/6_of_diamonds.png”; myImages[6]=“./img/7_of_diamonds.png”; myImages[7]=“./img/8_of_hearts.png”; myImages[8]=“./img/9_of_spades.png”; myImages[9]=“./img/ace_of_clubs.png”; myImages[10]=“./img/black_joker.png”; myImages[11]=“./img/jack_of_diamonds.png”; myImages[12]=“./img/king_of_hearts.png”; myImages[13]=“./img/queen_of_spades.png”; 函数shuffleCard(){ var shuffleSound=document.getElementById(“shuffleNoise”); shuffleSound.play(); var random1=Math.floor(Math.random()*myImages.length); document.getElementById(“deckOfCards1”).innerHTML=“”; var random2=Math.floor(Math.random()*myImages.length); document.getElementById(“deckOfCards2”).innerHTML=“”; } 函数{ 如果(){ 警报(“匹配!”); }否则{ 警告(“不匹配!”); } }

Javascript If语句来匹配数组 var myImages=newarray(); myImages[0]=“./img/10_of_clubs.png”; myImages[1]=“./img/2_of_clubs.png”; myImages[2]=“./img/3_of_hearts.png”; myImages[3]=“./img/4_of_spades.png”; myImages[4]=“./img/5_of_clubs.png”; myImages[5]=“./img/6_of_diamonds.png”; myImages[6]=“./img/7_of_diamonds.png”; myImages[7]=“./img/8_of_hearts.png”; myImages[8]=“./img/9_of_spades.png”; myImages[9]=“./img/ace_of_clubs.png”; myImages[10]=“./img/black_joker.png”; myImages[11]=“./img/jack_of_diamonds.png”; myImages[12]=“./img/king_of_hearts.png”; myImages[13]=“./img/queen_of_spades.png”; 函数shuffleCard(){ var shuffleSound=document.getElementById(“shuffleNoise”); shuffleSound.play(); var random1=Math.floor(Math.random()*myImages.length); document.getElementById(“deckOfCards1”).innerHTML=“”; var random2=Math.floor(Math.random()*myImages.length); document.getElementById(“deckOfCards2”).innerHTML=“”; } 函数{ 如果(){ 警报(“匹配!”); }否则{ 警告(“不匹配!”); } },javascript,Javascript,我已经创建了一个函数来洗牌一副牌,但是如何完成if语句以在两张牌匹配时显示警告消息?只需将变量random1和random2放在函数范围之外,这样您就可以在函数()中访问和比较它们 (我已将随机数添加到alt属性中,以便在此代码段中看到它们。) var myImages=new Array(), 随机数1, 第二, 得分=0; myImages[0]=“./img/10_of_clubs.png”; myImages[1]=“./img/2_of_clubs.png”; myImages[2]

我已经创建了一个函数来洗牌一副牌,但是如何完成if语句以在两张牌匹配时显示警告消息?

只需将变量
random1
random2
放在函数范围之外,这样您就可以在
函数()中访问和比较它们

(我已将随机数添加到alt属性中,以便在此代码段中看到它们。)

var myImages=new Array(),
随机数1,
第二,
得分=0;
myImages[0]=“./img/10_of_clubs.png”;
myImages[1]=“./img/2_of_clubs.png”;
myImages[2]=“./img/3_of_hearts.png”;
myImages[3]=“./img/4_of_spades.png”;
myImages[4]=“./img/5_of_clubs.png”;
myImages[5]=“./img/6_of_diamonds.png”;
myImages[6]=“./img/7_of_diamonds.png”;
myImages[7]=“./img/8_of_hearts.png”;
myImages[8]=“./img/9_of_spades.png”;
myImages[9]=“./img/ace_of_clubs.png”;
myImages[10]=“./img/black_joker.png”;
myImages[11]=“./img/jack_of_diamonds.png”;
myImages[12]=“./img/king_of_hearts.png”;
myImages[13]=“./img/queen_of_spades.png”;
函数shuffleCard(){
//var shuffleSound=document.getElementById(“shuffleNoise”);
//shuffleSound.play();
random1=Math.floor(Math.random()*myImages.length);
document.getElementById(“deckOfCards1”).innerHTML=“”;
random2=Math.floor(Math.random()*myImages.length);
document.getElementById(“deckOfCards2”).innerHTML=“”;
}
函数{
if(random1==random2){
分数+++;//增加分数
警报(“匹配!分数:+分数”);
}否则{
警告(“不匹配!”);
}
}
document.getElementById('shuffle')。addEventListener(“单击”,函数(ev){
shuffleCard();
});
document.getElementById('scrapt')。addEventListener(“单击”,函数(ev){
函数();
});

洗牌
抓取
用于比较两个变量

var myImages = new Array();
myImages[0] = "../img/10_of_clubs.png";         
myImages[1] = "../img/2_of_clubs.png";           
myImages[2] = "../img/3_of_hearts.png";         
myImages[3] = "../img/4_of_spades.png";         
myImages[4] = "../img/5_of_clubs.png";           
myImages[5] = "../img/6_of_diamonds.png";       
myImages[6] = "../img/7_of_diamonds.png";
myImages[7] = "../img/8_of_hearts.png";
myImages[8] = "../img/9_of_spades.png";
myImages[9] = "../img/ace_of_clubs.png";
myImages[10] = "../img/black_joker.png";
myImages[11] = "../img/jack_of_diamonds.png";
myImages[12] = "../img/king_of_hearts.png";
myImages[13] = "../img/queen_of_spades.png"; 

function shuffleCard(){
    var shuffleSound = document.getElementById("shuffleNoise");        
    shuffleSound.play();        
    var random1 = Math.floor(Math.random() * myImages.length); 
    document.getElementById("deckOfCards1").innerHTML = "<img src='" + 
    myImages[random1] + "'alt='myImages'></img>";        
    var random2 = Math.floor(Math.random() * myImages.length); 
    document.getElementById("deckOfCards2").innerHTML = "<img src='" + 
    myImages[random2] + "'alt='myImages'></img>";
}

function snatchFunction(){
    if () {
        alert("Match!");
    } else {
        alert("Do not match!");
    }
}
确保全局定义变量,以便
函数()
可以访问它:

function snatchFunction(){
    if (random1 == random2) {
        alert("Match!");
    } else {
        alert("Do not match!");
    }
}
var random1,random2
函数shuffleCard(){
var shuffleSound=document.getElementById(“shuffleNoise”);
shuffleSound.play();
random1=Math.floor(Math.random()*myImages.length);
document.getElementById(“deckOfCards1”).innerHTML=“”;
random2=Math.floor(Math.random()*myImages.length);
document.getElementById(“deckOfCards2”).innerHTML=“”;
}
函数{
if(random1==random2){
警报(“匹配!”);
}否则{
警告(“不匹配!”);
}
}
或通过函数调用传递变量:

var random1, random2

function shuffleCard(){
    var shuffleSound = document.getElementById("shuffleNoise");        
    shuffleSound.play();        
    random1 = Math.floor(Math.random() * myImages.length); 
    document.getElementById("deckOfCards1").innerHTML = "<img src='" + 
    myImages[random1] + "'alt='myImages'></img>";        
    random2 = Math.floor(Math.random() * myImages.length); 
    document.getElementById("deckOfCards2").innerHTML = "<img src='" + 
    myImages[random2] + "'alt='myImages'></img>";
}

function snatchFunction(){
    if (random1 == random2) {
        alert("Match!");
    } else {
        alert("Do not match!");
    }
}
函数shuffleCard(){
var shuffleSound=document.getElementById(“shuffleNoise”);
shuffleSound.play();
var random1=Math.floor(Math.random()*myImages.length);
document.getElementById(“deckOfCards1”).innerHTML=“”;
var random2=Math.floor(Math.random()*myImages.length);
document.getElementById(“deckOfCards2”).innerHTML=“”;
随机函数(随机1,随机2);
}
功能和功能(卡1、卡2){
如果(card1==card2){
警报(“匹配!”);
}否则{
警告(“不匹配!”);
}
}

尽管问题中没有提到,但在评论中提出了这个问题,我还是在这里添加了它

对于分数,将全局变量声明为random1或random2,即
var total_score=0

然后在
函数()中,每次匹配时增加值

function shuffleCard(){
    var shuffleSound = document.getElementById("shuffleNoise");        
    shuffleSound.play();        
    var random1 = Math.floor(Math.random() * myImages.length); 
    document.getElementById("deckOfCards1").innerHTML = "<img src='" + 
    myImages[random1] + "'alt='myImages'></img>";        
    var random2 = Math.floor(Math.random() * myImages.length); 
    document.getElementById("deckOfCards2").innerHTML = "<img src='" + 
    myImages[random2] + "'alt='myImages'></img>";

    snatchFunction(random1, random2);
}

function snatchFunction(card1, card2){
    if (card1 == card2) {
        alert("Match!");
    } else {
        alert("Do not match!");
    }
}

只是因为我喜欢这个想法:)另一种解决问题的方法:

var-types=[‘红心’、‘钻石’、‘梅花’、‘黑桃’],
类型1、类型2、编号1、编号2;
函数shuffle(){
$(“#card1”)
.removeClass(类型1)
.removeClass('c'+no1);
$(“#card2”)
.removeClass(类型2)
.removeClass('c'+no2);
type1=类型[Math.floor(Math.random()*types.length)],
type2=类型[Math.floor(Math.random()*types.length)],
no1=1+Math.floor(Math.random()*11),
no2=1+Math.floor(Math.random()*11);
$(“#card1”)
.removeClass('back')
.addClass(类型1)
.addClass('c'+no1);
$(“#card2”)
.removeClass('back')
.addClass(类型2)
.addClass('c'+no2);
如果(类型1==类型2&&no1==no2){
log('Match!!!');
}
}
.card{
位置:相对位置;
显示:内联块;
背景:url('https://cmkt-image-prd.global.ssl.fastly.net/0.1.0/ps/2502319/1160/772/m1/fpnw/wm0/12-playing-card-icons-1-.png?1491379334&s=8d4af6f2f06ee5cf0c53b95573d43e02');
背景重复:无重复;
宽度:105px;
高度:152px;
}
.回来{
背景位置:-823px-334px;
}
.标签顶部::之前{
位置:绝对位置;
顶部:15px;
左:35px;
}
.标签底部::之前{
位置:abs
function snatchFunction(){
    if (random1 === random2) {
        total_score += 10; // some score value 
        alert("Match! total score "+total_score);

    } else {
        alert("Do not match!");
    }
}