Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
February 18, 2022 11:49 pm GMT

Introduction to Data Structures and Algorithms With Modern JavaScript.

1. Arrays.

An array is a single variable in JavaScript that keeps numerous elements,unlike other languages where array is a reference to several variables. When we wish to keep a list of elements and retrieve them with a single variable, we utilize it frequently.

In JavaScript, an array may hold different items such as Boolean, strings, and numbers, all of which can be stored in a single array.

1.1 Declaring an Array.

An array can be declared in one of the following two ways:

// Method 1:let arr = [];// Method 2:let arr = newArray();

Method 1 is the most commonly used and preferred method above method 2 because when initializing;
Method 1:

// initialization and declaringlet arr = ["mango", "pineapple"];

Method 2:

// initialization and declaring// array has 3 elements/stringslet arr = new Array ("Toyota", "Audi", "Porshe");//array has 4 elements that are definedlet arr1 = new Array (1, 2, 3, 4);//array has 4 undefined elementslet arr2 = new Array (4);

It is evident from the above example that arr1 has 4 items, however arr2 has 4 undefined elements instead of a single element 4. As a result, method 2 is not favored when working with integers, but it is good when working with Boolean and strings, as illustrated above.

In method 2 startup of part 3 can, however, be changed to:

//First create an array of 4 undefined elementslet fruits = new Array(4);// Assign the array valuesCars[0] = "mango";Cars[1] = "apple";Cars[2] = "banana";Cars[3] = "orange";

1.2 Accessing Items in an Array.

Because arrays are indexed from 0, a number in square brackets is used to access elements in an array.

let fruits = ["mango", "apple", "banana"];console.log(fruits[0]); // mangoconsole.log(fruits[1]); // appleconsole.log(fruits[2]); // banana

We already know that 0 always produces the first item in an array. You can use the length property, which we'll discuss later, to retrieve the final element in an array by performing the following procedure.

let fruits = ["mango", "apple", "banana"];const lastItem = fruits.length -1;console.log(fruits[lastItem]); // banana//attempting to access a nonexistent elementconsole.log(fruits[5]); // returns undefined

You need add another index that corresponds to the inner array to be able to retrieve an item in a nested array.

let nestedArray = [    [        "mango",        "banana",    ],    [        "orange",        "avocado",    ]];console.log(nestedArray[1][1]); // avocado

1.3 Length property of an Array.

The number of elements in an array is returned using the length property of arrays.

An array's length attribute can be returned as:

let fruits = ["mango", "apple", "banana"];console.log(fruits.length); // 3

However, to set the number of elements in an array, we may use the assignment operator with the length property.

let fruits = ["mango", "apple", "banana"];fruits.length = 2;console.log(fruits.length); // 2

1.4 Adding an Item to an Array.

We may assign a value to the next index to add a new value to our fruit variable, which has 3 items in the indices 0 to 2.

let fruits = ["mango", "apple", "banana"];fruits[3] = "grape";console.log(fruits);

Output:

[ 'mango', 'apple', 'banana', 'grape' ]

Push() can be used to add an item at the end of an array to avoid scenarios when you mistakenly skip an index while adding an item, resulting in an empty item or items in the array.

let fruits = ["mango", "apple", "banana"];fruits.push("pineapple");console.log(fruits);

Output:

[ 'mango', 'apple', 'banana', 'pineapple' ]

The unshift() function, on the other hand, may be used to add an item to the beginning of an array.

let fruits = ["mango", "apple", "banana"];fruits.unshift("pineapple");console.log(fruits);

Output:

[ 'pineapple', 'mango', 'apple', 'banana' ]

1.5 Removing an Item from an Array.

We utilize the splice() function to remove or delete a specific item from an array.

let fruits = ["mango", "apple", "banana"];fruits.splice(1, 1);console.log(fruits);

Output:

[ 'mango', 'banana' ]

There should be two parameters when using the splice() function. The first parameter specifies the index number to be eliminated (in our case, 1), while the second specifies the number of items to be removed. Otherwise, when one parameter is entered, the item in the index number enter is deleted, along with all subsequent items.

To delete the first item and the last item of an array, use the shift() and pop() methods, respectively. When feasible, however, it is preferable to use the pop() method since the rest of the items in the array will maintain their original index numbers.

//using pop() to remove last itemlet fruits = ["mango", "apple", "banana", "pineapple"];fruits.pop();console.log(fruits);//using shift() to remove first item from the remaining itemsfruits.shift();console.log(fruits);

Output:

[ 'mango', 'apple', 'banana' ][ 'apple', 'banana' ]

1.6 Looping Through an Array.

To loop through an array, we may use the for keyword to loop through the full array, taking use of the length parameter.

//create an array of vehicleslet vehicles = [    "trucks",    "vans",    "buses",    "lorries"];//loop through the length of the arrayfor (let i = 0; i < vehicles.length; i++) {    console.log(i, vehicles[i]);}

Output:

0 'trucks'1 'vans'2 'buses'3 'lorries'

Although it does not obtain the index of each item, using for...of loop is a simpler and more succinct approach of looping through an array.

//create an array of vehicleslet vehicles = [    "trucks",    "vans",    "buses",    "lorries"];//loop through each vehiclefor (let vehicle of vehicles) {    console.log(vehicle);}

Output;

trucksvansbuseslorries

2. Queue

3. Linked list


Original Link: https://dev.to/kashuhappy/introduction-to-data-structures-and-algorithms-with-modern-javascript-58li

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To