Fundamentals Lite
  • 🚀Course Overview
  • Course Logistics
    • 🏫Course Methodology
      • 🧩Course Components
      • 💬Course Communication
    • 💻Required Hardware and Software
      • ☝️Required Software 1
      • ✌️Required Software 2
      • 👍Recommended Setup
    • 📅Schedule
    • 💡Tips and Tricks
      • 📒Coding Strategies
      • 🛠️Tooling Pro Tips
    • 🎓Post-Course
      • 🚀Upgrading to Paid Fundamentals
      • 🚂Bootcamp Admission Criteria
      • 📹Bootcamp Video Application
  • 1: Introduction
    • 1.1: What is Coding?
    • 1.2: Web Browsers
    • 1.3: Command Line
    • Additional Resources 1
  • 2: Basic Data Manipulation
    • 2.1: Operations
    • 2.2: Variables
    • 2.3: Our First Program
    • Additional Resources 2
  • 3: Structuring and Debugging Code
    • 3.1: Functions
    • 3.2: Errors
    • Additional Resources 3
  • 4: Conditional Logic
    • 4.1: Intro to Logic
    • 4.2: Pseudo-Code, Boolean OR
    • 4.3: Boolean AND, NOT
    • 4.4: Input Validation
    • Additional Resources 4
  • 5: Managing State and Input Validation
    • 5.1: Program Lifecycle and State
    • 5.2: Program State for Game Modes
    • Additional Resources 5
  • 6: Arrays and Iteration
    • 6.1: Arrays
    • 6.2: Loops
    • 6.3: Loops with Arrays
    • Additional Resources 6
  • 7: Version Control
    • 7.1: Git
    • Additional Resources 7
  • 8: GitHub
    • 8.1: Intro to GitHub
    • 8.2: GitHub Fork and Clone
    • 8.3: GitHub Pull Request
    • 8.4: GitHub Repo Browsing
    • 8.5: Deployment
    • Additional Resources 8
  • Homeworks
    • Day 2: Basic File and Data Manipulation
    • Day 3: Functions
    • Day 4: If Statements, Boolean Or, Boolean And
    • Day 5: Program State
    • Day 7: Loops
    • Day 8: Arrays and Loops
  • Projects
    • Project 1: Scissors Paper Stone
      • Project 1: Scissors Paper Stone (Part 1)
      • Project 1: Scissors Paper Stone (Part 2)
    • Project 2: Beat That!
Powered by GitBook
On this page
  • Introduction
  • Knowledge Check
  • Learning Outcomes
  • Base
  • Cost of Air Con
  • Screen Time
  • Ice Machine
  • Beer Order
  • More Comfortable
  • Cost of Cellular Data
  • Mortgage Calculator
  • Reference Solution
  1. Homeworks

Day 3: Functions

PreviousDay 2: Basic File and Data ManipulationNextDay 4: If Statements, Boolean Or, Boolean And

Last updated 2 years ago

Introduction

Today we will write functions to perform data transformations, similar to examples in . For each exercise, make a copy of the as per the . For some exercises you may need to google for information.

Knowledge Check

Before you begin, check that you can answer the following:

Learning Outcomes

  • Practice writing helper functions (and not just solve for the correct calculation)

  • Executing multiple helper functions as part of the overall program.

  • Practice creating contextual, meaningful variables.

Base

If you have attempted these problems as part of More Comfortable in Day 1, today re-write the code such that all of the logic is in helper functions and not solely in main.

Cost of Air Con

The user enters the number of hours of air-con use, and the app tells them the cost.

An aircon machine uses 2 kilowatts of electricity.

Electricity costs $0.20 per kilowatt-hour.

Screen Time

The user will enter the number of hours spent per day on their favourite app, and the program renders how many days you will spend in your lifetime on this app.

Assume an average life expectancy of 82 years.

For example, if I spend 2 hours per day on WhatsApp and I live an average lifetime, how many total days will I spend on WhatsApp in my lifetime?

Ice Machine

A hotel uses an ice machine to prepare ice for guests. They want to start the ice machine as close to each event as possible, so that the ice doesn't melt. In order to do this, they need to estimate how long they will need to run the ice machine.

Create a program that estimates the duration the ice machine needs to run. The user will input the number of guests for the event.

Assume each guest needs 2 drinks. Each drink has 4 ice cubes. Each cube weights 1.5 grams. The hotel's American-made ice machine produces 5 pounds of ice per hour.

Beer Order

Create a program for a bar to calculate how many kegs of beer they will need to order for each quarter-year (output) based on their estimated number of daily customers (input).

Assume that the average customer drinks 2 pints per visit; and each keg of beer pours exactly 124 pints.

More Comfortable

Cost of Cellular Data

Create a program to calculate how much a user will pay for their the $19.99 50GB post-paid data plan. The user will enter how many GB they use per month, and the app will tell them how much they are paying per GB of data used.

Assume that if the user exceeds 50GB, they will automatically purchase an additional 50GB plan. You may find the built-in function Math.ceil helpful for this (you can google how to use it).

For example, if the user only used 1GB this month, the app would calculate $19.99 per GB as the user paid $19.99 for the 50GB plan but only used 1GB. If the user used 2GB this month, the app would calculate $9.98 per GB. If the user used 51GB this month the user would have automatically been billed for 2 plans and the app would calculate $0.78 per GB.

Mortgage Calculator

Create a mortgage calculator for a bank. This bank is just starting so their loan terms are simple and the same for everyone. The user will enter the loan amount. The interest is 3% APR. The loan duration is 10 years.

Output several values including:

  1. How much the customer will pay back in total, including the principal.

  2. How much the customer will pay just in interest.

  3. How much the customer's monthly payment will be.

Concatenating Strings and Numbers

Sometimes when trying to add 2 numbers you may find that your numbers concatenate instead of add. For example, when adding '1' + 1 in your code, the result may be '11' instead of 2. This is because one of your numbers may be represented in JS as a string, most commonly when the number comes from the input variable in our programs.

The following is a more detailed breakdown of JS behaviour when concatenating strings and numbers.

  1. String + String

    1. 'hello' + 'hi' will return 'hellohi'

    2. '2' + '2' will return '22'

  2. Number + Number

    1. 2 + 2 will return 4

  3. Number + String

    1. 2 + '2' will return '22'

Reference Solution

See an example of all problems .

On , , and later versions, we can see how many hours per day we spend on each app. Find the number of hours you spend per day on the app you use most. If you're not able to find this, you can google for common statistics.

To resolve similar issues, try converting input to a number with before using it, e.g. const loanAmt = Number(input).

is a reference solution. Please only view the reference solution for each exercise after you have attempted the exercise yourself. Note that there are many ways to implement these solutions and the reference solution is only 1 way.

3.1: Functions
starter code
recommended folder structure for Fundamentals
here
Android 10
iOS 12
JS' built-in Number function
Here