Randell Dawson 05f73ca409 fix(curriculum): Convert blockquote elements to triple backtick syntax for JavaScript Algorithms and Data Structures (#35992)
* fix: convert js algorithms and data structures

* fix: revert some blocks back to blockquote

* fix: reverted comparison code block to blockquotes

* fix: change js to json

Co-Authored-By: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>

* fix: convert various section to triple backticks

* fix: Make the formatting consistent for comparisons
2019-05-17 08:20:30 -05:00

3.3 KiB

id, title, challengeType, videoUrl
id title challengeType videoUrl
56533eb9ac21ba0edf2244d8 Comparisons with the Logical And Operator 1 https://scrimba.com/c/cvbRVtr

Description

Sometimes you will need to test more than one thing at a time. The logical and operator (&&) returns true if and only if the operands to the left and right of it are true. The same effect could be achieved by nesting an if statement inside another if:
if (num > 5) {
  if (num < 10) {
    return "Yes";
  }
}
return "No";

will only return "Yes" if num is greater than 5 and less than 10. The same logic can be written as:

if (num > 5 && num < 10) {
  return "Yes";
}
return "No";

Instructions

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".

Tests

tests:
  - text: You should use the <code>&&</code> operator once
    testString: assert(code.match(/&&/g).length === 1, 'You should use the <code>&&</code> operator once');
  - text: You should only have one <code>if</code> statement
    testString: assert(code.match(/if/g).length === 1, 'You should only have one <code>if</code> statement');
  - text: <code>testLogicalAnd(0)</code> should return "No"
    testString: assert(testLogicalAnd(0) === "No", '<code>testLogicalAnd(0)</code> should return "No"');
  - text: <code>testLogicalAnd(24)</code> should return "No"
    testString: assert(testLogicalAnd(24) === "No", '<code>testLogicalAnd(24)</code> should return "No"');
  - text: <code>testLogicalAnd(25)</code> should return "Yes"
    testString: assert(testLogicalAnd(25) === "Yes", '<code>testLogicalAnd(25)</code> should return "Yes"');
  - text: <code>testLogicalAnd(30)</code> should return "Yes"
    testString: assert(testLogicalAnd(30) === "Yes", '<code>testLogicalAnd(30)</code> should return "Yes"');
  - text: <code>testLogicalAnd(50)</code> should return "Yes"
    testString: assert(testLogicalAnd(50) === "Yes", '<code>testLogicalAnd(50)</code> should return "Yes"');
  - text: <code>testLogicalAnd(51)</code> should return "No"
    testString: assert(testLogicalAnd(51) === "No", '<code>testLogicalAnd(51)</code> should return "No"');
  - text: <code>testLogicalAnd(75)</code> should return "No"
    testString: assert(testLogicalAnd(75) === "No", '<code>testLogicalAnd(75)</code> should return "No"');
  - text: <code>testLogicalAnd(80)</code> should return "No"
    testString: assert(testLogicalAnd(80) === "No", '<code>testLogicalAnd(80)</code> should return "No"');

Challenge Seed

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

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

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

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

Solution

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