0

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..

0

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

6 Comments

Hey i edited the code, just like you said. still its not working. Maybe i am doing something wrong.
I added a rework of your code. As far as I can understand the question.
Maybe you realized it that the strings are ascii characters for chess program. Would it make any difference? Actually i want to show that a white coin cant kill another white coin and same with black coins.
Seeing that there is a difference in ascii between white and black coins makes it even simpler. I'll edit my code.. just a sec.
I checked using alert(), the $.inArray() is not being recognized by jQuery. No console Errors. once check this link
|
1

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;
}

2 Comments

Hi, i understand the solution now, but it is not working. The problem is the same as @Kyorcode solution. The variables are not being equal to those strings. It may be because of ASCII characters.
@venkateshwar: I see, that's not the actual text in your element. Use the html methods instead of the text function to get the HTML code in the element, or use character codes instead of HTML entities when checking the values, i.e. "♔" becomes "\u2654".
0

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;
  }

1 Comment

yes it will return "check". Actually i haven't provided the full code here. Because i am having trouble in this part. I edited the question. Please inform me if it is still not clear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.