006 Assignments-Js-Fundamentals
006 Assignments-Js-Fundamentals
om
for JavaScript
t.c
Fundamentals sections po
gs
lo
b
e.
fre
re
ha
us
ilie
ta
The Complete
JavaScript Course
@jonasschmedtman
Table of Contents
Instructions ............................................................................................................ 4
om
LECTURE: Basic Operators ..................................................................................................... 5
LECTURE: Strings and Template Literals ............................................................................... 6
t.c
LECTURE: Taking Decisions: if / else Statements ................................................................. 6
LECTURE: Type Conversion and Coercion ............................................................................. 6
po
LECTURE: Equality Operators: == vs. === .............................................................................. 7
gs
LECTURE: Logical Operators .................................................................................................. 7
LECTURE: The switch Statement ........................................................................................... 8
lo
LECTURE: The Conditional (Ternary) Operator ...................................................................... 8
b
e.
om
LECTURE: The while Loop ..................................................................................................... 17
t.c
Solutions – Part 2 ................................................................................................ 18
po
LECTURE: Functions ............................................................................................................. 18
LECTURE: Function Declarations vs. Expressions .............................................................. 18
gs
LECTURE: Arrow Functions................................................................................................... 19
lo
LECTURE: Functions Calling Other Functions ..................................................................... 19
LECTURE: Introduction to Arrays ......................................................................................... 20
b
e.
§ The goal of these assignments is that you can immediately apply the concepts
you learn in each video;
§ So after you complete each lecture, find the assignment for the video you just
watched, and write the code according to the instructions;
om
§ Take all the time that you need, no need to hurry!
§ The solution for each assignment is at the end of Part 1 and Part 2. I advise you
t.c
to check it out after you completed each assignment, or in case you have
trouble moving forward in the code;
po
§ In order to actually write the code, create a new script called assignments.js
gs
in the current project folder and link it to the HTML file we have been using, just
like we previously linked script.js (an HTML file can include multiple
lo
JavaScript scripts). The console will now show outputs from both script.js
b
and assignments.js 😉
e.
§ And now, go have fun with these assignments! By the way, all these
fre
om
LECTURE: Data Types
1. Declare a variable called 'isIsland' and set its value according to your
t.c
country. The variable should hold a Boolean value. Also declare a variable
'language', but don't assign it any value yet
po
2. Log the types of 'isIsland', 'population', 'country' and 'language'
to the console
gs
lo
LECTURE: let, const and var
b
e.
1. Set the value of 'language' to the language spoken where you live (some
countries have multiple languages, but just choose one)
fre
2. Think about which variables should be const variables (which values will never
re
change, and which might change?). Then, change these variables to const.
3. Try to change one of the changed variables now, and observe what happens
ha
us
1. If your country split in half, and each half would contain half the population,
then how many people would live in each half?
ta
2. Increase the population of your country by 1 and log the result to the console
3. Finland has a population of 6 million. Does your country have more people than
Finland?
4. The average population of a country is 33 million people. Does your country
have less people than the average country?
5. Based on the variables you created, create a new variable 'description'
which contains a string with this format: 'Portugal is in Europe, and its 11 million
people speak portuguese'
om
minus the country's population)
2. After checking the result, change the population temporarily to 13 and then to
t.c
130. See the different results, and set the population back to original
po
LECTURE: Type Conversion and Coercion gs
1. Predict the result of these 5 operations without executing them:
lo
'9' - '5';
b
5 + 6 + '4' + 9 - 4 - 2;
2. Execute the operations to check if you were right
re
ha
us
ilie
ta
om
'numNeighbours' is 0 or any other value)
5. Test the code with different values of 'numNeighbours', including 1 and 0.
6. Change == to ===, and test the code again, with the same values of
t.c
'numNeighbours'. Notice what happens when there is exactly 1 border! Why
po
is this happening?
7. Finally, convert 'numNeighbours' to a number, and watch what happens now
when you input 1 gs
8. Reflect on why we should use the === operator and type conversion in this
lo
situation
b
e.
fre
2. Let's say Sarah is looking for a new country to live in. She wants to live in a
ha
country that speaks english, has less than 50 million people and is not an
island.
us
3. Write an if statement to help Sarah figure out if your country is right for her.
You will need to write a condition that accounts for all of Sarah's criteria. Take
ilie
your time with this, and check part of the solution if necessary.
4. If yours is the right country, log a string like this: 'You should live in Portugal :)'. If
ta
om
LECTURE: The Conditional (Ternary) Operator
t.c
1. If your country's population is greater than 33 million, use the ternary operator
to log a string like this to the console: 'Portugal's population is above average'.
po
Otherwise, simply log 'Portugal's population is below average'. Notice how only
one word changes between these two sentences!
gs
2. After checking the result, change the population temporarily to 13 and then to
lo
130. See the different results, and set the population back to original
b
e.
fre
re
ha
us
ilie
ta
om
console.log(continent);
console.log(population);
t.c
po
LECTURE: Data Types gs
lo
let isIsland = false;
b
let language;
e.
console.log(typeof isIsland);
fre
console.log(typeof population);
console.log(typeof country);
re
console.log(typeof language);
ha
us
ilie
language = 'portuguese';
const country = 'Portugal';
const continent = 'Europe';
const isIsland = false;
isIsland = true;
console.log(population / 2);
population++;
console.log(population);
console.log(population > 6);
console.log(population < 33);
const description1 =
country +
om
' is in ' +
continent +
t.c
', and its ' +
population +
po
' million people speak ' +
language;
console.log(description1); gs
b lo
e.
om
t.c
LECTURE: Equality Operators: == vs. ===
po
const numNeighbours = prompt(
);
gs
'How many neighbour countries does your country have?',
b lo
// LATER : This helps us prevent bugs
e.
have?'),
);
re
ha
if (numNeighbours === 1) {
console.log('Only 1 border!');
us
} else {
console.log('No borders');
ta
om
t.c
LECTURE: The switch Statement
po
switch (language) {
case 'chinese':
gs
lo
case 'mandarin':
b
break;
case 'spanish':
fre
case 'english':
ha
console.log('3rd place');
break;
us
case 'hindi':
console.log('Number 4');
ilie
break;
case 'arabic':
ta
console.log(
`${country}'s population is ${population > 33 ? 'above' :
'below'} average`,
);
om
t.c
po
gs
b lo
e.
fre
re
ha
us
ilie
ta
Note: Please start Part 2 from scratch and comment out all the code from Part 1.
LECTURE: Functions
1. Write a function called 'describeCountry' which takes three parameters:
'country', 'population' and 'capitalCity'. Based on this input, the
om
function returns a string with this format: 'Finland has 6 million people and its
capital city is Helsinki'
2. Call this function 3 times, with input data for 3 different countries. Store the
t.c
returned values in 3 different variables, and log them to the console
po
LECTURE: Function Declarations vs. Expressions gs
lo
1. The world population is 7900 million people. Create a function declaration
called 'percentageOfWorld1' which receives a 'population' value, and
b
returns the percentage of the world population that the given population
e.
represents. For example, China has 1441 million people, so it's about 18.2% of
fre
4. Create a function expression which does the exact same thing, called
'percentageOfWorld2', and also call it with 3 country populations (can be
ilie
om
LECTURE: Introduction to Arrays
t.c
1. Create an array containing 4 population values of 4 countries of your choice.
You may use the values you have been using previously. Store this array into a
po
variable called 'populations'
gs
2. Log to the console whether the array has 4 elements or not (true or false)
3. Create an array called 'percentages' containing the percentages of the
lo
world population for these 4 population values. Use the function
b
percentage values
fre
choice. Choose a country which has at least 2 or 3 neighbours. Store the array
us
om
and a capital called Helsinki.'
2. Increase the country's population by two million using dot notation, and then
t.c
decrease it by two million using brackets notation.
po
LECTURE: Object Methods gs
1. Add a method called 'describe' to the 'myCountry' object. This method
lo
will log a string to the console, similar to the string logged in the previous
b
1. There are elections in your country! In a small town, there are only 50 voters.
Use a for loop to simulate the 50 people voting, by logging a string like this to
ta
om
LECTURE: Looping Backwards and Loops in Loops
t.c
1. Store this array of arrays into a variable called 'listOfNeighbours'
[['Canada', 'Mexico'], ['Spain'], ['Norway', 'Sweden',
po
'Russia']];
2. Log only the neighbouring countries to the console, one by one, not the entire
gs
arrays. Log a string like 'Neighbour: Canada' for each country
lo
3. You will need a loop inside a loop for this. This is actually a bit tricky, so don't
worry if it's too difficult for you! But you can still try to figure this out anyway 😉
b
e.
fre
1. Recreate the challenge from the lecture 'Looping Arrays, Breaking and Continuing',
but this time using a while loop (call the array 'percentages3')
ha
2. Reflect on what solution you like better for this task: the for loop or the while
loop?
us
ilie
ta
LECTURE: Functions
om
const descPortugal = describeCountry('Portugal', 10,
'Lisbon');
t.c
const descGermany = describeCountry('Germany', 83,
po
'Berlin');
const descFinland = describeCountry('Finland', 6,
'Helsinki'); gs
console.log(descPortugal, descGermany, descFinland);
b lo
e.
fre
function percentageOfWorld1(population) {
ha
};
om
t.c
LECTURE: Functions Calling Other Functions
po
gs
const describePopulation = function (country, population) {
lo
const percentage = percentageOfWorld1(population);
const description = `${country} has ${population} million
b
console.log(description);
fre
};
re
describePopulation('Portugal', 10);
ha
describePopulation('China', 1441);
describePopulation('USA', 332);
us
ilie
ta
om
];
console.log(percentages);
t.c
po
LECTURE: Basic Array Operations (Methods) gs
lo
const neighbours = ['Norway', 'Sweden', 'Russia'];
b
e.
neighbours.push('Utopia');
fre
console.log(neighbours);
re
neighbours.pop();
ha
console.log(neighbours);
us
if (!neighbours.includes('Germany')) {
console.log('Probably not a central European country :D');
ilie
}
ta
neighbours[neighbours.indexOf('Sweden')] = 'Republic of
Sweden';
console.log(neighbours);
const myCountry = {
country: 'Finland',
capital: 'Helsinki',
language: 'finnish',
population: 6,
neighbours: ['Norway', 'Sweden', 'Russia']
};
om
t.c
LECTURE: Dot vs. Bracket Notation
po
console.log( gs
lo
`${myCountry.country} has ${myCountry.population} million
${myCountry.language}-speaking people,
b
);
re
myCountry.population += 2;
ha
console.log(myCountry.population);
us
myCountry['population'] -= 2;
console.log(myCountry.population);
ilie
ta
const myCountry = {
country: 'Finland',
capital: 'Helsinki',
language: 'finnish',
population: 6,
neighbours: ['Norway', 'Sweden', 'Russia'],
om
describe: function () {
console.log(
t.c
`${this.country} has ${this.population} million
${this.language}-speaking people,
po
${this.neighbours.length} neighbouring countries and a
capital called ${this.capital}.`
},
); gs
b lo
checkIsland: function () {
e.
false;
re
}
us
};
myCountry2.describe();
ilie
myCountry2.checkIsland();
console.log(myCountry2);
ta
om
t.c
LECTURE: Looping Backwards and Loops in Loops
po
const listOfNeighbours = [
['Canada', 'Mexico'],
gs
lo
['Spain'],
['Norway', 'Sweden', 'Russia'],
b
];
e.
fre
console.log(`Neighbour: ${listOfNeighbours[i][y]}`);
ha
us
let i = 0;
while (i < populations.length) {
const perc = percentageOfWorld1(populations[i]);
percentages3.push(perc);
i++;
}
console.log(percentages3);