Array
How do we store information?
We use variables to store the data.
For Example: If I want to store the name of a student, In that case, I will use a
variable
var name = “Varun”;
But What if, I want to store the names of 5 students
name1 = “Prateek”;
name2 = “Nrupul”;
name3 = “Yogesh”;
name4 = “Aman”;
name5 = “Albert”;
To store 5 names, we need to declare 5 variables.
Isn’t possible that one variable will contain all names?
Yes, It is possible with the array.
Arrays
The array is a contiguous chunk of memory that can store multiple values.
Declaration of an array
var arr = [];
If I want to store all 5 names in a single variable, then it is possible through an array
var arr = [”Prateek”, “Nrupul”,”Yogesh”,”Aman”,”Albert”];
Each value is stored at some index.
Array 1
the array index starts from 0.
Code 1: Declare and print 3 students names using variables
var name1 = “Rahul”;
var name2 = “Shubham”;
var name3 = “Rishabh”;
[Link](name1);
[Link](name2);
[Link](name3);
Code 2: Declare and Print 3 students names using an array
var names = [”Rahul”, “Shubham”, “Rishabh”];
[Link](names[0]);
[Link](names[1]);
[Link](names[2]);
Code 3: Perform the following tasks :
1. Create an array of vegetables
2. Store 3 vegetables
3. Print all the vegetables
var vegetables = [”Tomato”, “Beans”, “Onion”];
[Link](vegetables[0]);
[Link](vegetables[1]);
[Link](vegetables[2]);
Note: Don’t write vegetables[3] that will give undefined.
How to find the length of an array?
It means How many elements present in the array.
Use the length function to calculate the length.
Code 4: Find the length of the vegetables array.
Array 2
var vegetables = [”Tomato”, “Beans”, “Onion”];
[Link]([Link]);
Code 5: Perform the following tasks :
1. Create an array price.
2. Store the prices of 3 products in the array
3. Print the price of the last product.
Not a generic code :
var prices = [45, 71, 29];
[Link](prices[2]);
Generic Code :
var prices = [45, 71, 29];
last_index = [Link]-1;
[Link](prices[last_index]);
How to add elements in an array?
push() function helps to insert the elements in an array.
push() always inserts at the last.
Code 6: Insert 3 movie names in the arrays .
var items2 = [];
[Link](”Bahuballi”);
[Link](”Avengers”);
[Link](”Spider Man”);
[Students Task]
Code 7: Perform the following tasks :
Array 3
1. Create an array superheroes
2. push 3 superheroes in the array
3. Print the array
var superheroes = [];
[Link](”bat man”);
[Link](”super man”);
[Link](”iron man”);
[Link](superheroes);
How to update the array?
Suppose I want to change the first index value.
superheroes[0] = “Thor”;
How to print all elements using Loop?
Write a for loop from starting i = 0 to length of array - 1.
print all the elements using a loop.
Code 8: print all the elements of the array using a loop.
var movies = [];
[Link](”bat man”);
[Link](”super man”);
[Link](”iron man”);
for(var i = 0; i<[Link]; i++)
{
[Link](movies[i]);
}
Note : Don't run the loop till [Link], It will show undefined for last index
because last index does n't exist for movies.
Array 4
[Students Task]
Code 9: Perform the following tasks :
1. Create an array of movies and actors
2. Print all the movies names with actors
var movies = ["bahuballi", "Spider Man", "Iron Man", "Super Man"];
var actors = ["Prabhas", "Tom holland", "Robert Downey", "Henry Cavil"];
for(var i=0; i<[Link]; i++){
[Link](movies[i]," - ",actors[i]);
}
Note : Length of both arrays should be same
How to remove elements from an array?
To remove elements, we have a pop() function
pop() function that will remove elements from the last.
Code 10: pop the last 2 elements from an array
var movies = [];
[Link](”bat man”);
[Link](”super man”);
[Link](”iron man”);
[Link]();
[Link]();
[Link](movies);
[Students Task]
Code 11: Perform the following tasks :
1. Create an array of 6 numbers
2. print the numbers array
Array 5
3. delete last 3 numbers from that array
4. print the numbers array
First Way
var numbers = [2,3,4,5,6,7];
[Link](numbers);
[Link]();
[Link]();
[Link]();
[Link](numbers);
Second Way
var numbers = [2,3,4,5,6,7];
[Link](numbers);
for(var i=1; i<=3; i++)
{
[Link]();
}
[Link](numbers);
Arrays with Loop and Break
Code 12: Print the first 3 items in the array using a loop.
First Way
var movies = ["bahuballi", "Spider Man", "Iron Man", "Super Man"];
for(var i = 1; i<=3; i++)
{
[Link](movies[i]);
}
Second Way [ Using Break ]
var movies = ["bahuballi", "Spider Man", "Iron Man", "Super Man"];
for(var i = 0; i<[Link]; i++)
{
if(i==3)
Array 6
{
break;
}
[Link](movies[i]);
}
Arrays with Loop and Continue
Code 12: Print all movies except the third movie.
var movies = ["bahuballi", "Spider Man", "Iron Man", "Super Man"];
for(var i = 0; i<[Link]; i++)
{
if(i==2)
{
continue;
}
[Link](movies[i]);
}
Code 13: Print all movies except the third and fifth movies.
var movies = ["bahuballi", "Spider Man", "Iron Man", "Super Man","hulk","thor"];
for(var i = 0; i<[Link]; i++)
{
if(i==2 || i==4)
{
continue;
}
[Link](movies[i]);
}
Code 14 : Print the last 3 items of an products array
Array 7
**Approach 1 :**
var products = ["earphone", "headphones", "mic", "ipad", "cell phone", "laptop"];
var start = [Link] - 3;
for(var i=start; i<[Link]; i++)
{
[Link](products[i]);
}
Above Approach 1 will fail, if suppose the products array is
var products = ["earphone", "headphones"]
**Approach 2 :**
var products = ["earphone", "headphones", "mic", "ipad", "cell phone", "laptop"];
var start = 0;
if([Link]>3)
{
start = [Link] - 3;
}
for(var i=start; i<[Link]; i++)
{
[Link](products[i]);
}
Code 15 : For Even Array, print the second half of the array
var names = ["A","B","C","D","E","F","G","H"];
var start = [Link]([Link]/2);
for(var i=start; i<[Link]; i++)
{
[Link](names[i]);
}
Code 16 : For Even or Odd Array, print the second half of the array
Array 8
var names = ["A","B","C","D","E","F","G","H","K"];
var start=0;
// Handle Even Array
if([Link] % 2 == 0)
{
start = [Link]/2;
}
// Handle Odd Array
else
{
start = [Link]([Link]/2);
}
for(var i=start; i<[Link]; i++)
{
[Link](names[i]);
}
Code 17 : For Even or Odd Array, print the first half of the array
var names = ["A","B","C","D","E","F","G","H","K"];
var start=0;
if([Link] % 2 == 0)
{
end = [Link]/2;
}
else
{
end = [Link]([Link]/2);
}
for(var i=0; i<end; i++)
{
[Link](names[i]);
}
Code 18 : Given marks, find the total marks
var marks = [10, 15, 19, 20, 21];
var sum=0;
for(var i = 0; i<[Link]; i++)
{
Array 9
sum = sum+marks[i];
}
[Link](sum);
Code 19 : Find the sum of all subject marks and average also.
var subject_marks = [10, 15, 19, 20, 21];
var sum_marks = 0;
for(var i=0; i<subject_marks.length; i++)
{
sum_marks = sum_marks + subject_marks[i];
}
var average = [Link](sum_marks/subject_marks.length);
[Link]("Total sum is ",sum_marks);
[Link]("Average is ",average);
Code 20 : Given marks, find the maximum marks
var marks = [10, 15, 19, 20, 21,45, 31, 18];
var max = -Infinity;
for(var i = 0; i<[Link]; i++)
{
if(max<marks[i])
{
max = marks[i];
}
}
[Link](max);
Array 10