Control Structures and Functions

📚 Watch and Read

Control Structures

To solve real-world problems, we use control structures which allow our code to flow logicly similar to the way our brains do. For example, how would you explain the rules of the game "Rock, Paper, Scissors" to a friend who has never heard it? You might say something like:

  1. each player makes a fist
  2. each player shakes their fist three times saying 'Rock, Paper, Scissors'
  3. each player shakes their fist a fourth time and changes their fist to one of the shapes for rock, paper, or scissors
  4. player 1 wins if she chooses rock and player 2 chooses scissors or she chooses paper and player 2 chooses rock or she chooses scissors and player 2 chooses paper

In programming, we use control structures to accomplish logic like this.

After watching the videos above, you may see some places where we could use control structures to write rock, paper, scissors in code.

The rules around who wins could be expressed with if, and, or statements. The && represents AND, the || represents OR.

if (player1 == 'rock' && player2 == 'scissors' ||
    player1 == 'paper' && player2 == 'rock' ||
    player1 == 'scissors' && player2 == 'paper') {
        winner = player1
}

Procedures, Functions, and Libraries

Functions, also called procedures or methods (there are differences between the three, but for our introductory purposes, they serve the same purpose) allow you to right code once and call it again and again without writing it multiple times. In the example above, we could use a function to play many games of Rock, Paper, Scissors.

function game(player1, player2){
    if (player1 == 'rock' && player2 == 'scissors' ||
        player1 == 'paper' && player2 == 'rock' ||
        player1 == 'scissors' && player2 == 'paper') {

        console.log("winner is " + player1);

    }
}

game("Rock", "Scissors");
game("Paper", "Rock");

We wrote a function called game and used a pre-made function called console.log which the creators of the JavaScript language wrote because they knew that would be a common thing people would need to do and people shouldn't have to write their own code every time they want want to do something as simple as "print something on the screen".

Libraries take this one step further and are groups of related functions. saml2-js is a javascript library for integrating single-sign on to javascript applications. Single-Sign On (SSO) is an often required security practice for enterprise applications and implementing it is complex and requires many functions. When you logon to myRCC and get access to Blackboard, SIS, and other applications with 1 login, that is SSO. The saml2-js library, would allow a developer to integrate their app securely with SSO and is one example of a library that someone has written that includes all the necessary functions to implement SSO in one package.

Scope

Read What is scope? and watch this lecture on functions and scope which has lots of live coding examples and some audience Q&A.

Practice on freecodecamp

This video shows a couple ways to watch videos and code in Glitch at the same time.

New Console

Now that we're coding in JavaScript and not the command line, we're using a different console, the Browser console. This video covers opening the browser console in Chrome. The browser console will show any console.log statements you write in your JavaScript files.

Complete the lessons below in freeCodeCamp JavaScript course.

  1. Write Reusable JavaScript with Functions 💻 code📺 watch
  2. Passing Values to Functions with Arguments 💻 code📺 watch
  3. Global Scope and Functions 💻 code📺 watch
  4. Local Scope and Functions 💻 code📺 watch
  5. Global vs. Local Scope in Functions 💻 code📺 watch
  6. Return a Value from a Function with Return 💻 code📺 watch
  7. Understanding Undefined Value returned from a Function 💻 code📺 watch
  8. Assignment with a Returned Value 💻 code📺 watch
  9. Stand in Line 💻 code📺 watch
  10. Understanding Boolean Values 💻 code📺 watch
  11. Use Conditional Logic with If Statements 💻 code📺 watch
  12. Comparison with the Equality Operator 💻 code📺 watch
  13. Comparison with the Strict Equality Operator 💻 code📺 watch
  14. Practice comparing different values 💻 code📺 watch
  15. Comparison with the Inequality Operator 💻 code📺 watch
  16. Comparison with the Strict Inequality Operator 💻 code📺 watch
  17. Comparison with the Greater Than Operator 💻 code📺 watch
  18. Comparison with the Greater Than Or Equal To Operator 💻 code📺 watch
  19. Comparison with the Less Than Operator 💻 code📺 watch
  20. Comparison with the Less Than Or Equal To Operator 💻 code📺 watch
  21. Comparisons with the Logical And Operator 💻 code📺 watch
  22. Comparisons with the Logical Or Operator 💻 code📺 watch
  23. Introducing Else Statements 💻 code📺 watch
  24. Introducing Else If Statements 💻 code📺 watch
  25. Logical Order in If Else Statements 💻 code📺 watch
  26. Chaining If Else Statements 💻 code📺 watch
  27. Golf Code 💻 code📺 watch
  28. Selecting from Many Options with Switch Statements 💻 code📺 watch
  29. Adding a Default Option in Switch Statements 💻 code📺 watch
  30. Multiple Identical Options in Switch Statements 💻 code📺 watch
  31. Replacing If Else Chains with Switch 💻 code📺 watch
  32. Returning Boolean Values from Functions 💻 code📺 watch
  33. Return Early Pattern for Functions 💻 code📺 watch
  34. Counting Cards 💻 code📺 watch

