For the complete documentation index, see llms.txt. This page is also available as Markdown.

Session Plan

  • Conditions

    • General description - A function is a reusable piece of code.

    • Function usage

      • Arguments

    • Function creation

      • Parameters - Acts as placeholders

      • Parameters vs arguments

      • Return value - Variable can capture return value.

      • Calling a function on something Math.random - calling the function random on Math

    • Code inspiration

    • Code inspiration 2

    • Exercise

  • Global vs local scope

    • Lexical scope - inner functions contain the scope of parent functions even if the parent function has returned.

    • Code inspiration

    • Exercise

  • For loop

The trainees really struggle with the return value. What it means, how it is captured. What happens when nothing is returned etc. Try really hammering in this concept with lots of simple examples and exercises! e.g. if a function is called get something. That means that something is returned from that function.

Zoey Zou made a nice Notion session plan here: https://www.notion.so/JS1-Week2-8f2d4b7e7ba0425ea4a9e97816e9ceb7

Flipped classroom videos

Flipped classroom videos

Code inspiration

if statements

Functions

if and function

Scope

for loop

Exercises

if statements exercise

Use if statements to give a user a message based on their bank account balance. Use the balance variable and change that.

  • If a user has 0 or less balance log out 'Please deposit some money!'

  • If a user has more than 0 and at most 1000 log out 'Your balance is looking okay'

  • If a user has more than 1000 and at most 3000 log out 'Your balance is looking good'

  • If a user has more than 3000 and at most 10000 log out 'Your balance is fantastic'

  • If a user has more than 10000 log out 'Your balance is AMAZING!'

Function

Create a function called getCircleArea. It should have the radius of the circle as parameter and return the circle area. What happens if we don't return anything in the function?

Create a function called celsiusToFahrenheit it should have a parameter called celsius. It should return the temperature in fahrenheit.

Try call the function and check with google if the function returns the right value.

Exercise: Scope

With pen and paper write what is logged out.

for loop exercises

Simple for loop

Create a for loop that logs out the numbers from 74 - 98

for loop in a function

Create a function that has two parameters: stringToLog and numberOfTimesToLog

When calling the function it should log out the stringToLog the amount of times specified in numberOfTimesToLog. Use a for loop.

Send emails

Imagine we work at a company. Anna from the HR department wants us to send out a couple of emails to some recipients. The only problem is that she sent us the email in a weird format: benjamin@gmail.com|peter@gmail.com|hans@gmail.com|ahmad@gmail.com|sana@gmail.com|virgeen@gmail.com|mohammed@gmail.com

Use the sendEmailTo function to send an email to all the recipients that we got from Anna.

Hint use the .split method and look up iterating an array js for loop on google.

Last updated