cycle shell
Overview
In this lesson, students will use cycle shell to create simple applications.
Learning Goals
By the end of the lesson, students will be able to:
- read a function
- identify parameters
- explain how to return a value from a function
- concatenate strings
pseudocode planning
Distribute the features checklist and introduce the guess the number game.
- Ask students to describe the steps to playing a number in game if they were playing with another person. The list should be similar to:
- Person 1 thinks of a number. (variables)
- Person 1 tells person 2 the number of guesses they are allowed to take. (variables)
- Person 2 guesses a number.
- Person 1 checks if the guess is correct. (conditionals)
- If the guess is correct person 2 wins (strings)
- Otherwise person 1 give person 2 a hint: higher or lower. (strings)
- If person 2 runs out of guesses without getting the correct answer they lose the game. (conditionals)
- Tell students that the game is going to be built in stages as they learn new concepts.
- To create this game the first step is to learn about functions and cycle-shell.
Functions
Students go to http://cycle.sh which should have the following starting code:
function main (input) {
return input
}
- The first line of cycle-shell sets up the interactive page on the right side of the screen.
- A function is a reusable set of instructions. A function is a block of code and has curly braces
{}to mark the beginning and end of the code block. Code inside of the code block should be indented. The function is written as:function name (parameter1, parameter2) { return value } - Parameters are variables that only exist inside of the function.
- In cycle shell the parameters have the value of whatever is written in the input box.
- Each word that is separated by a space will be the value of the next parameter.
- The
returnstatement ends the function and sends back a value. In cycle shell this value is displayed. - Walk through an example of creating an echo application. After coding push the
Run Codebutton in cycle shell. - Walk through the code as the computer would.
- Type the word
hellointo the input box on the right side of the screen but do not pushenteryet. - Ask students, 'When I push enter, what is the value of
input?'input='hello'
- Ask students, 'What value gets returned from this function?'
hello
- Type the word
Strings
- To return words return
``function main (input) { return `Hello` }function main (input) { return `Hello, Daniel` } - To use a variable in a string, use a dollar sign followed by curly braces such as
${variableName}function main (name) { return `Hello, ${name}` }
Engage
- As a group, create an echo application that returns the user's input.
function main (input) { return input } - Create a pluralizer that adds an 's' to the user's input.
function main (input) { return `${input}s` } - Create a greeting app that returns 'Hello, -------'
function main (input) { return `Hello, ${input}` } - Create a compliment machine app that returns a compliment with the name the user inputs.
function main (input) { return `${input}, you have a wonderful smile.` } - Create a mad lib that takes multiple user inputs and places them in a sentence.
function main (adjective, noun, verb) { return `The ${adjective} ${noun} ${verb} to the pond.` }
Justify code
As a group discuss step by step what happens in one of the example programs. After going through one as a group, students practice explaining one of the other examples to a partner.