JAVASCRIPT FUNDAMENTALS Flashcards

1
Q
  1. Declare variables called ‘country’, ‘continent’ and ‘population’ and
    assign their values according to your own country (population in millions)
  2. Log their values to the console
A

let Continent = “North_America”;
let Country = “United_States_of_America”; //String
let Population = 332403650; //Number
console.log(
My Continent is ${Continent}.\nMy Country is ${Country}.\nThe Population of ${Country} is about ${Population} in 2022.
);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. Declare a variable called ‘isIsland’ and set its value according to your
    country. The variable should hold a Boolean value. Also declare a variable
    ‘language’, but don’t assign it any value yet
  2. Log the types of ‘isIsland’, ‘population’, ‘country’ and ‘language’
    to the console
A
let IsIsland = false; //Boolean
let Language; //Undefined
console.log(typeof IsIsland);
console.log(typeof Population);
console.log(typeof Country);
console.log(typeof Language);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. Set the value of ‘language’ to the language spoken where you live (some
    countries have multiple languages, but just choose one)
  2. Think about which variables should be const variables (which values will never
    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
A
let Language_2 = "Portuguese"; //Nonconst can change anytime unlike Const
console.log(`${Language_2}`);
const country = "Portugal";
console.log(`${country}`);
const continent = "Europe";
console.log(`${continent}`);
// const isIsland = false; //Const will always stay and never change like the word CONSTANT
// isIsland = true;
// console.log(`${isIsland}`);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. If your country split in half, and each half would contain half the population,
    then how many people would live in each half?
  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
A

console.log(Population / 2); //Divides Population (332403650) by 2
Population++; //Adds to Population (332403650) by + 1
console.log(Population);
console.log(Population > 6000000); //Is Population (332403650) greater than 6 Million
console.log(Population < 33000000); //Is Population (332403650) lesser than 33 Million
let Continent_2 = “Europe”;
let Country_2 = “Portugal”;
let Population_2 = 11000000; //Language_2 is already input earlier
console.log(
${Country_2} is in ${Continent_2}, and it's ${Population_2} people speak ${Language_2}
);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. Recreate the ‘description’ variable from the last assignment, this time
    using the template literal syntax
A

console.log(${Country_2} is in ${Continent_2}, and it's ${Population_2} people speak ${Language_2});

//Accidently did the Template Literal Syntax cause it was more efficient and fun this way.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. If your country’s population is greater that 33 million, log a string like this to the
    console: ‘Portugal’s population is above average’. Otherwise, log a string like
    ‘Portugal’s population is 22 million below average’ (the 22 is the average of 33
    minus the country’s population)
  2. After checking the result, change the population temporarily to 13 and then to
  3. See the different results, and set the population back to original
A
let Population_Alter = 5000000000; //Change Population here to alter results
if (Population_Alter > 33000000) {
  console.log(
    `${Country}'s Population is ${
      Population_Alter - 33000000
    } Persons Above Average`
  );
} else {
  console.log(
    `${Country}'s Population is ${
      33000000 - Population_Alter
    } Persons Below Average`
  );
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
1. Predict the result of these 5 operations without executing them:
'9' - '5';
'19' - '13' + '17';
'19' - '13' + 17;
'123' < 57;
5 + 6 + '4' + 9 - 4 - 2;
  1. Execute the operations to check if you were right
A

console. log(“9” - “5”); //4 (‘9’ - ‘5’ = 4)
console. log(“19” - “13” + “17”); //617 (19-13 = 6, [6 + ‘17’ = ‘617’])
console. log(“19” - “13” + 17); //23 (‘19’ - ‘13’ = 6, [6+ 17 = 23])
console. log(“123” < 57); //false (‘123’ is still a number and is greater than 57)
console. log(5 + 6 + “4” + 9 - 4 - 2); //1143 (5 + 6 = 11, + ‘4’, 9 - 4 - 2 = 3, [11 + ‘4’ + 3 = 1143])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. Declare a variable ‘numNeighbours’ based on a prompt input like this:
    prompt(‘How many neighbour countries does your country
    have?’);
  2. If there is only 1 neighbour, log to the console ‘Only 1 border!’ (use loose equality
    == for now)
  3. Use an else-if block to log ‘More than 1 border’ in case ‘numNeighbours’
    is greater than 1
  4. Use an else block to log ‘No borders’ (this block will be executed when
    ‘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
    ‘numNeighbours’. Notice what happens when there is exactly 1 border! Why
    is this happening?
  7. Finally, convert ‘numNeighbours’ to a number, and watch what happens now
    when you input 1
  8. Reflect on why we should use the === operator and type conversion in this
    situation
A

const numNeighbours = Number(prompt(‘How many neighbour countries does your own country have?’));

if (numNeighbours === 1) { //If you input only 1 amount
console.log(‘Only 1 border’);
} else if (numNeighbours > 1) { //If you input < 1 amount
console.log(‘More than 1 border’);
} else { //If you input > 1 amount
console.log(‘There are no borders’);
};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. Comment out the previous code so the prompt doesn’t get in the way
  2. Let’s say Sarah is looking for a new country to live in. She wants to live in a
    country that speaks english, has less than 50 million people and is not an
    island.
  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
    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
    not, log ‘Portugal does not meet your criteria :(‘
  5. Probably your country does not meet all the criteria. So go back and temporarily
    change some variables in order to make the condition true (unless you live in
    Canada :D)
A
let language = "portuguese"; //Change Language and Population to alter results
let population = 40000000;
if (language === ('english', 'portuguese') && population < 50000000 && !IsIsland) 
//!IsIsland basically mean "It's not an Island"
{
  console.log(`You should live in ${country}`);
} else {
  console.log(`${country} does not meet your criteria`);
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. Use a switch statement to log the following string for the given ‘language’:
    chinese or mandarin: ‘MOST number of native speakers!’
    spanish: ‘2nd place in number of native speakers’
    english: ‘3rd place’
    hindi: ‘Number 4’
    arabic: ‘5th most spoken language’
    for all other simply log ‘Great language too :D
A

let language2 = “mandarin”; //Change the language here for different results

switch (language2) {
case “chinese”:
case “mandarin”:
console.log(“Most number of native speakers.”);
break;
case “spanish”:
console.log(“2nd to amount of native speakers.”);
break;
case “english”:
console.log(“3rd to amount of native speakers.”);
break;
case “hindi”:
console.log(“4th to amount of native speakers.”);
break;
case “arabic”:
console.log(“5th to amount of native speakers.”);
break;
default:
console.log(“Every other language is still good, but not as popular.”);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  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’.
    Otherwise, simply log ‘Portugal’s population is below average’. Notice how only
    one word changes between these two sentences!
  2. After checking the result, change the population temporarily to 13 and then to
  3. See the different results, and set the population back to original
A

let Population_3 = 83240000;

console.log(
  `${Country}'s population is ${
    Population_3 > 33000000 ? "above" : "below"
  } average`
);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
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'
  1. 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
A
function describeCountry(country, population, capitalcity) {
  return `${country} has ${population} million people and its capital city is ${capitalcity}\n`;
}
const descPortugal = describeCountry("Portugal", 10, "Lisbon");
const descGermany = describeCountry("Germany", 83, "Berlin");
const descFinland = describeCountry("Finland", 6, "Helsinki");
console.log(descPortugal, descGermany, descFinland);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
  1. The world population is 7900 million people. Create a function declaration
    called ‘percentageOfWorld1’ which receives a ‘population’ value, and
    returns the percentage of the world population that the given population
    represents. For example, China has 1441 million people, so it’s about 18.2% of
    the world population
  2. To calculate the percentage, divide the given ‘population’ value by 7900
    and then multiply by 100
  3. Call ‘percentageOfWorld1’ for 3 populations of countries of your choice,
    store the results into variables, and log them to the console
  4. Create a function expression which does the exact same thing, called
    ‘percentageOfWorld2’, and also call it with 3 country populations (can be
    the same populations)
  5. Recreate the last assignment, but this time create an arrow function called
    ‘percentageOfWorld3’
A
function PercentageOfWorld1(population) { //Expression
  return (population / 7900) * 100;
}
const PercentageOfWorld2 = function (population) { //Function Declaration
  return (population / 7900) * 100;
};
const percPortugal1 = PercentageOfWorld1(10);
const percChina1 = PercentageOfWorld1(1441);
const percUSA1 = PercentageOfWorld1(332);
console.log(percPortugal1, percChina1, percUSA1);
/*
const PercentageOfWorld5 = population => (population / 7900) * 100;
const percPortugal3 = PercentageOfWorld5(10); //Recreation of Example #13 Above.
const percChina3 = PercentageOfWorld5(1441);
const percUSA3 = PercentageOfWorld5(332);
console.log(percPortugal3, percChina3, percUSA3);
*/
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
  1. Create a function called ‘describePopulation’. Use the function type you
    like the most. This function takes in two arguments: ‘country’ and
    ‘population’, and returns a string like this: ‘China has 1441 million people,
    which is about 18.2% of the world.’
  2. To calculate the percentage, ‘describePopulation’ call the
    ‘percentageOfWorld1’ you created earlier
  3. Call ‘describePopulation’ with data for 3 countries of your choice
A
const describePopulation = function (country, population) {
  const percentage = PercentageOfWorld1(population);
  const description =  `${country} has ${population} million people, which is about ${percentage} of the world`;
  console.log(description);
};

describePopulation(‘Portugal’, 10);
describePopulation(‘China’, 1441);
describePopulation(‘USA’, 332);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
  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
    variable called ‘populations’
  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
    world population for these 4 population values. Use the function
    ‘percentageOfWorld1’ that you created earlier to compute the 4
    percentage values
A
const populations = [10, 1441, 332, 83];
console.log(population. length === 4);
const percentages = [
  PercentageOfWorld1(populations[0]), //Function "PercentageOfWorld1" is defined above already.
  PercentageOfWorld1(populations[1]),
  PercentageOfWorld1(populations[2]),
  PercentageOfWorld1(populations[3])
];
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
  1. Create an array containing all the neighbouring countries of a country of your
    choice. Choose a country which has at least 2 or 3 neighbours. Store the array
    into a variable called ‘neighbours’
  2. At some point, a new country called ‘Utopia’ is created in the neighbourhood of
    your selected country. So add it to the end of the ‘neighbours’ array
  3. Unfortunately, after some time, the new country is dissolved. So remove it from
    the end of the array
  4. If the ‘neighbours’ array does not include the country ‘Germany’, log to the
    console: ‘Probably not a central European country :D’
  5. Change the name of one of your neighbouring countries. To do that, find the
    index of the country in the ‘neighbours’ array, and then use that index to
    change the array at that index position. For example, you can search for
    ‘Sweden’ in the array, and then replace it with ‘Republic of Sweden’.
A

const neighbours = [‘Norway’, ‘Sweden’, ‘Russia’];

neighbours. push(‘Utopia’); //Push leaves out what is told
console. log(neighbours);

neighbours. pop(); //Pops out the last of string of the array
console. log(neighbours);

if (!neighbours.includes(‘Germanys’)) { //If neighbours don’t include Germany, then output console.log msg.
console.log(‘Not a Central European Country’);
}

neighbours[neighbours.indexOf(‘Sweden’)] = ‘Republic of Sweden’; //Replaces Sweden with “Republic of Sweden” in the array.
console.log(neighbours)

17
Q
  1. Create an object called ‘myCountry’ for a country of your choice, containing
    properties ‘country’, ‘capital’, ‘language’, ‘population’ and
    ‘neighbours’ (an array like we used in previous assignments)
A
const myCountry = {
  country: 'America',
  capital: 'Alabama',
  language: 'English',
  population: 332000000,
  neighbours: ['Canada', 'Mexico']
};

console.log(“The const of ‘myCountry’ was made and the object is subdivided into 5 sections. This will be used int the ‘DOT VS BRACKET NOTATIONS’ example below.”);

18
Q
  1. Using the object from the previous assignment, log a string like this to the
    console: ‘Finland has 6 million finnish-speaking people, 3 neighbouring countries
    and a capital called Helsinki.’
  2. Increase the country’s population by two million using dot notation, and then
    decrease it by two million using brackets notation.
A

console.log(
${myCountry.country} has ${myCountry.population} million ${myCountry.language} speaking people, ${myCountry.neighbours.length} neighbouring countries and a capital city called ${myCountry.capital}.
);

myCountry.population += 2; //Add 2 people to the population count
console.log(myCountry.population);

myCountry[‘population’] -= 2; //This will then subtract 2 people after adding 2 people just before
console.log(myCountry.population);

19
Q
  1. Add a method called ‘describe’ to the ‘myCountry’ object. This method
    will log a string to the console, similar to the string logged in the previous
    assignment, but this time using the ‘this’ keyword.
  2. Call the ‘describe’ method
  3. Add a method called ‘checkIsland’ to the ‘myCountry’ object. This
    method will set a new property on the object, called ‘isIsland’.
    ‘isIsland’ will be true if there are no neighbouring countries, and false if
    there are. Use the ternary operator to set the property.
A

const myCountry2 = {

country: ‘Finland’,
capital: ‘Helsinki’,
language: ‘Finnish’,
population: 6,
neighbours: [‘Norway’, ‘Sweden’, ‘Russia’],

describe: function () {
console.log(${myCountry2.country} has ${myCountry2.population} million ${myCountry2.language} speaking people, ${myCountry2.neighbours.length} neighbouring countries and a capital city called ${myCountry2.capital}.
); //Took the array of info from the Object and input it into the Template Literal.
},

  checkIsland: function () {
    this.IsIsland = this.neighbours.length === 0  ? true :
    false; //Check if the neighbouring countries are Islands and it will be given in a boolean statement true or false.
  }
};
myCountry2.describe();
myCountry2.checkIsland();
console.log(myCountry2);
20
Q
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
the console (for numbers 1 to 50): 'Voter number 1 is currently voting
  1. Let’s bring back the ‘populations’ array from a previous assignment
  2. Use a for loop to compute an array called ‘percentages2’ containing the
    percentages of the world population for the 4 population values. Use the
    function ‘percentageOfWorld1’ that you created earlier
  3. Confirm that ‘percentages2’ contains exactly the same values as the
    ‘percentages’ array that we created manually in the previous assignment,
    and reflect on how much better this solution is
A
for (let voter = 1; voter <= 10; voter++) //The Loop of the console.log message repeating is until the number indicated, which in this case is 10.
  console.log(`Voter number ${voter} is currently voting`);

const populations2 = [10, 1441, 332, 83];
const percentages2 = [];
for (let i = 0; i < populations2.length; i++) {
const perc = PercentageOfWorld1(populations2[i]); //A secondary method to displaying the population percentages of each country with a for loop.
percentages2.push(perc);
}
console.log(percentages2);

21
Q
  1. Store this array of arrays into a variable called ‘listOfNeighbours’
    [[‘Canada’, ‘Mexico’], [‘Spain’], [‘Norway’, ‘Sweden’,
    ‘Russia’]];
  2. Log only the neighbouring countries to the console, one by one, not the entire
    arrays. Log a string like ‘Neighbour: Canada’ for each country
  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
A
const listOfNeighbours = [
  ['Canada', 'Mexico'],
  ['Spain'],
  ['Norway', 'Sweden', 'Russia'],
];
for (let i = 0; i < listOfNeighbours.length; i++) //This will display the "Neighbours" as the list above.
for (let y = 0; y < listOfNeighbours[i].length; y++) //This will separate each "Neighbours" by each country.
  console.log(`Neighbours: ${listOfNeighbours [i] [y]}`);
22
Q
  1. Recreate the challenge from the lecture ‘Looping Arrays, Breaking and Continuing’,
    but this time using a while loop (call the array ‘percentages3’)
  2. Reflect on what solution you like better for this task: the for loop or the while
    loop?
A
const percentages3 = []; //Crashes the "live-server" cause of endless looping so restart required if mistakae prominent.
let i = 0;
while (i < populations2.length) {
  const perc =  PercentageOfWorld1(populations2[i]);
  percentages3.push(perc);
  i++;
} 
console.log(percentages3);