Arrays Flashcards
What is an Array ?
Its is a data structure,
Example - we can store the data and when required we can further process it.
const friend1 = 'hemal' const friend2 = 'samir' const friend3 = 'Amit'
Type1
const friends = [‘hemal’, ‘samir’, ‘Amit’, ‘Manish’];
console.log(friends);
Type 2
console. log(friends[0]);
console. log(friends[2]);
console. log(friends[1]);
Type 3
console. log(friends.length);
console. log(friends[friends.length - 2]);
Coding Challenge ( Arrays )
Change on of the friend to Vishal from the below list
const friends = [‘hemal’, ‘samir’, ‘Amit’, ‘Manish’];
friends[2] = ‘Vishal’
console.log(friends);
Coding Challenge ( Arrays )
- 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
const populations = [10, 1441, 332, 83]; console.log(populations.length === 4); const percentages = [ percentageOfWorld1(populations[0]), percentageOfWorld1(populations[1]), percentageOfWorld1(populations[2]), percentageOfWorld1(populations[3]) ]; console.log(percentages);
Basic Operator in Arrays
Add new member (Vishal) in the last of friends list name
const friends1 = [‘hemal’, ‘samir’, ‘Amit’, ‘Manish’];
friends1. push(‘Jay’);
console. log(friends1);
Output :- It will add Jay in the last
Basic Operator in Arrays
Add new member (Rakesh) in the start of friends name const friends1 = ['hemal', 'samir', 'Amit', 'Manish'];
friends1. unshift(‘jackie’)
console. log(friends1);
OutPut :- Jackie will be added as first name.
Basic Operator ( Arrays )
Remove Last Name from the below list
const friends1 = [‘hemal’, ‘samir’, ‘Amit’, ‘Manish’];
friends1. pop(friends1);
console. log(friends1)
Output :- It will remove the last name from the list
Basic Operator ( Arrays )
Remove First Name from the below list const friends1 = ['hemal', 'samir', 'Amit', 'Manish'];
friends1. shift()
console. log(friends1);
Output :- First name will be remove from the list.
Basic Operator ( Arrays )
Identify the Index of hemal in the list
Example
const friends1 = [‘hemal’, ‘samir’, ‘Amit’, ‘Manish’];
console.log(friends1.indexOf(‘hemal’));
OUTPUT :- 0
Coding Challenge - Basic Operator ( Arrays )
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’.
const neighbours = [‘Norway’, ‘Sweden’, ‘Russia’];
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);