JAVASCRIPT FUNDAMENTALS Flashcards
- Declare variables called ‘country’, ‘continent’ and ‘population’ and
assign their values according to your own country (population in millions) - Log their values to the console
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.
);
- 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 - Log the types of ‘isIsland’, ‘population’, ‘country’ and ‘language’
to the console
let IsIsland = false; //Boolean let Language; //Undefined console.log(typeof IsIsland); console.log(typeof Population); console.log(typeof Country); console.log(typeof Language);
- Set the value of ‘language’ to the language spoken where you live (some
countries have multiple languages, but just choose one) - Think about which variables should be const variables (which values will never
change, and which might change?). Then, change these variables to const. - Try to change one of the changed variables now, and observe what happens
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}`);
- If your country split in half, and each half would contain half the population,
then how many people would live in each half? - Increase the population of your country by 1 and log the result to the console
- Finland has a population of 6 million. Does your country have more people than
Finland? - The average population of a country is 33 million people. Does your country
have less people than the average country? - 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
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}
);
- Recreate the ‘description’ variable from the last assignment, this time
using the template literal syntax
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.
- 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) - After checking the result, change the population temporarily to 13 and then to
- See the different results, and set the population back to original
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` ); }
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;
- Execute the operations to check if you were right
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])
- Declare a variable ‘numNeighbours’ based on a prompt input like this:
prompt(‘How many neighbour countries does your country
have?’); - If there is only 1 neighbour, log to the console ‘Only 1 border!’ (use loose equality
== for now) - Use an else-if block to log ‘More than 1 border’ in case ‘numNeighbours’
is greater than 1 - Use an else block to log ‘No borders’ (this block will be executed when
‘numNeighbours’ is 0 or any other value) - Test the code with different values of ‘numNeighbours’, including 1 and 0.
- 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? - Finally, convert ‘numNeighbours’ to a number, and watch what happens now
when you input 1 - Reflect on why we should use the === operator and type conversion in this
situation
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’);
};
- Comment out the previous code so the prompt doesn’t get in the way
- 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. - 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. - 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 :(‘ - 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)
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`); };
- 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
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.”);
}
- 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! - After checking the result, change the population temporarily to 13 and then to
- See the different results, and set the population back to original
let Population_3 = 83240000;
console.log( `${Country}'s population is ${ Population_3 > 33000000 ? "above" : "below" } average` );
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'
- 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
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);
- 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 - To calculate the percentage, divide the given ‘population’ value by 7900
and then multiply by 100 - Call ‘percentageOfWorld1’ for 3 populations of countries of your choice,
store the results into variables, and log them to the console - 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) - Recreate the last assignment, but this time create an arrow function called
‘percentageOfWorld3’
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); */
- 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.’ - To calculate the percentage, ‘describePopulation’ call the
‘percentageOfWorld1’ you created earlier - Call ‘describePopulation’ with data for 3 countries of your choice
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);
- 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’ - Log to the console whether the array has 4 elements or not (true or false)
- 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
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]) ];