I'm unable to compare two array items in Javscript












0















I'm having a hard time figuring how to accurately compare two items in an array. Here is the javascript:



const listCards = document.querySelectorAll('.fa');
cardArray = Array.from(listCards);
let shuffleCards = shuffle(cardArray.slice(3,cardArray.length-1));

document.querySelectorAll('.fa').forEach(function(cv){cv.remove(), this});



/*
* Display the cards on the page
* - shuffle the list of cards using the provided "shuffle" method below
* - loop through each card and create its HTML
* - add each card's HTML to the page
*/
const listAddCard = document.querySelectorAll('.card');

for(i=0; i<listAddCard.length; i++){

listAddCard.item(i).appendChild(shuffleCards[i]);
if(i>0){
if(shuffleCards[i].innerHTML === (shuffleCards[0].innerHTML)){
console.log('i is' + shuffleCards[i].innerHTML + 'and 0 is' + shuffleCards[0].innerHTML)
listAddCard.item(i).classList.add('match');


}
}
else listAddCard.item(i).classList.add('match');
}

// Shuffle function from http://stackoverflow.com/a/2450976
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;

while (currentIndex !== 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}

return array;
}


Here is the HTML that the javascript is sourcing:



<ul class="deck">
<li class="card">
<i class="fa fa-diamond"></i>
</li>
<li class="card">
<i class="fa fa-paper-plane-o"></i>
</li>
<li class="card">
<i class="fa fa-anchor"></i>
</li>
<li class="card">
<i class="fa fa-bolt"></i>
</li>
<li class="card">
<i class="fa fa-cube"></i>
</li>
<li class="card">
<i class="fa fa-anchor"></i>
</li>
<li class="card">
<i class="fa fa-leaf"></i>
</li>
<li class="card">
<i class="fa fa-bicycle"></i>
</li>
<li class="card">
<i class="fa fa-diamond"></i>
</li>
<li class="card">
<i class="fa fa-bomb"></i>
</li>
<li class="card">
<i class="fa fa-leaf"></i>
</li>
<li class="card">
<i class="fa fa-bomb"></i>
</li>
<li class="card open show">
<i class="fa fa-bolt"></i>
</li>
<li class="card">
<i class="fa fa-bicycle"></i>
</li>
<li class="card">
<i class="fa fa-paper-plane-o"></i>
</li>
<li class="card">
<i class="fa fa-cube"></i>
</li>

</ul>


Whenever I run this check in my code if(shuffleCards[i].innerHTML === (shuffleCards[0].innerHTML)), it always runs true. I'm not sure how to compare it accurately.



I basically converted a node list into an array and then I'm trying to compare the value of each item in that list.










share|improve this question























  • Well you're comparing innerHTML. All of these have empty innerHTML. Of course the evaluation will always be true

    – Abana Clara
    Nov 14 '18 at 0:27













  • Ok, so what should I compare?

    – Aditya Gorti
    Nov 14 '18 at 0:34
















0















I'm having a hard time figuring how to accurately compare two items in an array. Here is the javascript:



const listCards = document.querySelectorAll('.fa');
cardArray = Array.from(listCards);
let shuffleCards = shuffle(cardArray.slice(3,cardArray.length-1));

document.querySelectorAll('.fa').forEach(function(cv){cv.remove(), this});



/*
* Display the cards on the page
* - shuffle the list of cards using the provided "shuffle" method below
* - loop through each card and create its HTML
* - add each card's HTML to the page
*/
const listAddCard = document.querySelectorAll('.card');

for(i=0; i<listAddCard.length; i++){

listAddCard.item(i).appendChild(shuffleCards[i]);
if(i>0){
if(shuffleCards[i].innerHTML === (shuffleCards[0].innerHTML)){
console.log('i is' + shuffleCards[i].innerHTML + 'and 0 is' + shuffleCards[0].innerHTML)
listAddCard.item(i).classList.add('match');


}
}
else listAddCard.item(i).classList.add('match');
}

// Shuffle function from http://stackoverflow.com/a/2450976
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;

while (currentIndex !== 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}

return array;
}


Here is the HTML that the javascript is sourcing:



<ul class="deck">
<li class="card">
<i class="fa fa-diamond"></i>
</li>
<li class="card">
<i class="fa fa-paper-plane-o"></i>
</li>
<li class="card">
<i class="fa fa-anchor"></i>
</li>
<li class="card">
<i class="fa fa-bolt"></i>
</li>
<li class="card">
<i class="fa fa-cube"></i>
</li>
<li class="card">
<i class="fa fa-anchor"></i>
</li>
<li class="card">
<i class="fa fa-leaf"></i>
</li>
<li class="card">
<i class="fa fa-bicycle"></i>
</li>
<li class="card">
<i class="fa fa-diamond"></i>
</li>
<li class="card">
<i class="fa fa-bomb"></i>
</li>
<li class="card">
<i class="fa fa-leaf"></i>
</li>
<li class="card">
<i class="fa fa-bomb"></i>
</li>
<li class="card open show">
<i class="fa fa-bolt"></i>
</li>
<li class="card">
<i class="fa fa-bicycle"></i>
</li>
<li class="card">
<i class="fa fa-paper-plane-o"></i>
</li>
<li class="card">
<i class="fa fa-cube"></i>
</li>

