You are on page 1of 7

JAVASCRIPT ARRAY METHODS

1.Write a JavaScript program to create an array of students heights(cm) with


given inputs and add a new student's height 145(cm) into the array
Input: [156,150,149,152,151,147]
Output: [156,150,149,152,151,147,145]
Program:
<script>
var arr=[156,150,149,152,151,147];
console.log("array before adding "+arr);
arr.push(145);
console.log("array after adding "+arr);
</script>

2.Write a Javascript Program to create an array of hospital visitors and remove


the latest visitor out of the array
Input: ['Ram', 'Manas', 'Komali', 'Rajesh']
Output: ['Ram', 'Manas', 'Komali']
Program:
<script>

var arr=['Ram','Manas','Komali','Rajesh'];

console.log("array before removing "+arr);

arr.pop();

console.log("array after Removing "+arr);

</script>

3.Write a JavaScript program to create an array of input number and add a


number "25" to the array after removing the latest number entry
Input: [12, 78,14,20,62]
Output:[12,78,14,20,25]
Program:
<script>

var arr=[12,78,14,20,62];

console.log("array before replacing "+arr);

arr.pop();

arr.push(25);

console.log("array after replacing "+arr);

</script>
4.Write a Javascript program to create an array of online orders of Mc Donalds
on basis of their first come first serve basis and return the remaining pending
orders after the first order is out for delivery.
Input: ['Burger', 'Sandwich', 'fries', 'Coffee']
Output: ['Sandwich', 'fries', 'Coffee']
Program:
<script>

var arr=['Burger','Sandwich','Fries','Coffee'];

console.log("array before "+arr);

arr.shift(0);

console.log("array after "+arr);

</script>

5.Write a JavaScript program to create an array of input  consecutive railway


stations and return the latest passed station that is crossed in the journey.
Input: ['Vijayawada','Khammam','Warangal','Hyderabad']
Output: Vijayawada
Program:
<script>

var arr=['Vijayawada','Khammam','Warangal','Hyderabad'];

console.log("stations "+arr);

console.log("Current station "+arr.shift());

</script>

 
6.Write a Javascript program to create an array of even numbers and add an odd
number "25" in the beginning of the array.
Input: [12,18,22,34,48,72]
Output: [25,12,18,22,34,48,72]
Program:
<script>

var arr=[12,18,22,34,48,72];

console.log("before "+arr);

arr.unshift(25);

console.log("after "+arr);
</script>

7.Write a Javascript program  create an array of given input and return the
length of the array
Input: [Chair, Table, Cupboard, Bench, Door[
Output:5
Program:
<script>

var arr=['Chair','Table','Cupboard','Bench','Door'];

console.log("length of arr "+arr.length);

</script>

8.Write a Javascript program to create an array of boys and return the array after
deleting the first and last elements in the array.
Input: ['Manish', 'Adarsh', 'Sameer', 'Raman', 'Bhaskar','Teja']
Output: [ 'Adarsh', 'Sameer', 'Raman', 'Bhaskar']
Program:
<script>

var arr=['Manish','Adarsh','Sameer','Raman','Bhaskar','Teja'];

console.log("before "+arr);

arr.shift();

arr.pop();

console.log("after "+arr);

</script>

9.Write a JavaScript program to create an array of various stocks as in input and


add a stock ['Cipla','ITC'] at index position "2".
INPUT:['Reliance', 'Titan', 'Wipro', 'Bajaj']
Output:['Reliance', 'Titan', 'Cipla', 'ITC', 'Wipro', 'Bajaj']
Program:
<script>

var arr=['Reliance','Titan','Wipro','Bajaj'];

console.log("before "+arr);

arr.splice(2,0,'Cipla','ITC');

console.log("after "+arr);
</script>

10.Write a javascript program to create an array of given elements and remove


"3" elements from the index position "2" using splice( ) array method
Input:[10,14,74,45,52,56,47,40,21]
Output:[10,14,56,47,40,21 ]
Program:
<script>

var arr=[10,14,74,45,52,56,47,40,21];

console.log("before "+arr);

arr.splice(2,3);

console.log("After "+arr);

</script>

11.Write a javascript program to find the youngest driver age among the given
array of ages of the drivers.
Input: [45,51,36,28,25,17,19,20,25]
Output: 17
Program:
<script>

var arr=[45,51,36,28,25,17,19,20,25];

console.log("array "+arr);

arr.sort();

console.log("min age "+arr[0]);

</script>

12.Write a javascript program to find the name "Radha" from array of  the log
register of the Employee and return found if you find.
Input: ['Ravali', 'Rakesh', 'Suman', 'Saikiran', 'Radha', 'Rani']
Output: found
Program:
<script>

var arr=['Ravali','Rakesh','Suman','Saikiran','Radha','rani'];

function finding(arr)

{
if(arr=='Radha')

return true;

else

return false;

var op=arr.find(finding);

if(op=='Radha')

console.log('Found');

else

console.log('not found');

</script>

13.Write a javascript program to create Voters age array. find if the age less
than 18 from array and delete it.
Return the array.
Input:[20,45,31,39,17,19,40,34,18]
Output:[20,45,31,39,19,40,34,18]
Program:
<script>

var arr=[20,45,31,39,17,19,40,34,18];

function checkAge(age)

return age>=18;
}

var aged=arr.filter(checkAge);

console.log(aged);

</script>

14.Write a JavaScript program


Consider an array of groceries and find "Cheese" and return the index of it  from
the array
Input:['milk', 'eggs', 'bread', 'butter', 'Cheese', 'Sugar']
Ouput: 4
Program:
<script> .

var arr=['milk','eggs','bread','butter','cheese','sugar'];

function findindex(ip)

if(ip=='cheese')

return true;

Else

return false;

var op=arr.findIndex(findindex);

console.log(op);

</script>

15.Write a javascript program for an object constructor:


Create an object "car" and a constructor with arguments
(make,model,year,color) and assign values to the object 
Input: Car("Hyundai","venue",2021,"blue")
Output:(in console) Hyundai venue 2021 blue 
Program:

<script>

function car(carCompany,carName,carYear,carColor)

this.carCompany=carCompany;

this.carName=carName;

this.carYear=carYear;

this.carColor=carColor; };

var hyndai=new car('Hyundai','Venue',2021,'Blue');

console.log(hyndai);

</script>

You might also like