Comparison with the logical and operator exercise question

Tell us what’s happening:
Describe your issue in detail here.
Hi guys! so I’ve been following along with the logical operators prior to this and it’s been pretty straightforward up until I reached this “&&” from what I understand, it’s essentially compressing two operators into one to determine the left and right being right or false. What I don’t understand is why is it asking…" using the && operator, which will return the string Yes if val is less than or equal to 50 and greater than or equal to 25 . Otherwise, will return the string No " And when I look at the hint, the one comment with a solution is showing different numbers like 21 and something else. Can someone explain to me in an example or such, how this works? because I really want to understand it :slight_smile: thank you so much for your time!

  **Your code so far**

function testLogicalAnd(val) {
// Only change code below this line

if (val) {
  if (val) {
    return "Yes";
  }
}

// Only change code above this line
return "No";
}

testLogicalAnd(10);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Safari/605.1.15

Challenge: Comparisons with the Logical And Operator

Link to the challenge:

HI @Sherif123 !

The way the logical and operator works && is that if all conditions are true then the code inside the if statement will execute.

Here is an example of both conditions being true.

function testLogicalAnd(val) {
  if (val > 10 && val < 30) {
    return "Yes"
  }
  return "No";
}
//this function would return Yes for any number between 11 - 29
// You can test this yourself by using the console.log I added and seeing the result printed to the console. 
console.log(testLogicalAnd(24))
testLogicalAnd(24);

Now let’s look at an example if one of the conditions is false

function testLogicalAnd(val) {
  if (val > 10 && val < 30) {
    return "Yes"
  }
  return "No";
}
//Even though the number 9 is less than 30 it is not greater than 10 so the function would return No
// You can test this yourself by using the console.log I added and seeing the result printed to the console. 
console.log(testLogicalAnd(9))
testLogicalAnd(9);

Lastly, let’s look at an example where both conditions are false.

function testLogicalAnd(val) {
  if (val > 10 && typeof val === "string") {
    return "Yes"
  }
  return "No";
}
// The number 10 fails both conditions because 10 is not greater than 10 and 10 is not a string
// You can test this yourself by using the console.log I added and seeing the result printed to the console. 
console.log(testLogicalAnd(10))
testLogicalAnd(10);

I would suggest playing around with different numbers to test your understanding of how the && operator works.

Also, here is a link to the typeof operator. You will learn about this later, but it returns a string indicating what type the operand is. In our example, typeof val === "string" would return false because the number 10 is not a string.
You can also use console.log (typeof val === "string") to see the result for yourself.

Hope that helps!

1 Like

I can’t find anything showing 21. So I don’t understand your problems. Could you show the part of the hint that you feel confused?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.