</ul>


Whenever I run this check in my code if(shuffleCards[i].innerHTML === (shuffleCards[0].innerHTML)), it always runs true. I'm not sure how to compare it accurately.



I basically converted a node list into an array and then I'm trying to compare the value of each item in that list.










share|improve this question























  • Well you're comparing innerHTML. All of these have empty innerHTML. Of course the evaluation will always be true

    – Abana Clara
    Nov 14 '18 at 0:27













  • Ok, so what should I compare?

    – Aditya Gorti
    Nov 14 '18 at 0:34














0












0








0








I'm having a hard time figuring how to accurately compare two items in an array. Here is the javascript:



const listCards = document.querySelectorAll('.fa');
cardArray = Array.from(listCards);
let shuffleCards = shuffle(cardArray.slice(3,cardArray.length-1));

document.querySelectorAll('.fa').forEach(function(cv){cv.remove(), this});



/*
* Display the cards on the page
* - shuffle the list of cards using the provided "shuffle" method below
* - loop through each card and create its HTML
* - add each card's HTML to the page
*/
const listAddCard = document.querySelectorAll('.card');

for(i=0; i<listAddCard.length; i++){

listAddCard.item(i).appendChild(shuffleCards[i]);
if(i>0){
if(shuffleCards[i].innerHTML === (shuffleCards[0].innerHTML)){
console.log('i is' + shuffleCards[i].innerHTML + 'and 0 is' + shuffleCards[0].innerHTML)
listAddCard.item(i).classList.add('match');


}
}
else listAddCard.item(i).classList.add('match');
}

// Shuffle function from http://stackoverflow.com/a/2450976
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;

while (currentIndex !== 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}

return array;
}


Here is the HTML that the javascript is sourcing:



<ul class="deck">
<li class="card">
<i class="fa fa-diamond"></i>
</li>
<li class="card">
<i class="fa fa-paper-plane-o"></i>
</li>
<li class="card">
<i class="fa fa-anchor"></i>
</li>
<li class="card">
<i class="fa fa-bolt"></i>
</li>
<li class="card">
<i class="fa fa-cube"></i>
</li>
<li class="card">
<i class="fa fa-anchor"></i>
</li>
<li class="card">
<i class="fa fa-leaf"></i>
</li>
<li class="card">
<i class="fa fa-bicycle"></i>
</li>
<li class="card">
<i class="fa fa-diamond"></i>
</li>
<li class="card">
<i class="fa fa-bomb"></i>
</li>
<li class="card">
<i class="fa fa-leaf"></i>
</li>
<li class="card">
<i class="fa fa-bomb"></i>
</li>
<li class="card open show">
<i class="fa fa-bolt"></i>
</li>
<li class="card">
<i class="fa fa-bicycle"></i>
</li>
<li class="card">
<i class="fa fa-paper-plane-o"></i>
</li>
<li class="card">
<i class="fa fa-cube"></i>
</li>

</ul>


Whenever I run this check in my code if(shuffleCards[i].innerHTML === (shuffleCards[0].innerHTML)), it always runs true. I'm not sure how to compare it accurately.



I basically converted a node list into an array and then I'm trying to compare the value of each item in that list.










share|improve this question














I'm having a hard time figuring how to accurately compare two items in an array. Here is the javascript:



const listCards = document.querySelectorAll('.fa');
cardArray = Array.from(listCards);
let shuffleCards = shuffle(cardArray.slice(3,cardArray.length-1));

document.querySelectorAll('.fa').forEach(function(cv){cv.remove(), this});



/*
* Display the cards on the page
* - shuffle the list of cards using the provided "shuffle" method below
* - loop through each card and create its HTML
* - add each card's HTML to the page
*/
const listAddCard = document.querySelectorAll('.card');

for(i=0; i<listAddCard.length; i++){

listAddCard.item(i).appendChild(shuffleCards[i]);
if(i>0){
if(shuffleCards[i].innerHTML === (shuffleCards[0].innerHTML)){
console.log('i is' + shuffleCards[i].innerHTML + 'and 0 is' + shuffleCards[0].innerHTML)
listAddCard.item(i).classList.add('match');


}
}
else listAddCard.item(i).classList.add('match');
}

