Mr_Green
Mr_Green

Reputation: 41832

jQuery how to check two array elements belongs to same array or not

i am new to jQuery as well as programming, how can i check whether two array elements belong to same array. I did this.. but its not working

var blackCoins = ["♚", "♛", "♜", "♝", "♞", "♟"];
var whiteCoins = ["♔", "♕", "♖", "♗", "♘", "♙"];
var buffer = $("myselector1").text();
var storeBuffer = $("#myselector2").text();
        var flag = 0;           
        if ($.inArray(buffer, whiteCoins) > -1  &&  $.inArray(storeBuffer, whiteCoins) > -1){                        
                flag = 1;
        }
        else if($.inArray(buffer, blackCoins) > -1  &&  $.inArray(storeBuffer, blackCoins) > -1) {
                flag = 1;
        }           
}

i can give more information if needed..

Upvotes: 0

Views: 177

Answers (3)

KyorCode
KyorCode

Reputation: 1497

JQuery .InArray() this function should help you out.

Use this in a && combination and you'll get your answer.

var blackCoins = ["♚", "♛", "♜", "♝", "♞", "♟"];
var whiteCoins = ["♔", "♕", "♖", "♗", "♘", "♙"];

function getCoinColor(coin) {
    var inBlack = $.inArray(coin, blackCoins);
    var inWhite = $.inArray(coin, whiteCoins);

    if (inBlack > -1) {
        return "black";
    }

    if (inWhite > -1) {
        return "white";
    }
}

function canKill(selectedCoin, targetCoin) {
    return getCoinColor(selectedCoin) != getCoinColor(targetCoin);
}


var targetCoin = "♚";
var selectedCoin = "♝";

var killable = canKill(selectedCoin, targetCoin);

if (killable) {
    alert("Killed it!");
}
else {
    alert("Can't kill your own kind!");
}​

I tried to write it as easy to understand as possible. I know there are a few ways to shorten it.

JsFiddle

Upvotes: 1

m7o
m7o

Reputation: 921

Something is strange about how you get the element to check - because of this:

$("#check").attr("id");

The attribute "id" of $('#check') will return "check"... what are you trying to do?

I assume we have a valid selector, then the answer would be:

  var flag=0;            
  var whiteCoins = ["♔", "♕", "♖", "♗", "♘", "♙"];
  var mytext = $('#myselector').text();

  if($.inArray(mytext, whiteCoins) > -1){
    flag=1;
  }

What I don't get from your code is if you maybe are checking two different elements against your array. From your code it is not clear what $(this) is. If you are checking two selectors and one of them is $(this), use this:

  var flag=0;            
  var whiteCoins = ["♔", "♕", "♖", "♗", "♘", "♙"];
  var mytext1 = $('#myselector1').text();
  var mytext2 = $(this).text();
  if($.inArray(mytext1, whiteCoins) > -1 && $.inArray(mytext2, whiteCoins) > -1){
    flag=1;
  }

Upvotes: 0

Guffa
Guffa

Reputation: 700212

You are doing the same check twice, so put that check in a function:

function isWhiteCoin(color) {
  switch (color) {
    case "♔":
    case "♕":
    case "♖":
    case "♗":
    case "♘":
    case "♙": return true;
  }
  return false;
}

Now you can just call the function twice:

var flag=0;
var preRefColor = $("#check").text();
var thisColor = $(this).text();
if (isWhiteCoin(preRefColor) && isWhiteCoin(thisColor)) {
  flag = 1;
}

Upvotes: 1

Related Questions