Please help me understand this comparison problem

Please help me understand this comparison problem
0

#1

Assign the variables to values so that the following statements evaluate as described:

// Expression A
(x + y) > z || a == b || ((l || m) && n) // resolves to true

// Expression B
!!n && (x + y + z) && a // resolves to false

// Expression C
m[n].length + n === 1 // resolves to true

let a = 1;
let b = 1;
let l = 0;
let m = 1;
let n = 0;
let x = 1;
let y = 1;
let z = 1;

I don’t understand why I have a problem in the console with the .length property. I don’t even understand how (x + y + z) in expression B can be true or false. Please someone help me out.


#2

The operands for && and || don’t necessarily have to be explicitly true or false. What matters is their “truthiness” (i.e., whether they convert to true or false). It’s perfectly valid to have something like (x + y + z) as their operand.

m is a number. As far as I know, using bracket notation on a number is meaningless (unlike in strings, arrays, or objects), and you get undefined, which has no properties (like length) whatsoever


#3

So there is a problem with this problem then?

And how can (x + y + z) not be true? Wouldn’t numbers always be true?


#4

Not if x+y+z evaluates to 0 I believe.


#5

#6

this problem looks cool. Where did you find it?

I was looking at your m[n] line. It looks like m should be an array or a string?
and n should also be a number?
so for eg.

let m = “11”;
let n = 0;

if we plug those into expression c we get:
m[n].length + n

m[n] is m[0] which is 1
1.length is 1
1 + n is
1 + 0
which is 1

something like that?


#7

“m[0] which is 1”

How?
And as stated before, the .length property won’t work on a number.


#8

sorry it was a string , see the edited comment for my repl.it


#9

Where did this problem come from? Is it on the web? Do you have a link to it?


#10

In the following expression (taken from what you post), the overall expression evaluates to false, because when all the operators are &&, it only takes a single false to make the entire expression false.

!!n && (x + y + z) && a

Yes, the (x + y + z) which is 3 evaluates to true
Yes, the a which is 1 evaluates to true
However, the !!n evaluates to false, because n is 0 which is false, but the ! to the left makes it true and then the other ! to the left negates the true to become false again.


#11

I asked the OP as well and he said it was from a bootcamp he will be participating in. Still wondering though why they are giving the OP questions before the bootcamp starts.


#12

There are many ways to solve this. Starting from your current values, you can solve the challenge by changing one of the variables. You need to change it to an array, and you need to add at least one element to that array, and that element also can’t be a number (the element could be a string or another array nested inside the first).