// Shuffle function from http://stackoverflow.com/a/2450976
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;

while (currentIndex !== 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}

return array;
}


Here is the HTML that the javascript is sourcing:



<ul class="deck">
<li class="card">
<i class="fa fa-diamond"></i>
</li>
<li class="card">
<i class="fa fa-paper-plane-o"></i>
</li>
<li class="card">
<i class="fa fa-anchor"></i>
</li>
<li class="card">
<i class="fa fa-bolt"></i>
</li>
<li class="card">
<i class="fa fa-cube"></i>
</li>
<li class="card">
<i class="fa fa-anchor"></i>
</li>
<li class="card">
<i class="fa fa-leaf"></i>
</li>
<li class="card">
<i class="fa fa-bicycle"></i>
</li>
<li class="card">
<i class="fa fa-diamond"></i>
</li>
<li class="card">
<i class="fa fa-bomb"></i>
</li>
<li class="card">
<i class="fa fa-leaf"></i>
</li>
<li class="card">
<i class="fa fa-bomb"></i>
</li>
<li class="card open show">
<i class="fa fa-bolt"></i>
</li>
<li class="card">
<i class="fa fa-bicycle"></i>
</li>
<li class="card">
<i class="fa fa-paper-plane-o"></i>
</li>
<li class="card">
<i class="fa fa-cube"></i>
</li>

</ul>


Whenever I run this check in my code if(shuffleCards[i].innerHTML === (shuffleCards[0].innerHTML)), it always runs true. I'm not sure how to compare it accurately.



I basically converted a node list into an array and then I'm trying to compare the value of each item in that list.







javascript arrays compare






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 0:25









Aditya GortiAditya Gorti

287




287













  • Well you're comparing innerHTML. All of these have empty innerHTML. Of course the evaluation will always be true

    – Abana Clara
    Nov 14 '18 at 0:27













  • Ok, so what should I compare?

    – Aditya Gorti
    Nov 14 '18 at 0:34



















  • Well you're comparing innerHTML. All of these have empty innerHTML. Of course the evaluation will always be true

    – Abana Clara
    Nov 14 '18 at 0:27













  • Ok, so what should I compare?

    – Aditya Gorti
    Nov 14 '18 at 0:34

















Well you're comparing innerHTML. All of these have empty innerHTML. Of course the evaluation will always be true

– Abana Clara
Nov 14 '18 at 0:27







Well you're comparing innerHTML. All of these have empty innerHTML. Of course the evaluation will always be true

– Abana Clara
Nov 14 '18 at 0:27















Ok, so what should I compare?

– Aditya Gorti
Nov 14 '18 at 0:34





Ok, so what should I compare?

– Aditya Gorti
Nov 14 '18 at 0:34












2 Answers
2






active

oldest

votes


















1














compare with the class attribute



