lundi 10 août 2020

Comparison operator ' === ' and ' == ' does not work in Javascript. Why isn't it? [closed]

I'm having trouble for both comment sections "Display incorrect cells in 'color'". My school assignment requires I use the code below. I only pasted in the parts I'm having issues with.

I can confirm that the Event Listener, function, For loop, and If stmt works. I'm having an issue with the comparison operator ' === '. Also, ' == ' doesn't work. However, I've tried ' != ' and it does work (but not the way I want it too).

My best guess is that it might have to do with the data types being compared. I'm looking for insight to why '===' and '==' are not working because the book solution shows visuals that it works.

window.onload = init;
function init(){
   setupPuzzle(); 

function setupPuzzle(){               
   puzzleCells = document.querySelectorAll("table#hanjieGrid td");      
   
   
   var filled = document.querySelectorAll("table#hanjieGrid td.filled");
   var empty = document.querySelectorAll("table#hanjieGrid td.empty");
   
    //Display incorrect cells in pink
   document.getElementById("peek").addEventListener("click", function(){
      for(var i = 0; i < filled.length; i++){
         if(filled[i].style.backgroundColor === "rgb(255,255,255)"){      
            filled[i].style.backgroundColor = "rgb(255,211,211)";          
         }  
      }                                                                          
      //Display incorrect gray cells in red
      for(var i =0; i < empty.length; i++){
         if(empty[i].style.backgroundColor === "rgb(101,101,101)"){     
            empty[i].style.backgroundColor = "rgb(255,101,101)";    
         }
      }
    }
  );
}

Aucun commentaire:

Enregistrer un commentaire