- Notifications
You must be signed in to change notification settings - Fork0
License
devyjs/skills-based-javascript-intro-to-flow-control-js-intro-000
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
- Write
if
statements in JS - Write
if
-else if
-else
statements in JS - Use the ternary operator in JS
- Write
switch
statements in JS
Flow Control allows the execution of code only under certain conditions.
JavaScript has similar methods to control what blocks of code to execute:if
statements,if
-else
statements,if
-else if
-else
statements, ternary operators, andswitch
statements.
You'll be writing your code inflow-control.js
. Make sure to run the tests usinglearn
.
if
statements look like this:
if(something){// do something}
They work as the name implies:ifsomething
istruthy (so the booleantrue
or anything other than the empty string (''
),0
,null
, orundefined
), the code in between the curly braces runs; if not, the code between the curly braces is skipped.
Now, inflow-control.js
let's write a function calledbasicTeenager
that accepts an age as a parameter. The function should contain an if-statement that checks to see if the age is a teenager. If the age is between 13 and 19, return"You are a teenager!"
You will often see anif
statement used in combination with anelse
clause. Anelse
clause will only get executed if the previousif
statement is falsey.
Syntax:
if(conditionToTest){// executed if `conditionToTest` is truthy}else{// executed if `conditionToTest` is falsey}
- Define a function
teenager
that accepts an age as a parameter. If the age is between 13 and 19 it should return"You are a teenager!"
. Otherwise, the function should return"You are not a teenager"
.
if
statements can also be combined with anelse if
clause. This is like anelse
statement, but with its own condition. It will only run if its condition is true, and the previous statement's condition was false.
if(conditionToTest1`){ // condition is false hence code is not executed} else if (conditionToTest2) { // execute this code if `conditionToTest1`statement is falsey AND `conditionToTest2`istruthy}
You can optionally add a finalelse
statement after all of yourelse if
statements. You can probably guess what will happen: ifall of the other statements are falsey, this finalelse
block will execute; otherwise, an earlier statement executes and theelse
block is skipped.
if(conditionToTest1`){ // condition is false hence code is not executed} else if (conditionToTest2) { // execute this code if `conditionToTest1`statement is falsey AND `conditionToTest2`istruthy}else{// execute this code iff none of the other conditions are met}
- Define a function
ageChecker
that takes in an age as a parameter. If the age is between 13-19 it should return"You are a teenager!"
. If the age is 12 or below, it should return"You are a kid"
. If the age is above 19, it should return"You are a grownup"
Remember the ternary operator? You can think of it as a shortcut for theif-else
statement.
This operator tests a condition; if the condition is truthy, it evaluates the left-hand side of the colon; otherwise it evaluates the right-hand side of the colon.
Syntax:
conditionToTest ?valueToBeReturnedIfTrue :valueToBeReturnedIfFalse
- Define a function
ternaryTeenager
that accepts age as a parameter. The body of the function should use the ternary operator to return"You are a teenager"
if age is between 13-19 and returns"You are not a teenager"
if the age is anything else.
Switch statements acts like a big if/else if/else chain. The switch expression is evaluated once and the value of the expression is compared with the values of each case. If there is a match, the associated block of code is executed.
Syntax:
switch(expression){casen:// code to be executed if case n is truebreak;// break out of switch statement once code executedcasem:// code to be executed if case m is truebreak;// break out of switch statement once code executeddefault:// all other cases// code to be executed if case n and case m false}
Example:
varmood="hungry"switch(mood){case"happy":console.log("Dance to Pharrel's Happy");break;case"sad":console.log("You should eat a pint of icecream");break;case"anxious":console.log("Take some deep breaths");break;case"hungry":console.log("You should eat a big chocolate cake");break;default:console.log("That's not a mood we support");}
In the example above, we'll see"You should eat a big chocolate cake"
printed to the console. If we change the value of themood
variable tosad
you'll see"You should eat a pint of icecream"
. If the value ofmood
changed to"grumpy"
, the default statement would trigger and print out"That's not a mood we support"
.
- Define a function
switchAge
that accepts an age as a parameter. The case statement should switch onage
and return"You are a teenager"
if the age is 13, 14, 15, 16, 17, 18, or 19, and return"You have an age"
as the default.
- Codecademy - if/if else/if else if else
- MDN - if..else
- Codecademy - Ternary Operator
- Codecademy - Switch Statements
ViewIntro To Flow Control in JS on Learn.co and start learning to code for free.
About
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Languages
- JavaScript100.0%