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

2.4 KiB

id, title, challengeType, videoUrl
id title challengeType videoUrl
5690307fddb111c6084545d7 Logical Order in If Else Statements 1 https://scrimba.com/c/cwNvMUV

Description

Order is important in if, else if statements. The function is executed from top to bottom so you will want to be careful of what statement comes first. Take these two functions as an example. Here's the first:
function foo(x) {
  if (x < 1) {
    return "Less than one";
  } else if (x < 2) {
    return "Less than two";
  } else {
    return "Greater than or equal to two";
  }
}

And the second just switches the order of the statements:

function bar(x) {
  if (x < 2) {
    return "Less than two";
  } else if (x < 1) {
    return "Less than one";
  } else {
    return "Greater than or equal to two";
  }
}

While these two functions look nearly identical if we pass a number to both we get different outputs.

foo(0) // "Less than one"
bar(0) // "Less than two"

Instructions

Change the order of logic in the function so that it will return the correct statements in all cases.

Tests

tests:
  - text: <code>orderMyLogic(4)</code> should return "Less than 5"
    testString: assert(orderMyLogic(4) === "Less than 5", '<code>orderMyLogic(4)</code> should return "Less than 5"');
  - text: <code>orderMyLogic(6)</code> should return "Less than 10"
    testString: assert(orderMyLogic(6) === "Less than 10", '<code>orderMyLogic(6)</code> should return "Less than 10"');
  - text: <code>orderMyLogic(11)</code> should return "Greater than or equal to 10"
    testString: assert(orderMyLogic(11) === "Greater than or equal to 10", '<code>orderMyLogic(11)</code> should return "Greater than or equal to 10"');

Challenge Seed

function orderMyLogic(val) {
  if (val < 10) {
    return "Less than 10";
  } else if (val < 5) {
    return "Less than 5";
  } else {
    return "Greater than or equal to 10";
  }
}

// Change this value to test
orderMyLogic(7);

Solution

function orderMyLogic(val) {
  if(val < 5) {
    return "Less than 5";
  } else if (val < 10) {
    return "Less than 10";
  } else {
    return "Greater than or equal to 10";
  }
}