Assignments Of Day 8: Arrays in JavaScript

Rashmi Mishra
0

 Assignments Of Day 8: Arrays in JavaScript

Assignment 1: Create and Access Arrays

Problem:
Create an array called colors that contains 5 different color names. Then, access the first and the last element of the array and print them to the console.

Solution:

javascript

Copy code

// Creating an array with 5 colors

let colors = ['Red', 'Green', 'Blue', 'Yellow', 'Purple'];

 

// Accessing the first element

console.log(colors[0]);  // Output: Red

 

// Accessing the last element

console.log(colors[colors.length - 1]);  // Output: Purple


Assignment 2: Use of Array Methods

Problem:
Create an array called numbers with the following values: [5, 10, 15, 20, 25]. Use the push() method to add 30 to the end of the array, and use pop() to remove the last element from the array. After performing these operations, print the updated array.

Solution:

javascript

Copy code

let numbers = [5, 10, 15, 20, 25];

 

// Using push() to add 30 to the end of the array

numbers.push(30);

console.log(numbers);  // Output: [5, 10, 15, 20, 25, 30]

 

// Using pop() to remove the last element

numbers.pop();

console.log(numbers);  // Output: [5, 10, 15, 20, 25]


Assignment 3: Use of shift() and unshift() Methods

Problem:
Create an array called fruits with the following elements: ['Apple', 'Banana', 'Orange']. Use the shift() method to remove the first element, and then use the unshift() method to add 'Mango' to the beginning of the array. Print the updated array.

Solution:

javascript

Copy code

let fruits = ['Apple', 'Banana', 'Orange'];

 

// Using shift() to remove the first element

fruits.shift();

console.log(fruits);  // Output: ['Banana', 'Orange']

 

// Using unshift() to add 'Mango' at the beginning

fruits.unshift('Mango');

console.log(fruits);  // Output: ['Mango', 'Banana', 'Orange']


Assignment 4: Splice Method

Problem:
Create an array vegetables with the following elements: ['Carrot', 'Cucumber', 'Tomato', 'Onion']. Use the splice() method to remove 'Cucumber' and 'Tomato', and add 'Lettuce' and 'Broccoli' in their place. Print the updated array.

Solution:

javascript

Copy code

let vegetables = ['Carrot', 'Cucumber', 'Tomato', 'Onion'];

 

// Using splice() to remove 'Cucumber' and 'Tomato' and add 'Lettuce' and 'Broccoli'

vegetables.splice(1, 2, 'Lettuce', 'Broccoli');

console.log(vegetables);  // Output: ['Carrot', 'Lettuce', 'Broccoli', 'Onion']


Assignment 5: Slice Method

Problem:
Create an array numbers with the values [1, 2, 3, 4, 5, 6, 7]. Use the slice() method to extract a subarray that contains only the values from index 2 to index 4 (inclusive) and print the result.

Solution:

javascript

Copy code

let numbers = [1, 2, 3, 4, 5, 6, 7];

 

// Using slice() to get a subarray from index 2 to index 4

let subarray = numbers.slice(2, 5);  // The second parameter is exclusive

console.log(subarray);  // Output: [3, 4, 5]


Assignment 6: Iterating Over an Array

Problem:
Create an array names with the following elements: ['Alice', 'Bob', 'Charlie', 'David']. Use a for loop to iterate through the array and print each name to the console.

Solution:

javascript

Copy code

let names = ['Alice', 'Bob', 'Charlie', 'David'];

 

// Using a for loop to iterate through the array

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

    console.log(names[i]);

}

// Output:

// Alice

// Bob

// Charlie

// David


Assignment 7: Using forEach()

Problem:
Create an array colors with the following values: ['Red', 'Green', 'Blue', 'Yellow']. Use the forEach() method to print each color in uppercase.

Solution:

javascript

Copy code

let colors = ['Red', 'Green', 'Blue', 'Yellow'];

 

// Using forEach() to iterate through the array and print each color in uppercase

colors.forEach(function(color) {

    console.log(color.toUpperCase());

});

// Output:

// RED

// GREEN

// BLUE

// YELLOW


Assignment 8: Array Length and Accessing Last Element

Problem:
Create an array marks with the following values: [95, 80, 72, 85, 92]. Write a program to find the length of the array and access the last element of the array using the length property.

Solution:

javascript

