Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

License

NotificationsYou must be signed in to change notification settings

devyjs/skills-based-javascript-intro-to-flow-control-js-intro-000

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Objectives

  • Writeif statements in JS
  • Writeif-else if-else statements in JS
  • Use the ternary operator in JS
  • Writeswitch statements in JS

About

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

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

if-else Statements

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 functionteenager 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/else if Statements

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

Ternary Operator

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 functionternaryTeenager 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

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

Resources

ViewIntro To Flow Control in JS on Learn.co and start learning to code for free.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript100.0%

[8]ページ先頭

©2009-2025 Movatter.jp