Coding Assignments To Master Arrays

Coding Assignments To Master Arrays

Mastering arrays in JavaScript is a fundamental step towards becoming proficient in the language, as arrays are used to store multiple values in a single variable. They are essential for managing lists of elements, performing data manipulations, and implementing algorithms.

Here are five progressively challenging assignments to help you master arrays in JavaScript. Each assignment builds on the previous ones, allowing you to apply and reinforce your knowledge.


Assignment 1: Basic Operations on Arrays

  1. Create an Array: Initialize an array named fruits with the values 'Apple', 'Banana', 'Cherry'.
  2. Access Elements: Log the second item in the fruits array to the console.
  3. Add Elements: Add 'Dragonfruit' to the end of the fruits array using push().
  4. Remove Elements: Remove the first element of the fruits array using shift() and log the removed element.
  5. Find the Length: Log the length of the fruits array.

Assignment 2: Iterating over Arrays

  1. Basic Looping: Use a for loop to log each item in the fruits array.
  2. Advanced Looping: Use the forEach method to log each item in the fruits array along with its index.
  3. Filtering: Create a new array named longFruits that contains only the fruits in the fruits array with more than 6 characters in their name, using filter().
  4. Mapping: Create a new array named upperCaseFruits that contains all fruits from the fruits array in uppercase, using map().
  5. Reducing: Find the total length of all fruit names in the fruits array combined, using reduce().

Assignment 3: Array Manipulations

  1. Concatenating Arrays: Concatenate another array of fruits to the existing fruits array and log the result.
  2. Slicing Arrays: Create a new array that contains the first two fruits of the fruits array using slice().
  3. Splicing: Remove the second fruit in the fruits array and add two new fruits in its place using splice().
  4. Reversing an Array: Reverse the fruits array and log the result.
  5. Sorting: Sort the fruits array in alphabetical order and log the result.

Assignment 4: Multidimensional Arrays

  1. Creating a Multidimensional Array: Create a 2D array named matrix that represents a 3x3 matrix filled with numbers.
  2. Accessing Elements: Log the element in the second row, third column of the matrix.
  3. Row-wise Iteration: Use nested loops to log every element in the matrix row by row.
  4. Column-wise Iteration: Modify the loop to log every element column by column.
  5. Diagonal Elements: Log the elements on the main diagonal of the matrix (top-left to bottom-right).

Assignment 5: Advanced Array Techniques

  1. Flattening Arrays: Given a multidimensional array, flatten it into a single array using Array.prototype.flat().
  2. Array Destructuring: Given an array [1, 2, 3, 4, 5], use array destructuring to create variables for the first and third elements.
  3. Spread Operator: Combine two arrays into one using the spread operator.
  4. Finding Unique Elements: Given an array of numbers, find all unique numbers using Set.
  5. Array Rotation: Write a function that rotates an array to the right by n steps without using extra space for another array.

Resources for You

ChatGPT Guide For Software Developers

Learn to use ChatGPT to stay ahead of competition

Front-End Developer Interview Kit

Today, Start preparing to get your dream job!

JavaScript Developer Kit

Start your JavaScript journey today!

Are you looking for Front-end Developer Job?

Get Front-end Interview Kit Now, And Be Prepared to Get Your Dream Job

Get Front-end Interview Kit

Newsletter for Developers!

Join our newsletter to get important Web Development and Technology Updates