1. 33

Is it ever possible that (a ==1 && a== 2 && a==3) could evaluate to true, in JavaScript?

  1.  

  2. 19

    I don’t know what’s worse, the (ab)use of JavaScript noted in the top 3 answers, or the fact that someone thought that this was a good interview question to pose.

    1. 3

      It is a great opportunity to showcase js wizardry, though.

      So I think it is a fair question.

      1. 2

        “Wizardy”, which if ever actually used would only lead to confusion and bugs. If anything, the interviewer should expect the interviewed person to add that this should never be used, even if it’s possible.

        1. 5

          At the same time, it’s that sort of deep knowledge that is used to break code. Knowing what’s possibly in play when the impossible seems to happening is a good skill to have. It does seem a bit excessive for an interview question. Unless the job is developing a JS library, then it may be relevant.

    2. 6

      If anyone asks you this question (or one like it) in an interview, walk out.

      1. 2

        In what other languages would it be possible?

        I guess everything with properties (functions disguised as fields) so D, C#, etc.

        Afaik not with C, C++, or Java.

        1. 26
          #define a (++i)
          int i = 0;
          
          if (a == 1 && a == 2 && a == 3)
              ....
          
          1. 1

            Isn’t that undefined behavior? Or is && a sequence point?

            1. 3

              && and || are sequence points. The right expression may never happen depending on the result of the left, so it would make things interesting if they weren’t.

          2. 10

            This is very easy to do in C++.

            1. 5

              You can also do it with Haskell.

              1. 3

                Doable with Java (override the equals method), and as an extension, with Clojure too:

                (deftype Anything []
                  Object
                  (equals [a b] true))
                
                (let [a (Anything.)]
                  (when (and (= a 1) (= a 2) (= a 3))
                    (println "Hello world!")))
                

                Try it!

                Or, inspired by @zge above:

                (let [== (fn [& _] true)
                      a 1]
                  (and (== a 1) (== a 2) (== a 3)))
                
                1. 3

                  Sort of. In Java, == doesn’t call the equals method, it just does a comparison for identity. So

                   a.equals(1) && a.equals(2) && a.equals(3); 
                  

                  can be true, but never

                   a == 1 && a == 2 && a == 3;
                  
                  1. 1

                    But wouldn’t you have

                2. 3

                  perl can do it very simply

                  my $i = 0;
                  sub a {
                  	return ++$i;
                  }
                  
                  if (a == 1 && a == 2 && a == 3) {
                  	print("true\n");
                  }
                  
                  1. 2

                    Here is a C# version.

                    using System;
                    
                    namespace ContrivedExample
                    {
                        public sealed class Miscreant
                        {
                            public static implicit operator Miscreant(int i) => new Miscreant();
                    
                            public static bool operator ==(Miscreant left, Miscreant right) => true;
                    
                            public static bool operator !=(Miscreant left, Miscreant right) => false;
                        }
                    
                        internal static class Program
                        {
                            private static void Main(string[] args)
                            {
                                var a = new Miscreant();
                                bool broken = a == 1 && a == 2 && a == 3;
                                Console.WriteLine(broken);
                            }
                        }
                    }
                    
                    1. 2

                      One of the ‘tricks’ where all a’s are different Unicode characters is possible with Python and Ruby. Probably in Golang too.

                      1. 7

                        In python, you can simply create class with __eq__ method and do whatever you want.

                        1. 4

                          Likewise in ruby, trivial to implement

                          a = Class.new do
                            def ==(*)
                              true
                            end
                          end.new
                          
                          a == 1 # => true
                          a == 2 # => true
                          a == 3 # => true
                          
                      2. 2

                        In Scheme you could either take the lazy route and do (note the invariance of the order or ammount of the operations):

                        (let ((= (lambda (a b) #t))
                               (a 1))
                          (if (or (= 1 a) (= 2 a) (= 3 a))
                              "take that Aristotle!"))
                        

                        Or be more creative, and say

                        (let ((= (lambda (x _) (or (map (lambda (n) (= x n)) '(1 2 3)))))
                                (a 1))
                            (if (or (= 1 a) (= 2 a) (= 3 a))
                                "take that Aristotle!"))
                        

                        if you would want = to only mean “is equal to one, two or three”, instead of everything is “everything is equal”, of course only within this let block. The same could also be done with eq?, obviously.

                        1. 1

                          Here is a Swift version that uses side effects in the definition of the == operator.

                          import Foundation
                          
                          internal final class Miscreant {
                              private var value = 0
                              public static func ==(lhs: Miscreant, rhs: Int) -> Bool {
                                  lhs.value += 1
                                  return lhs.value == rhs
                              }
                          }
                          
                          let a = Miscreant()
                          print(a == 1 && a == 2 && a == 3)