0

I am working on studying for an entry test, and being self learned I have been working a lot of functions problems. this one has stumped me, I a to write a function testing to see if 3 values are equal. The code i have tired is:

Function equal(a,b,c){ 
  return a==b==c; 
}

as well as:

function equal(a,b,c){
  let newEqual=a==b;
  return newEqual===c
}

I feel like I am missing something rather simple but have not been able to put my finger on it. thank you in advance for any insight

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Sean A
  • 5
  • 2
  • What is your specific question? – Dave Sep 21 '20 at 17:45
  • Function needs a new, please read [Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) vs [functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions) – mplungjan Sep 21 '20 at 17:45
  • 1
    Also read [== Equality](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality) vs [===Identity or strict equality](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality) operators – mplungjan Sep 21 '20 at 17:47
  • Does this [question](https://stackoverflow.com/questions/9973323/javascript-compare-3-values) answer your question. – flaxel Sep 21 '20 at 17:49
  • All, this is not a "do my homework for me" site. Only specific questions should be answered. – Dave Sep 21 '20 at 17:53

4 Answers4

1

a == b == c will be evaluated as :

a == b then checks the result ( true ) and compares it with c => true == c which is false :

const a = 5;
const b = 5;
const c = 5;

const result = a == b == c ;

console.log(result); // false

const a1 = 5;
const b1 = 5;
const c1 = true;

const result1 = a1 == b1 == c1 ;

console.log(result1); // true

You should compare them separately :

const a = 5;
const b = 5;
const c = 5;

const result = a == b && b == c ;

console.log(result);
Taki
  • 17,320
  • 4
  • 26
  • 47
  • 1
    [== Equality](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality) vs [===Identity or strict equality](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality) operators – mplungjan Sep 21 '20 at 17:48
0

To check whether all three variables are equal or not, use && operator to add on queries.

&& returns true if all conditions are true.

function equal(a,b,c){
    return a == b && b == c;
}

As mentioned by @mplungjan, for strict comparison use === instead of ==.

Dark Knight
  • 6,116
  • 1
  • 15
  • 37
  • I did have to switch to the following a===b&&b===c so that the result would come up false if there was a string element ex('1',2,3) needed to return false, but thank you for your help. This set me where I needed to go. – Sean A Sep 21 '20 at 18:06
0

Try this.

function equal(a,b,c){ 
  return a==b && b==c;
}
0

You can check it by doing the following:

function equal(a,b,c){
  return (a==b) && (b==c)
}

That way you are checking if a == b is true, and b == c is true then all three are equal. In other words, true && true = true

Hussam Ahmed
  • 413
  • 1
  • 5
  • 17