💬 06 Chat

Due: 04/08/19 9 AM

Instructions

The purpose of this chat is to continue developing your self-reflection skills.

  1. After going through the content above, write out at least two questions you have related to any of the concepts presented.

    Use this opportunity to ask questions about concepts you don't fully understand. If you are confident in your understand of all the concepts, develop questions around how a concept might be applied or why it might be useful to you in your career.

  2. Join the the #sp19-06-chat Slack channel and ask your questions.

Grading Rubric

% Explanation
100% Two questions asked in #sp19-06-chat related to this module's content.
+10% Bonus points for asking more than 2 questions.
+10% Bonus points for helping other students find answers to their questions.

📝 06 Journal

Due: 04/08/19 9 AM

Instructions

  1. Direct Message (DM) Michael Greene (profmikegreene) with two lists.
    1. One for the things you understand most confidently.
    2. One for the things you are struggling with.

The items in these lists can be vocabulary, assignment tasks, concepts, whatever sticks out in your mind. Try to find at least two items per list and feel free to make them as long as you like.

Grading Rubric

% Explanation
50% Direct message to profmikegreene containing a list of things you are confident you understand
50% Direct message to profmikegreene containing a list of things you are struggling with

⚛️ 06 Project

Due: 04/08/19 9 AM

The purpose of this project is to prove your understanding of the concepts in this lesson.

Instructions

Setup

  1. Click Channels in the Slack sidebar to view all channels
  2. Join the #sp19-06-project slack channel
  3. In that Slack channel, you will see a link that will take you to Github Classroom
  4. Click the Accept this assignment button and Github will create a project repo for you.
  5. Once this step is complete, the page should say "Your assignment has been created here: https://github.com/RCC-ITP-175/sp19-06-". Click this link and visit your repository.
  6. Create a new Glitch project using the Clone from Git Repo option and paste in RCC-ITP-175/sp19-06-YOURGITHUBUSERNAME to clone from the repo you just created.

Javascript

  1. Your Glitch project now contains several exercise.js files which contain instructions and some console.log statements to validate that you've completed the instructions.
  2. Write some Javascript to complete the instructions for each of the exercise.js files.

Submission

  1. Open the Glitch Tools menu in the bottom left, and select Git, Import, and Export

  2. Click Export to Github and if asked type RCC-ITP-175/sp19-06-

    For example, I would type RCC-ITP-175/sp19-06-profmikegreene

  3. Click OK and your project should be exported to Github

  4. Visit your repo on github.com and click the branches dropdown and you should see a Glitch branch containing your code

All done!

Grading Rubric

pts Explanation
5 Create a github repo for this project
15 exercise1.js completed
15 exercise2.js completed
15 exercise3.js completed
Last Updated: 4/7/2019, 4:25:22 PM