JSFiddle - React, Tailwind, and code Playground
by Ryan Morris
HTML
<h1>Control Flow Exercise</h1>
JavaScript
/****************************************************************************/
// EXERCISE 1:
//
// In the function below, return true if the input variable is equal
// to the number 42. Otherwise return false. Use an `if' statement
// to achieve this.
function Exercise1 (input) {
if (input === 42) {
return true;
}
return false;
}
console.group('Exercise 1');
console.assert(Exercise1(42) === true);
console.assert(Exercise1(1) === false);
console.assert(Exercise1('string') === false);
console.assert(Exercise1(25) === false);
console.assert(Exercise1("42") === false, "String provided, expected number");
console.groupEnd('Exercise 1');
/************************console.assert(Exercise1('string') === false);
****************************************************/
// EXERCISE 2:
//
// In the function below, return true if the input variable is equal
// to the number 42. If the input is the number 43 then return null.
// In all other cases return false.
//
// Use an `if' statement to achieve this.
function Exercise2 (input) {
if (input === 42) {
return true;
}
if (input === 43) {
return null;
}
return false;
}
console.group('Exercise 2');
console.assert(Exercise2(42) === true);
console.assert(Exercise2(43) === null);
console.assert(Exercise2('string') === false);
console.assert(Exercise2(25) === false);
console.groupEnd('Exercise 2');
/****************************************************************************/
// EXERCISE 3:
//
// Repeat exercise 2, this time using a `switch' statement.
function Exercise3 (input) {
switch (input) {
case 42:
return true;
break;
case 43:
return null;
break;
default:
return false;
break;
}
}
console.group('Exercise 3');
console.assert(Exercise3(42) === true);
console.assert(Exercise3(43) === null);
console.assert(Exercise3('string') === false);
console.assert(Exercise3(25) === false);
console.groupEnd('Exercise...