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:
- each player makes a fist
- each player shakes their fist three times saying 'Rock, Paper, Scissors'
- each player shakes their fist a fourth time and changes their fist to one of the shapes for rock, paper, or scissors
- 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.
- Write Reusable JavaScript with Functions 💻 code📺 watch
- Passing Values to Functions with Arguments 💻 code📺 watch
- Global Scope and Functions 💻 code📺 watch
- Local Scope and Functions 💻 code📺 watch
- Global vs. Local Scope in Functions 💻 code📺 watch
- Return a Value from a Function with Return 💻 code📺 watch
- Understanding Undefined Value returned from a Function 💻 code📺 watch
- Assignment with a Returned Value 💻 code📺 watch
- Stand in Line 💻 code📺 watch
- Understanding Boolean Values 💻 code📺 watch
- Use Conditional Logic with If Statements 💻 code📺 watch
- Comparison with the Equality Operator 💻 code📺 watch
- Comparison with the Strict Equality Operator 💻 code📺 watch
- Practice comparing different values 💻 code📺 watch
- Comparison with the Inequality Operator 💻 code📺 watch
- Comparison with the Strict Inequality Operator 💻 code📺 watch
- Comparison with the Greater Than Operator 💻 code📺 watch
- Comparison with the Greater Than Or Equal To Operator 💻 code📺 watch
- Comparison with the Less Than Operator 💻 code📺 watch
- Comparison with the Less Than Or Equal To Operator 💻 code📺 watch
- Comparisons with the Logical And Operator 💻 code📺 watch
- Comparisons with the Logical Or Operator 💻 code📺 watch
- Introducing Else Statements 💻 code📺 watch
- Introducing Else If Statements 💻 code📺 watch
- Logical Order in If Else Statements 💻 code📺 watch
- Chaining If Else Statements 💻 code📺 watch
- Golf Code 💻 code📺 watch
- Selecting from Many Options with Switch Statements 💻 code📺 watch
- Adding a Default Option in Switch Statements 💻 code📺 watch
- Multiple Identical Options in Switch Statements 💻 code📺 watch
- Replacing If Else Chains with Switch 💻 code📺 watch
- Returning Boolean Values from Functions 💻 code📺 watch
- Return Early Pattern for Functions 💻 code📺 watch
- 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.
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.
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
Direct Message
(DM) Michael Greene (profmikegreene) with two lists.- One for the things you understand most confidently.
- 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
- Click
Channels
in the Slack sidebar to view all channels - Join the #sp19-06-project slack channel
- In that Slack channel, you will see a link that will take you to Github Classroom
- Click the
Accept this assignment
button and Github will create a project repo for you. - 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.
- Create a new Glitch project using the
Clone from Git Repo
option and paste inRCC-ITP-175/sp19-06-YOURGITHUBUSERNAME
to clone from the repo you just created.
Javascript
- 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. - Write some Javascript to complete the instructions for each of the exercise.js files.
Submission
Open the Glitch Tools menu in the bottom left, and select
Git, Import, and Export
Click
Export to Github
and if asked typeRCC-ITP-175/sp19-06-
For example, I would type
RCC-ITP-175/sp19-06-profmikegreene
Click OK and your project should be exported to Github
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 |