-1

I want to use JavaScript to determine which class/display color a number of divs(called div1, div2...up through div21) are depending on the values in schedule_array. So if 2 is in schedule_array, I want div2 to be of class clicked and if 3 is not in schedule_array, I want div3 to be of class unclicked. The problem is that my === sign seems to be giving false no matter what for this code:

 function setClasses()
 {
     alert("schedule array is "+schedule_array[3]);

     for(var k = 1; k<22; k++)
     {
         var name = "div" + k;
         if(contains(schedule_array, k))
         {
             document.getElementById(name).className = "clicked";
         }
         else
         {
             document.getElementById(name).className = "unclicked";
         }
     }
 }

 setClasses();


 function contains(a, obj) {
     //alert(a[3] + "for function contains");
     //alert(a.length);
     for (var i = 0; i < a.length; i++) {
         document.write(a[i] + "=" + obj);
         if (a[i] === obj) {
             document.write("true,,,,,,,,");
             return true;
         }
         else
         {
             document.write("false,,,,,,,,");
         }
     }

     return false;

 }
(stole this latter function from http://stackoverflow.com/questions/237104/array-containsobj-in-javascript)

I have no idea why, but the following code's == fails even when I am comparing the same integer to itself. For example with the code below I get output like:

21=21false,,,,,, 

As you can see I've already checked to make sure schedule_array exists and has valid numbers. I have also checked to make sure both for loops are running. Finally, I confirmed that in js, even something like "5"==5 should give a true, so even if there is some weird typing going on, that shouldn't affect the outcome.

Question: what is up with this weirdness? What am I doing wrong?

j08691
  • 204,283
  • 31
  • 260
  • 272
Fawn
  • 153
  • 1
  • 9
  • 5
    The code you posted is using `===`, not `==`, so no type conversion will happen and the number `5` will not compare as equal to the string `"5"`. – Pointy Feb 04 '15 at 21:02
  • @Pointy - that's the answer - post it as an answer so you can be upvoted and this question can benefit others – Duniyadnd Feb 04 '15 at 21:04
  • 2
    @Duniyadnd yes I'm familiar with the process, but I've learned through experience that jumping in with an answer in a case like this is often met with "oops that was just a typo when I typed in the question". Hopefully the OP will clarify whether or not that is **really** the code involved. – Pointy Feb 04 '15 at 21:05
  • After @Pointy : `===` means : they're exactly the same type & they have exactly the same value. While `==` means they're compareable (after type conversion). Since one is a string (`"5"`) and second is a number (`5`), it will return `false` on `===` as they're not equal in type. Use `==` instead. **[SO](http://stackoverflow.com/questions/359494/does-it-matter-which-equals-operator-vs-i-use-in-javascript-comparisons)** – Artur Filipiak Feb 04 '15 at 21:10
  • @Pointy - point taken. – Duniyadnd Feb 04 '15 at 21:37

2 Answers2

3

This works for me.

function contains(a, obj) {
    for (var i = 0; i < a.length; i++) {
        if (a[i] == obj) {
            return true;
        }
    }
    return false;
}

alert(contains(['1', 2, 3, 4, 5], 1));

Are you sure that a is an array?

http://jsfiddle.net/kjruk00b/

Bitwise Creative
  • 4,035
  • 5
  • 27
  • 35
1

You are using === which is a equality without conversion (strict equality). This means that, that although the numeric value is the same (equality) the strict equality operator is not coercing the String into a Integer to allow this comparison to happen. Switching to the non-strict equality operator == should solve your problem as it will convert the String into an Integer, allowing the comparison to work as you expected.

It may be worth your while looking at the Javascript truth tables for these 2 operators to get an idea for how they work.

Steve U
  • 326
  • 2
  • 7