freeCodeCamp/guide/english/certifications/javascript-algorithms-and-d.../basic-javascript/comparisons-with-the-logica.../index.md

1.5 KiB

title
Comparisons with the && (logical AND) operator

Comparisons with the && (logical AND) operator

Problem explanation:

· Combine the two if statements into one statement which will return "Yes" if val is less than or equal to 50 and greater than or equal to 25. Otherwise, will return "No".

Hint 1

The logical AND (&&) operator compares both statements and returns true only if both are true or can be converted to true (truthy).

try to solve the problem now

Hint 2

Remember that this effect can be also achieved by nesting if statements.

try to solve the problem now

Spoiler alert!

Solution ahead!

Basic code solution:

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

  if (val <= 50 && val >= 25) {
      return "Yes";
  }

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

// Change this value to test
testLogicalAnd(10);

· Run code at repl.it

Code explanation

The function first evaluates if the condition val <= 50 evaluates to true converting val to a number if necessary, then does the same with val >=25 because of the logical AND (&&) operator; if both return true, the return "Yes" statement is executed.

Resources