freeCodeCamp/curriculum/challenges/english/02-javascript-algorithms-an.../basic-javascript/chaining-if-else-statements...

3.7 KiB

id title challengeType
56533eb9ac21ba0edf2244dc Chaining If Else Statements 1

Description

if/else statements can be chained together for complex logic. Here is pseudocode of multiple chained if / else if statements:
if (condition1) {
  statement1
} else if (condition2) {
  statement2
} else if (condition3) {
  statement3
. . .
} else {
  statementN
}

Instructions

Write chained if/else if statements to fulfill the following conditions: num < 5 - return "Tiny"
num < 10 - return "Small"
num < 15 - return "Medium"
num < 20 - return "Large"
num >= 20 - return "Huge"

Tests

tests:
  - text: You should have at least four <code>else</code> statements
    testString: assert(code.match(/else/g).length > 3, 'You should have at least four <code>else</code> statements');
  - text: You should have at least four <code>if</code> statements
    testString: assert(code.match(/if/g).length > 3, 'You should have at least four <code>if</code> statements');
  - text: You should have at least one <code>return</code> statement
    testString: assert(code.match(/return/g).length >= 1, 'You should have at least one <code>return</code> statement');
  - text: <code>testSize(0)</code> should return "Tiny"
    testString: assert(testSize(0) === "Tiny", '<code>testSize(0)</code> should return "Tiny"');
  - text: <code>testSize(4)</code> should return "Tiny"
    testString: assert(testSize(4) === "Tiny", '<code>testSize(4)</code> should return "Tiny"');
  - text: <code>testSize(5)</code> should return "Small"
    testString: assert(testSize(5) === "Small", '<code>testSize(5)</code> should return "Small"');
  - text: <code>testSize(8)</code> should return "Small"
    testString: assert(testSize(8) === "Small", '<code>testSize(8)</code> should return "Small"');
  - text: <code>testSize(10)</code> should return "Medium"
    testString: assert(testSize(10) === "Medium", '<code>testSize(10)</code> should return "Medium"');
  - text: <code>testSize(14)</code> should return "Medium"
    testString: assert(testSize(14) === "Medium", '<code>testSize(14)</code> should return "Medium"');
  - text: <code>testSize(15)</code> should return "Large"
    testString: assert(testSize(15) === "Large", '<code>testSize(15)</code> should return "Large"');
  - text: <code>testSize(17)</code> should return "Large"
    testString: assert(testSize(17) === "Large", '<code>testSize(17)</code> should return "Large"');
  - text: <code>testSize(20)</code> should return "Huge"
    testString: assert(testSize(20) === "Huge", '<code>testSize(20)</code> should return "Huge"');
  - text: <code>testSize(25)</code> should return "Huge"
    testString: assert(testSize(25) === "Huge", '<code>testSize(25)</code> should return "Huge"');

Challenge Seed

function testSize(num) {
  // Only change code below this line


  return "Change Me";
  // Only change code above this line
}

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

Solution

function testSize(num) {
  if (num < 5) {
    return "Tiny";
  } else if (num < 10) {
    return "Small";
  } else if (num < 15) {
    return "Medium";
  } else if (num < 20) {
    return "Large";
  } else {
    return "Huge";
  }
}