Practice Assignments For Javascript Fundamentals Sections: The Complete Javascript Course
Practice Assignments For Javascript Fundamentals Sections: The Complete Javascript Course
for JavaScript
Fundamentals sections
The Complete
JavaScript Course
@jonasschmedtman
Table of Contents
Instructions ............................................................................................................ 4
§ So after you complete each lecture, find the assignment for the video you just
watched, and write the code according to the instructions;
§ The solution for each assignment is at the end of Part 1 and Part 2. I advise you
to check it out after you completed each assignment, or in case you have
trouble moving forward in the code;
§ In order to actually write the code, create a new script called assignments.js
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
JavaScript scripts). The console will now show outputs from both script.js
and assignments.js 😉
§ And now, go have fun with these assignments! By the way, all these
assignments are about countries 🇵🇹 🇩🇪 🇮🇳
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 +
' is in ' +
continent +
', and its ' +
population +
' million people speak ' +
language;
console.log(description1);
if (numNeighbours === 1) {
console.log('Only 1 border!');
} else if (numNeighbours > 1) {
console.log('More than 1 border');
} else {
console.log('No borders');
}
switch (language) {
case 'chinese':
case 'mandarin':
console.log('MOST number of native speakers!');
break;
case 'spanish':
console.log('2nd place in number of native speakers');
break;
case 'english':
console.log('3rd place');
break;
case 'hindi':
console.log('Number 4');
break;
case 'arabic':
console.log('5th most spoken language');
break;
default:
console.log('Great language too :D');
}
console.log(
`${country}'s population is ${population > 33 ? 'above' :
'below'} average`,
);
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
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
returned values in 3 different variables, and log them to the console
LECTURE: Functions
function percentageOfWorld1(population) {
return (population / 7900) * 100;
}
describePopulation('Portugal', 10);
describePopulation('China', 1441);
describePopulation('USA', 332);
neighbours.push('Utopia');
console.log(neighbours);
neighbours.pop();
console.log(neighbours);
if (!neighbours.includes('Germany')) {
console.log('Probably not a central European country :D');
}
neighbours[neighbours.indexOf('Sweden')] = 'Republic of
Sweden';
console.log(neighbours);
const myCountry = {
country: 'Finland',
capital: 'Helsinki',
language: 'finnish',
population: 6,
neighbours: ['Norway', 'Sweden', 'Russia']
};
console.log(
`${myCountry.country} has ${myCountry.population} million
${myCountry.language}-speaking people,
${myCountry.neighbours.length} neighbouring countries and
a capital called ${myCountry.capital}.`
);
myCountry.population += 2;
console.log(myCountry.population);
myCountry['population'] -= 2;
console.log(myCountry.population);
const myCountry = {
country: 'Finland',
capital: 'Helsinki',
language: 'finnish',
population: 6,
neighbours: ['Norway', 'Sweden', 'Russia'],
describe: function () {
console.log(
`${this.country} has ${this.population} million
${this.language}-speaking people,
${this.neighbours.length} neighbouring countries and a
capital called ${this.capital}.`
);
},
checkIsland: function () {
this.isIsland = this.neighbours.length === 0 ? 'true' :
'false';
const listOfNeighbours = [
['Canada', 'Mexico'],
['Spain'],
['Norway', 'Sweden', 'Russia'],
];