Copy code

let marks = [95, 80, 72, 85, 92];

 

// Finding the length of the array

let length = marks.length;

console.log(length);  // Output: 5

 

// Accessing the last element using length

let lastMark = marks[length - 1];

console.log(lastMark);  // Output: 92


Assignment 9: Combining Arrays Using concat()

Problem:
Create two arrays: array1 = [1, 2, 3] and array2 = [4, 5, 6]. Use the concat() method to combine them into a new array called combinedArray, and print the result.

Solution:

javascript

Copy code

let array1 = [1, 2, 3];

let array2 = [4, 5, 6];

 

// Using concat() to combine the two arrays

let combinedArray = array1.concat(array2);

console.log(combinedArray);  // Output: [1, 2, 3, 4, 5, 6]


Assignment 10: Find Element in an Array

Problem:
Create an array numbers = [10, 20, 30, 40, 50]. Write a program to check if the number 30 exists in the array, and print Found if it does, or Not Found if it doesn't.

Solution:

javascript

Copy code

let numbers = [10, 20, 30, 40, 50];

 

// Checking if 30 exists in the array

if (numbers.includes(30)) {

    console.log('Found');  // Output: Found

} else {

    console.log('Not Found');

}


These assignments help practice key concepts like array creation, accessing elements, array methods (like push(), pop(), shift(), unshift(), splice(), slice()), and iteration using loops.

4o mini

You said:

give more

ChatGPT said:

ChatGPT

Here are more assignments with solutions on the concept of Arrays in JavaScript:


Assignment 11: Filtering Even Numbers

Problem:
Create an array numbers with the values [1, 2, 3, 4, 5, 6, 7, 8]. Use the filter() method to create a new array evenNumbers that contains only the even numbers from the original array. Print the new array.

Solution:

javascript

Copy code

let numbers = [1, 2, 3, 4, 5, 6, 7, 8];

 

// Using filter() to find even numbers

let evenNumbers = numbers.filter(function(num) {

    return num % 2 === 0;

});

 

console.log(evenNumbers);  // Output: [2, 4, 6, 8]


Assignment 12: Sum of All Elements in an Array

Problem:
Create an array nums with the values [5, 10, 15, 20]. Use a loop to find the sum of all the elements in the array and print the result.

Solution:

javascript

Copy code

let nums = [5, 10, 15, 20];

let sum = 0;

 

// Using a for loop to sum the elements

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

    sum += nums[i];

}

 

console.log(sum);  // Output: 50


Assignment 13: Sorting an Array

Problem:
Create an array numbers with the values [30, 5, 40, 10, 25]. Use the sort() method to sort the array in ascending order and print the sorted array.

Solution:

javascript

Copy code

let numbers = [30, 5, 40, 10, 25];

 

// Using sort() to sort the array in ascending order

numbers.sort(function(a, b) {

    return a - b;

});

 

console.log(numbers);  // Output: [5, 10, 25, 30, 40]


Assignment 14: Finding Maximum and Minimum Value in an Array

Problem:
Create an array numbers with the values [12, 45, 7, 34, 56]. Write a program to find the maximum and minimum values in the array.

Solution:

javascript

Copy code

let numbers = [12, 45, 7, 34, 56];

 

// Finding the maximum value

let maxValue = Math.max(...numbers);

 

// Finding the minimum value

let minValue = Math.min(...numbers);

 

console.log("Maximum Value:", maxValue);  // Output: Maximum Value: 56

console.log("Minimum Value:", minValue);  // Output: Minimum Value: 7


Assignment 15: Merge Two Arrays

Problem:
Create two arrays array1 = [1, 2, 3] and array2 = [4, 5, 6]. Merge these two arrays into a single array using the concat() method and print the result.

Solution:

javascript

Copy code

let array1 = [1, 2, 3];

let array2 = [4, 5, 6];

 

// Merging the arrays using concat()

let mergedArray = array1.concat(array2);

 

console.log(mergedArray);  // Output: [1, 2, 3, 4, 5, 6]


Assignment 16: Reverse an Array

Problem:
Create an array numbers with the values [10, 20, 30, 40]. Use the reverse() method to reverse the order of the array and print the result.

Solution:

javascript

Copy code

let numbers = [10, 20, 30, 40];

 

// Reversing the array using reverse()

numbers.reverse();

 

