Bonus Homework Week 2

Introduction

For this week’s bonus material, you’ll be practing the new syntax that you’ve learned this week with some exercises.

As usual with the bonus homeworks, this will be done on your local machine, rather than on a sandbox like freeCodeCamp.

These problems are meant to make you think, so I’m not giving you as many hints as I could do. If you find it hard to figure out exactly what you’re supposed to be doing here, then leave a message on the week-2 channel on Discord, and I can explain in more detail!

Assignment

1) Set up your JavaScript environment | 20 mins

To start off for this week, I want you to create some files on your computer, and open them up with VSCode.

If you didn’t do any of the bonus homework material yet, then check out week 0’s bonus homework, with has some information to help get started with VSCode.

Make a new folder called week2-problems with a blank index.html file as normal, and a file called main.js alongside it:

week2-problems
│   index.html
|   main.js

Once you have this set up, make sure to include your main.js file from inside your HTML file. Your index.html file should end up looking like this:

<!DOCTYPE html>
<html>
<head>
  <title>Week 2 Problems</title>
</head>
<body>
  <script src="main.js"></script>
</body>
</html>

Finally, write a simple console log at the top of your main.js file, and make sure that your message shows up in the developer tools console:

console.log("Hello World")

2) Write a function that converts a temperature in Celsius to Fahrenheit | 25 min

I’d like you to play around with a simple problem, which will help you to get used to the idea of variables and functions in JavaScript. We’ll be making a calculator that converts values from celsius to fahrenheit.

In your main.js file, write a function called celsiusToFahrenheit, which takes a number in Celsius as an argument, and returns a number in Fahrenheit. You might find this equation useful to come up with your solution:

Image of Celsius to Farenheit formula

Make a few console logs invoking (using) this function, to make sure that it’s working correctly. As a sanity check, make sure that:

console.log(celsiusToFahrenheit(0)) // outputs 32
console.log(celsiusToFahrenheit(20)) // outputs 68
console.log(celsiusToFahrenheit(100)) // outputs 212

3) Write a function that collects the user’s age | 20 min

In this exercise, I want you to play around with getting user input, and then storing that in a variable. It turns out that we can use the prompt() statement to get some information from the user (this is explained in this reading at the end of this bonus homework).

In your main.js file, declare another function called getAgeFromUser, which takes no arguments.

This function should use prompt() to ask the user how old they are, and then return that variable at the end.

Ensure that:

console.log(getAgeFromUser()) // makes prompt popup, and outputs whatever we enter

4) Write a function to calculate the user’s age in 2050 | 20 min

In this exercise, I want you to write a function that uses the getAgeFromUser function from the last exercise to calculate the user’s age in 2050.

In your main.js file, declare a function called calculateAgeIn2050, which takes no arguments.

Inside the function, make a call to getAgeFromUser, storing the result in a variable. Create an alert() saying “In 2050, you will be X years old!”, replacing X with the age that the user will be.

(Hint: this will be 29 years older than the user is at the moment!)

Ensure that for a 20-year old user:

console.log(calculateAgeIn2050()) // outputs "In 2050, you will be 49 years old"

5) Write a function to calculate the user’s age in any year | 15 min

As a final step, I want you to make a slight edit to your calculateAgeIn2050 function, so that it gets the user’s age in the year that we pass into it as an argument.

Rename the calculateAgeIn2050 function to something else that expresses its new purpose (getting the user’s age in whichever year is inputted). Make a change to the function so that it now takes a year as an argument, and spits out the correct calculation in the alert at the end.

Ensure that for a 20-year old user:

console.log(calculateAgeInYear(2050)) // outputs "In 2050, you will be 49 years old"
console.log(calculateAgeInYear(2025)) // outputs "In 2025, you will be 24 years old"
console.log(calculateAgeInYear(2100)) // outputs "In 2100, you will be 79 years old"

6) Watch/read the following materials | 1 hour 39 mins

As a final step, here is a list of extra materials which might be interesting.

The first two links here are some videos that add a little color to what we’re learning about. The last 5 articles go over what we learned in lecture this week in a little more detail.