24
It is possible that (a == 1 && a == 2 && a == 3)
can be evaluated as true
?
This is an interview question asked by a major technology company. I’m trying to find the answer. I know we never write that code in our day-to-day, but I’m curious.
24
It is possible that (a == 1 && a == 2 && a == 3)
can be evaluated as true
?
This is an interview question asked by a major technology company. I’m trying to find the answer. I know we never write that code in our day-to-day, but I’m curious.
35
It is possible if a
is a constant object that returns values that are incremented each time the object is invoked. In the case below:
const a={
valor: 1,
valueOf: function(){
return a.valor++;
}
}
On the first invocation of a
he will return 1
. After that, every time a
is called, will return the last value +1
(1, 2 , 3...).
Soon, (a == 1 && a == 2 && a == 3)
will be true, because a
will be equal to 1, 2 and 3 constant returns a
.
When I call upon a == 1
, the value of a
is 1
(true) and the a
becomes valuable 2
, soon a == 2
is also true, and so on.
To illustrate, could do so also that the result is the same:
const a={
valor: 1,
valueOf: function(){
return a.valor++;
}
}
if(a == 1 && a == 2 && a == 3 && a == 4 && a == 5){
// até esse "a" no console.log abaixo já incrementa o valor
console.log("fim. O valor de a é "+a);
}
There is a similar question in Soen.
I just don’t understand the part where you say the answer on Soen says it’s because of a short circuit. From what I’ve read, he claims this works due to loose comparison (Loose Equality) where only the value of a
, but not its type which is where it would give problem when comparing an object with an integer.
"It has nothing to do with short-circuit (short-circuit) as you propose..." I don’t see any mention of short-Circuit in the quoted answer, nor can I understand in what sense you say you are wrong/wrong, since it is the same as yours! (changing the toString
for valueOf
)
@Isac It’s true, I ended up getting confused with another answer I saw elsewhere rs. I think it was sleep.
similar question no, identical question.
Browser other questions tagged javascript
You are not signed in. Login or sign up in order to post.
The name of it is INPUT.
– Victor Stafusa
Interestingly, I read the INPUT, I’ll read the rest later.
– NoobSaibot
In my opinion, that’s a terrible interview question. No one is obliged to keep thinking of cases as messy as the answer that uses an object. When this will be useful in the employee’s practical life?
– bfavaretto
Such questions serve to measure speed and ability of logical reasoning and propose solutions to problems. In addition to confronting the condition as to its self-control, autodidatism, determination to solve problems, communication and expression, critical, synthetic, objective thinking. Don’t underestimate a silly question of programming logic, because it says a lot about who you really are.
– Paulo Dos Santos