0% found this document useful (0 votes)
18 views

Array Exercise

Uploaded by

divyaas340
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Array Exercise

Uploaded by

divyaas340
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Array Exercise

1.Write a function that creates an array of numbers from 1 to


`n`.

function createArray(n) {

const arr = [];

for (let i = 1; i <= n; i++) {

arr.push(i); // You can replace this with manual indexing

return arr;

Console.log(createArray(5))

2. Write a function to find the largest number in an array.

function findMax(arr) {

let max = arr[0];

for (let i = 1; i < arr.length; i++) {

if (arr[i] > max) {


max = arr[i];

return max;

Console.log(findMax([1,2,6,4,90]))

3.Write a function to find the smallest number in an array.

function findMin(arr) {

let min = arr[0];

for (let i = 1; i < arr.length; i++) {

if (arr[i] < min) {

min = arr[i];

return min;

Console.log(findMin([3,2,1,7,4]))
4. Write a function to calculate the sum of all elements in an
array.

function sumArray(arr) {

let sum = 0;

for (let i = 0; i < arr.length; i++) {

sum += arr[i];

return sum;

Console.log(sumArray([1,2,3,4,5,6]))

5.Write a function to calculate the average value of an array.

function averageArray(arr) {

let sum = sumArray(arr);

return sum / arr.length;

Console.log(averageArray([1,2,3,4,5]))
6. Write a function that counts the number of even and odd
numbers in an array.

function countEvenOdd(arr) {

let evenCount = 0;

let oddCount = 0;

for (let i = 0; i < arr.length; i++) {

if (arr[i] % 2 === 0) {

evenCount++;

} else {

oddCount++;

return { evenCount, oddCount };

Console.log(countEvenOdd(1,2,3,4,5,6,7,8))
7. Write a function to reverse the elements of an array.

function reverseArray(arr) {

const reversed = [];

for (let i = arr.length - 1; i >= 0; i--) {

reversed.push(arr[i]);

return reversed;

Console.log(reverseArray([1,2,3,4,5]))

8.Write a function that removes duplicates from an array.

function removeDuplicates(arr) {

const unique = [];

for (let i = 0; i < arr.length; i++) {

if (!unique.includes(arr[i])) {

unique.push(arr[i]);

}
return unique;

Console.log(removeDuplicates(1,1,2,3,5,4,7,3,2,5))

9. Write a function that checks if a specific element exists in an


array.

function containsElement(arr, element) {

for (let i = 0; i < arr.length; i++) {

if (arr[i] === element) {

return true;

return false;

console.log(containsElement([1,2,3,4,5,6],6))

10.Write a function that sorts an array without using built-in


sort methods.
function bubbleSort(arr) {

for (let i = 0; i < arr.length; i++) {

for (let j = 0; j < arr.length - i - 1; j++) {

if (arr[j] > arr[j + 1]) {

// Swap

const temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

return arr;

Console.log(bubbleSort([3,4,6,7,1,2]))

11.Write a function that counts the elements in an array


without using the `length` property.
function countElements(arr) {

let count = 0;

for (let i = 0; arr[i] !== undefined; i++) {

count++;

return count;

Console.log(countElements([1,2,3,4,5,6,7,8,9]))

12. Write a function that merges two arrays into one.

function mergeArrays(arr1, arr2) {

const merged = [];

for (let i = 0; i < arr1.length; i++) {

merged.push(arr1[i]);

for (let j = 0; j < arr2.length; j++) {

merged.push(arr2[j]);

return merged;
}

Console.log(mergedArrays([1,2,3,4],[5,6,7,8]))

13. Write a function that returns the index of a specific


element in an array.

function findIndex(arr, element) {

for (let i = 0; i < arr.length; i++) {

if (arr[i] === element) {

return i;

return -1; // Element not found

Console.log(findIndex([1,2,3,4],4))

14.Write a function that shifts all elements in an array to the


left.
function shiftLeft(arr) {

const shifted = [];

for (let i = 1; i < arr.length; i++) {

shifted.push(arr[i]);

return shifted;

Console.log(shiftLeft([1,2,3,4]))

15. Write a function that removes the last element of an array


without using pop.

function removeLast(arr) {

const newArr = [];

for (let i = 0; i < arr.length - 1; i++) {

newArr.push(arr[i]);

return newArr;

Console.log(removeLast([1,2,3,4]))
16.Write a function that retrieves the last element of an array.

function getLastElement(arr) {

return arr[arr.length - 1];

Console.log(getLastElement([1,2,3,4,5]))

17. Write a function that retrieves the first element of an


array.

function getFirstElement(arr) {

return arr[0];

Console.log(getFirstElement([1,2,3,4]))

18.Write a function that repeats an array a specified number


of times.
function repeatArray(arr, times) {

const result = [];

for (let t = 0; t < times; t++) {

for (let i = 0; i < arr.length; i++) {

result.push(arr[i]);

return result;

Console.log(repeatArray([1,2,3,4],3))

19. Write a function that extracts a portion of an array


between two indices.

function extractSubarray(arr, start, end) {

const subarray = [];

for (let i = start; i < end && i < arr.length; i++) {

subarray.push(arr[i]);

return subarray;
}

Console.log(extractSubarray([1,2,3,4,5],2,4))

You might also like