- Practice the concepts
- JavaScript exercises
- Code along
- PROJECT: Grade calculator
- [OPTIONAL] Extra resources
In this section you will be doing interactive exercises that will allow you to practice with the concepts you've learned about this week.
In the first course you'll learn about functions, the structure and how they're used. They are a fundamental part of understanding programming and you should become very familiar with them!
In this second course you'll learn about higher order functions, which we'll refer to again during the JavaScript2 module. Go through it to get familiar, but don't feel like you completely have to understand it at this point.
Inside of your
JavaScript1fork and inside of theWeek3folder, create a folder calledhomework. Inside of that folder, create a folder called js-exercises. For all the following exercises create a new .js file in that folder (5 files in total). Make sure the name of each file reflects its content: for example, the filename for exercise one could be giveCompliment.js.
In each file, start off with the string 'use strict'. This will make sure the code interpreter will enforce stronger rules when looking at your code.
Exercise 1: You are amazing, Noer!
There is no better way to start your day then with a compliment!
- Write a function named
giveCompliment
- It takes 1 argument: your name
- Inside the function create an array with 10 strings. Each string should be a compliment, like
"great","awesome" - Write logic that randomly selects a compliment
- Return a string: "You are [COMPLIMENT], [YOUR_NAME]!
- Call the function three times, giving each function call the same argument: your name.
Exercise 2: Dog years
You know how old your dog is in human years, but what about dog years? Calculate it!
- Write a function named
calculateDogAge.
- It takes 1 argument: your puppy's age (number).
- Calculate your dog's age based on the conversion rate of 1 human year to 7 dog years.
- Return a string: "Your doggie is [CALCULATED_VALUE] years old in dog years!"
- Call the function three times with different sets of values.
Exercise 3: Be your own fortune teller
Why pay a fortune teller when you can just program your fortune yourself?
- Write a function named
tellFortune.
- It takes 4 arguments: number of children (number), partner's name (string), geographic location (string), job title (string).
- Randomly select values from the arrays.
- Return a string: "You will be a [JOB_TITLE] in [LOCATION], and married to [PARTNER_NAME] with [NUMBER_KIDS] kids."
- Create 4 arrays,
numChildren,partnerNames,locationsandjobs. Give each array 5 random values that make sense - Call the function 1 time, by passing the arrays as the argument.
Exercise 4: Shopping at the supermarket
Let's do some grocery shopping! We're going to get some things to cook dinner with. However, you like to spend your money and always buy too many things. So when you have more than 3 items in your shopping cart the first item gets taken out.
- Write a function named
addToShoppingCart.
- It takes in 1 argument: a grocery item (string)
- Add grocery item to array. If the amount of items is more than 3 remove the first one in the array
- Return a string: "You bought [LIST_OF_GROCERY_ITEMS]!"
- Create an array with 2 predefined strings:
"bananas"and"milk" - Call the function 3 times, each time with a different string as the argument.
Exercise 5: Total cost is ...
You want to buy a couple of things from the supermarket to prepare for a party. After scanning all the items the cashier gives you the total price, but the machine a broken! Let's write her a function that does it for her instead!
- Write a function called
calculateTotalPrice
- It takes 1 argument: an object that contains properties that only contain number values
- Add all the number values together
- Return a number: the total price of all items
- Create an object named
cartForPartywith 5 properties. Each property should be a grocery item (likebeersorchips) and hold a number value (like1.75or0.99) - Call the function 1 time, giving it the object
cartForPartyas an argument
In this project you'll be building a simple meditation application, that will allow you to set a timer, loop a video and play a song!
You'll be working with the and
Every week ends with a project you have to build on your own. Instead of getting clear-cut instructions, you'll get a list of criteria that your project needs to measure up to.
In this project you'll write a script that validates whether or not a credit card number is valid.
Here are the rules for a valid number:
- Number must be 16 digits, all of them must be numbers
- You must have at least two different digits represented (all of the digits cannot be the same)
- The final digit must be even
- The sum of all the digits must be greater than 16
- The following credit card numbers are valid:
9999777788880000
6666666666661666The following credit card numbers are invalid:
a92332119c011112 (invalid characters)
4444444444444444 (only one type of number)
1111111111111110 (sum less than 16)
6666666666666661 (odd final number)These are the requirements your project needs to fulfill:
- Make a JavaScript file with a name that describes its contents
- Create a function with a descriptive name, for example:
doSomethingorcalcAnotherThing - Write at least 2 comments that explain to others what a line of code is meant to do
- Make the return value of the function a template string, so you can insert variables!
- Use
nodefrom the command line to test if your code works as expected
Good luck!
If you have time left over and feel like you could go for more practice, try out the following course:
In this free course, you'll build 30 small projects that will sharpen your HTML, CSS and JavaScript skills. Each project is meant to show you different ways you could use programming to make fun and useful things! Enjoy!
After you've finished your todo list it's time to show us what you got! The homework that needs to be submitted is the following:
- JavaScript exercises
- PROJECT: Credit card validator
Upload both to your forked JavaScript1 repository in GitHub. Make a pull request to the original repository.
Forgotten how to upload your homework? Go through the guide to learn how to do this again.