Chapter 3.1 - Homework
The best and only way to really understand any programming concept is to try it in action. That is why we designed a few programming exercises for you to complete using the concepts you've learned in this chapter—variables, conditional statements, arrays, and loops. We will ask you to explain some of these in class, as well as to show your code and provide feedback!
Exercises with variables
As you know, different variables can hold different data types. In some other languages you need to explicitly state what type should a variable hold: integer, character, string, boolean, etc. In JavaScript, you don't need to specify that, and instead you just use var, let, or const, depending on your use case. This approach has pros and cons: you don't have to worry about defining data types, but it often leads to unexpected bugs in your code (for example, 3 + '3' == '33' but 3 - '3' == 0). To fix this, Microsoft actually designed a typed version of JavaScript called TypeScript, but we won't get into that. For now, experiment with different types in plain JavaScript. Open the console in your browser (Right Click, Inspect, Console) and do the following:
- Create a variable called
myNameand assign your own name to it. Remember that strings can be defined as follows:let string1 = "Hello, string1" let string2 = 'Hello, string2' let string3 = `Hello, string3` - Create a variable called
myAgeand assign your age to it. Numbers don't have to have quotation marks around them. - Create a variable called
myAgeInTenYearsand assign your age in ten years to it. However, you have to usemyAgeto setmyAgeInTenYears. Remember that you can assign arithmetic operations to variables! - Similarly, create a variable called
halfOfMyAgeand assign half of your age to it usingmyAge. - Finally, create another variable called
myMessageas follows:
Now, print it out, usinglet myMessage = `I'm ${myName} and I'll be ${myAge + 10} years old soon.`console.log(myMessage). As you can see, using back quotes allows you to embed JavaScript code inside your strings with${code}syntax!
Exercises with conditional statements
Earlier, we've learned about if-else statements. However, there is another way to improve them, using else if statements. It allows to merge more sophisticated conditions. For example,
if (condition1) {
console.log("condition1 is true")
} else if (condition2) {
console.log("condition2 is true")
} else {
console.log("condition1 and condition2 are both false")
}
Note that you don't have to have the last else statements. To practice this structure, do the following simple challenge:
- It creates a variable called
pockedMoneywith some sum of money. - If that sum of money exceeds $1000, it prints
I can afford the new iPhone!. If it is less than $1000 but more than $900, it printsI almost have enough money!. Otherwise, it printsI can't afford the new iPhone..
Use if-else statements and logic operators!
Exercises with loops
Loops allow to repeat the same code multiple times with different values, without having to repeat the code itself. Sometimes, it just helps simplify the code, but often it is impossible to avoid, as you may need to execute the same code millions of times or you may not know variable values in advance. For example, the following code will print five consecutive integers starting from one:
console.log(1)
console.log(2)
console.log(3)
console.log(4)
console.log(5)
However, what if you wanted to print 1000 consecutive integers? Writing 1000 lines of code will take a long time... Similarly, what if you want to print n consecutive numbers but you don't know what n is? This is where loops come into play.
Loops Challenge 1
Using the while loop, print powers of the first 16 integers. Your code should produce the following:
1
4
9
16
25
36
49
64
81
100
121
144
169
196
225
256
However, you may only have one line that contains console.log(). As a hint, here is how a while loop would like:
let i = 0;
while (condition) {
// code
i++;
}
In JavaScript, everything after // indicates a "comment" and doesn't affect anything.
In order to input multiple lines in your console, hold
Shiftbefore creating a new line.
Loops Challenge 2
Using the for loop, print the sum of the first 100 integers. As a hint, you may use the following template code:
// code
for (let i = 0; i < 100; i++) {
// code
}
// print
You will probably need other variable(s) besides i! Once you complete this, rewrite this code using the while loop. After all, they are identical! (At least using the features that we have learned so far.)
Exercises with arrays
Arrays in JavaScript can hold multiple variable types at the same time. For example, this is a valid array in JavaScript:
let arr = ['Hello', 2021]
Remember also that you can access elements by their index, starting from. For example,
console.log(arr[0])
will print Hello.
For the final part of your homework, create an array that holds any 5 elements, and print it out using a loop!
Note about homework
Please save the code that you are going to write somewhere convenient so that you can show it in class!