console.log(numbers);  // Output: [40, 30, 20, 10]


Assignment 17: Find the Index of an Element

Problem:
Create an array fruits with the values ['Apple', 'Banana', 'Cherry', 'Date']. Use the indexOf() method to find the index of 'Cherry' in the array and print the result.

Solution:

javascript

Copy code

let fruits = ['Apple', 'Banana', 'Cherry', 'Date'];

 

// Finding the index of 'Cherry'

let index = fruits.indexOf('Cherry');

 

console.log(index);  // Output: 2


Assignment 18: Check if an Array Contains a Specific Element

Problem:
Create an array animals with the values ['Lion', 'Tiger', 'Elephant']. Write a program to check if the array contains the element 'Elephant' using the includes() method.

Solution:

javascript

Copy code

let animals = ['Lion', 'Tiger', 'Elephant'];

 

// Checking if 'Elephant' exists in the array

if (animals.includes('Elephant')) {

    console.log('Elephant is found in the array');  // Output: Elephant is found in the array

} else {

    console.log('Elephant is not found in the array');

}


Assignment 19: Convert Array to String

Problem:
Create an array colors with the values ['Red', 'Green', 'Blue']. Use the join() method to convert the array into a string where each element is separated by a comma.

Solution:

javascript

Copy code

let colors = ['Red', 'Green', 'Blue'];

 

// Converting the array to a string using join()

let colorString = colors.join(', ');

 

console.log(colorString);  // Output: Red, Green, Blue


Assignment 20: Find the Sum of Odd Numbers in an Array

Problem:
Create an array numbers with the values [3, 5, 8, 10, 13, 15]. Write a program that calculates the sum of all odd numbers in the array.

Solution:

javascript

Copy code

let numbers = [3, 5, 8, 10, 13, 15];

let sumOdd = 0;

 

// Iterating through the array and summing odd numbers

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

    if (numbers[i] % 2 !== 0) {

        sumOdd += numbers[i];

    }

}

 

console.log(sumOdd);  // Output: 36 (3 + 5 + 13 + 15)


Assignment 21: Removing Duplicate Elements from an Array

Problem:
Create an array numbers with the values [10, 20, 30, 20, 10]. Write a program to remove duplicate elements from the array.

Solution:

javascript

Copy code

let numbers = [10, 20, 30, 20, 10];

 

// Using Set to remove duplicates

let uniqueNumbers = [...new Set(numbers)];

 

console.log(uniqueNumbers);  // Output: [10, 20, 30]


Assignment 22: Loop Through an Array Using forEach()

Problem:
Create an array names with the values ['John', 'Jane', 'Jack']. Use the forEach() method to print each name with a message "Hello, <name>!".

Solution:

javascript

Copy code

let names = ['John', 'Jane', 'Jack'];

 

// Using forEach() to loop through the array and print a message

names.forEach(function(name) {

    console.log(`Hello, ${name}!`);

});

// Output:

// Hello, John!

// Hello, Jane!

// Hello, Jack!


Assignment 23: Convert Array to Number Array

Problem:
Create an array stringNumbers with the values ['1', '2', '3', '4']. Convert the array to a number array using the map() method and print the result.

Solution:

javascript

Copy code

let stringNumbers = ['1', '2', '3', '4'];

 

// Using map() to convert string array to number array

let numberArray = stringNumbers.map(function(num) {

    return Number(num);

});

 

console.log(numberArray);  // Output: [1, 2, 3, 4]


Assignment 24: Check if Array is Empty

Problem:
Write a program that checks if an array data is empty or not, and print "Array is empty" if it is, or "Array is not empty" if it is not.

Solution:

javascript

Copy code

let data = [];

 

// Checking if the array is empty

if (data.length === 0) {

    console.log('Array is empty');

} else {

    console.log('Array is not empty');

}

// Output: Array is empty


Assignment 25: Count Occurrences of an Element in an Array

Problem:
Create an array numbers with the values [1, 2, 3, 2, 4, 2]. Write a program to count how many times the number 2 appears in the array.

Solution:

javascript

Copy code

let numbers = [1, 2, 3, 2, 4, 2];

let count = 0;

 

// Counting the occurrences of 2

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

    if (numbers[i] === 2) {

        count++;

    }

}

 

console.log(count);  // Output: 3



Post a Comment

0Comments

Post a Comment (0)