-1

Testing the last 6 characters in a string derived from document.images[].src. My "if" condition statement always passes the test, even when they should fail. The variable "test_result" always has a value of 3 at the end of the function. I have tried using "==" in the condition statement with the same result. Thanks for your help

function test_it()
{
   var test_result = 0;
   var test1 = document.images[0].src;
   test1 = test1.substring(test1.length, test1.length - 6);
   if (test1 = "52.gif")
   {
     test_result ++
   }

   var test2 = document.images[1].src;
   test2 = test2.substring(test2.length, test2.length - 6);
   if (test2 ="48.gif")
   {
      test_result ++
   }

   var test3 = document.images[2].src;
   test3 = test3.substring(test3.length, test3.length - 6);
   if (test3 = "47.gif")
   {
      test_result ++
   }

   if (test_result = 3)
   {
      document.getElementById("ta").value = test1 + " " + test2 + " " + test3 + " " +     test_result
   }
   else
   {
      return
   }
}
Rellif
  • 79
  • 1
  • 1
  • 11

6 Answers6

1

The identity (===) operator behaves identically to the equality (==) operator except no type conversion is done, and the types must be the same to be considered equal.

Reference: Javascript Tutorial: Comparison Operators

The == operator will compare for equality after doing any necessary type conversions. The === operator will not do the conversion, so if two values are not the same type === will simply return false. It's this case where === will be faster, and may return a different result than ==. In all other cases performance will be the same.

To quote Douglas Crockford's excellent JavaScript: The Good Parts,

JavaScript has two sets of equality operators: `===` and `!==`, and their evil twins `==` and `!=`. The good ones work the way you would expect. If the two operands are of the same type and have the same value, then `===` produces true and `!==` produces false. The evil twins do the right thing when the operands are of the same type, but if they are of different types, they attempt to coerce the values. the rules by which they do that are complicated and unmemorable. These are some of the interesting cases:

'' == '0'           // false
0 == ''             // true
0 == '0'            // true

false == 'false'    // false
false == '0'        // true

false == undefined  // false
false == null       // false
null == undefined   // true

' \t\r\n ' == 0     // true
The lack of transitivity is alarming. My advice is to never use the evil twins. Instead, always use === and !==. All of the comparisons just shown produce false with the === operator.

Source: Which equals operator (== vs ===) should be used in JavaScript comparisons?

Community
  • 1
  • 1
softvar
  • 17,917
  • 12
  • 55
  • 76
  • Even if I think that in this case is probably a good advice, I don't agree with the "use always" part. If you are confident that you are comparing values of the same type is better to uses their "evil twins" as you named them or when you don't know or care of the type of the argument given to the function, e.g. function ifFive(x){ if(x === 5) doStuff(); } ; will fail in ifFive(myInput.value) ; even if the value is five, which is indeed what you want, and not the type itself. – Juan Garcia Mar 13 '14 at 20:57
0

Replace = with === in your if statements.

There's a good (or bad) coding style for writing if conditions called YODA Notation that could help:

Instead of writing:

if (test1 === "52.gif")

Write:

if ("52.gif" === test1)

Now, if you accidentally use assignment operator instead of equality operator, you'll get a ReferenceError.

fardjad
  • 20,031
  • 6
  • 53
  • 68
  • +1 for recommending Yoda conditions, -1 for not warning of the side effects of the constant resolving to false. – Juan Garcia Mar 13 '14 at 21:11
0

The = operator is used to assign a value to a variable, use == or === to compare two values.

Peter
  • 7,020
  • 4
  • 31
  • 51
  • Exact answer. Thank you. I had tried == on the condtition but did not on the final test of "test_result". – Rellif Mar 13 '14 at 20:48
0

You must use the equality or strict equality comparison operators, namely == or === respectively. A good description of the differences can be found here Comparison Operators:

Equal (==)

If the two operands are not of the same type, JavaScript converts the operands, then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers if possible; else if either operand is a string, the string operand is converted to a number if possible. If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory.

Strict equal (===)

Returns true if the operands are strictly equal (see above) with no type conversion.

Peter
  • 3,998
  • 24
  • 33
0

Use == (double equal - with type conversion if necessary) or === (tiple equal - without type conversion)

See this link:

Which equals operator (== vs ===) should be used in JavaScript comparisons?

Community
  • 1
  • 1
Guillermo Zacur
  • 321
  • 2
  • 5
0

Your test condition is always true, and you are testing 3 times, then test_result will be always 3 at the end.

The = operator assign the value to the variable, so

if (test1 = "52.gif")
{
  test_result ++
}

is the same as

test1 = "52.gif" ;
if (test1) // Always true
{
  test_result ++
}

To compare by value regardless of the type you need to use the == "is equal to" operator. With this operator expressions like (1 == 1) is true and (1 == "1") is also true. To force type check then you neeed to use the === operator, then (1 === 1) is true but (1 === "1") is false.

Change your if statements to:

if (test1 == "52.gif")
{
  test_result ++
}
Juan Garcia
  • 714
  • 6
  • 23