if (shuffleCards[i].classList.value === (shuffleCards[0].classList.value)) {





share|improve this answer
























  • This worked perfectly, thanks!

    – Aditya Gorti
    Nov 15 '18 at 4:16



















0














Well you're comparing innerHTML. All of these have empty innerHTML. Of course the evaluation will always be true.



Compare using element.className or element.classList.contains("fa-diamond") or elements[i].classList.contains(elements[0].className).



if(shuffleCards[i].className == shuffleCards[0].className){
console.log('i is' + shuffleCards[i].innerHTML + 'and 0 is' + shuffleCards[0].innerHTML)
listAddCard.item(i).classList.add('match');
}


or



if(shuffleCards[i].classList.contains(shuffleCards[0].className)){
console.log('i is' + shuffleCards[i].innerHTML + 'and 0 is' + shuffleCards[0].innerHTML)
listAddCard.item(i).classList.add('match');
}


or



if(shuffleCards[i].classList.contains('fa-diamond')){
console.log('i is' + shuffleCards[i].innerHTML + 'and 0 is' + shuffleCards[0].innerHTML)
listAddCard.item(i).classList.add('match');
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291430%2fim-unable-to-compare-two-array-items-in-javscript%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    compare with the class attribute



    if (shuffleCards[i].classList.value === (shuffleCards[0].classList.value)) {





    share|improve this answer
























    • This worked perfectly, thanks!

      – Aditya Gorti
      Nov 15 '18 at 4:16
















    1














    compare with the class attribute



    if (shuffleCards[i].classList.value === (shuffleCards[0].classList.value)) {





    share|improve this answer
























    • This worked perfectly, thanks!

      – Aditya Gorti
      Nov 15 '18 at 4:16














    1












    1








    1







    compare with the class attribute



    if (shuffleCards[i].classList.value === (shuffleCards[0].classList.value)) {





    share|improve this answer













    compare with the class attribute



    if (shuffleCards[i].classList.value === (shuffleCards[0].classList.value)) {






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 14 '18 at 0:45









    Gabriel LopezGabriel Lopez

    2526




    2526













    • This worked perfectly, thanks!

      – Aditya Gorti
      Nov 15 '18 at 4:16



















    • This worked perfectly, thanks!

      – Aditya Gorti
      Nov 15 '18 at 4:16

















    This worked perfectly, thanks!

    – Aditya Gorti
    Nov 15 '18 at 4:16





    This worked perfectly, thanks!

    – Aditya Gorti
    Nov 15 '18 at 4:16













    0














    Well you're comparing innerHTML. All of these have empty innerHTML. Of course the evaluation will always be true.



    Compare using element.className or element.classList.contains("fa-diamond") or elements[i].classList.contains(elements[0].className).



    if(shuffleCards[i].className == shuffleCards[0].className){
    console.log('i is' + shuffleCards[i].innerHTML + 'and 0 is' + shuffleCards[0].innerHTML)
    listAddCard.item(i).classList.add('match');
    }


    or



    if(shuffleCards[i].classList.contains(shuffleCards[0].className)){
    console.log('i is' + shuffleCards[i].innerHTML + 'and 0 is' + shuffleCards[0].innerHTML)
    listAddCard.item(i).classList.add('match');
    }


    or



    if(shuffleCards[i].classList.contains('fa-diamond')){
    console.log('i is' + shuffleCards[i].innerHTML + 'and 0 is' + shuffleCards[0].innerHTML)
    listAddCard.item(i).classList.add('match');
    }





    share|improve this answer




























      0














      Well you're comparing innerHTML. All of these have empty innerHTML. Of course the evaluation will always be true.



      Compare using element.className or element.classList.contains("fa-diamond") or elements[i].classList.contains(elements[0].className).



      if(shuffleCards[i].className == shuffleCards[0].className){
      console.log('i is' + shuffleCards[i].innerHTML + 'and 0 is' + shuffleCards[0].innerHTML)
      listAddCard.item(i).classList.add('match');
      }


      or



      if(shuffleCards[i].classList.contains(shuffleCards[0].className)){
      console.log('i is' + shuffleCards[i].innerHTML + 'and 0 is' + shuffleCards[0].innerHTML)
      listAddCard.item(i).classList.add('match');
      }


      or



      if(shuffleCards[i].classList.contains('fa-diamond')){
      console.log('i is' + shuffleCards[i].innerHTML + 'and 0 is' + shuffleCards[0].innerHTML)
      listAddCard.item(i).classList.add('match');
      }





      share|improve this answer


























        0












        0








        0







        Well you're comparing innerHTML. All of these have empty innerHTML. Of course the evaluation will always be true.



        Compare using element.className or element.classList.contains("fa-diamond") or elements[i].classList.contains(elements[0].className).



        if(shuffleCards[i].className == shuffleCards[0].className){
        console.log('i is' + shuffleCards[i].innerHTML + 'and 0 is' + shuffleCards[0].innerHTML)
        listAddCard.item(i).classList.add('match');
        }


        or



        if(shuffleCards[i].classList.contains(shuffleCards[0].className)){
        console.log('i is' + shuffleCards[i].innerHTML + 'and 0 is' + shuffleCards[0].innerHTML)
        listAddCard.item(i).classList.add('match');
        }


        or



        if(shuffleCards[i].classList.contains('fa-diamond')){
        console.log('i is' + shuffleCards[i].innerHTML + 'and 0 is' + shuffleCards[0].innerHTML)
        listAddCard.item(i).classList.add('match');
        }





        share|improve this answer













        Well you're comparing innerHTML. All of these have empty innerHTML. Of course the evaluation will always be true.



        Compare using element.className or element.classList.contains("fa-diamond") or elements[i].classList.contains(elements[0].className).



        if(shuffleCards[i].className == shuffleCards[0].className){
        console.log('i is' + shuffleCards[i].innerHTML + 'and 0 is' + shuffleCards[0].innerHTML)
        listAddCard.item(i).classList.add('match');
        }


        or



        if(shuffleCards[i].classList.contains(shuffleCards[0].className)){
        console.log('i is' + shuffleCards[i].innerHTML + 'and 0 is' + shuffleCards[0].innerHTML)
        listAddCard.item(i).classList.add('match');
        }


        or



        if(shuffleCards[i].classList.contains('fa-diamond')){
        console.log('i is' + shuffleCards[i].innerHTML + 'and 0 is' + shuffleCards[0].innerHTML)
        listAddCard.item(i).classList.add('match');
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 0:41









        Abana ClaraAbana Clara

        1,621919




        1,621919






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291430%2fim-unable-to-compare-two-array-items-in-javscript%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            List item for chat from Array inside array React Native

            Thiostrepton

            Caerphilly