Front End Technology Articles

Page 368 of 652

Swap kth element of array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 679 Views

We are required to write a JavaScript function that accepts an array of Numbers and a number, say k (k must be less than or equal to the length of array). And our function should replace the kth element from the beginning with the kth element from the end of the array. Understanding the Problem When we say "kth element from beginning" and "kth element from end", we need to understand the positioning: kth element from beginning: index = k - 1 kth element from end: index = array.length - k Example ...

Read More

Squared sum of n odd numbers - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 340 Views

We are required to write a JavaScript function that takes in a Number, say n, and finds the sum of the square of first n odd natural Numbers. For example, if the input number is 3, we need to find the first 3 odd numbers (1, 3, 5) and calculate the sum of their squares: 1² + 3² + 5² = 1 + 9 + 25 = 35 Understanding the Pattern The first n odd natural numbers follow the pattern: 1, 3, 5, 7, 9... The formula for the i-th odd number is (2 * i) - 1. Example const num = 3; const squaredSum = num => { let sum = 0; for(let i = 1; i

Read More

Arrays Data Structure in Javascript

Samual Sam
Samual Sam
Updated on 15-Mar-2026 603 Views

The array is a container which can hold a fixed number of items and these items should be of the same type. It stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. Why do we need arrays? Let's say you want to record the average temperatures of all days of the week. You can record them as follows − let avgTempMon = 35; let ...

Read More

Adding an element at the end of the array in Javascript

Lokesh Badavath
Lokesh Badavath
Updated on 15-Mar-2026 600 Views

In JavaScript, adding elements to the end of an array is a fundamental operation. The most common and efficient method is using the push() method, which modifies the original array by appending one or more elements. An array is a special variable that can hold multiple values in a single ordered collection. Arrays in JavaScript are dynamic, meaning you can add or remove elements after creation. What is an Array? An array is a collection of items stored at contiguous memory locations. Arrays allow random access to elements, making it faster to access elements by their position ...

Read More

Change string based on a condition - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 813 Views

We are required to write a JavaScript function that takes in a string. The task of our function is to change the string according to the following condition − If the first letter in the string is a capital letter then we should change the full string to capital letters. Otherwise, we should change the full string to small letters. Example Following is the code − const str1 = "This is a normal string"; const str2 = "thisIsACamelCasedString"; const changeStringCase = str => { ...

Read More

Adding an element at the start of the array in Javascript

Lokesh Badavath
Lokesh Badavath
Updated on 15-Mar-2026 454 Views

In JavaScript, adding an element at the start of an array is a common operation. The most straightforward method is using the unshift() method, which adds one or more elements to the beginning of an array and returns the new length. An array is a data structure that stores multiple values in a single variable. Arrays in JavaScript are zero-indexed, meaning the first element is at position 0. Syntax array.unshift(element1, element2, ..., elementN) Parameters The unshift() method accepts one or more parameters representing the elements to add at the beginning of the array. ...

Read More

Diagonal product of a matrix - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

Suppose, we have a 2-D array representing a square matrix like this − const arr = [ [1, 3, 4, 2], [4, 5, 3, 5], [5, 2, 6, 4], [8, 2, 9, 3] ]; We are required to write a function that takes in this array and returns the product of the elements present at the principal diagonal of the matrix. Principal Diagonal Elements The principal diagonal consists of elements where the row index equals the column index (i === j). For ...

Read More

Removing an element from the start of the array in javascript

Sai Teja Kotha
Sai Teja Kotha
Updated on 15-Mar-2026 230 Views

In JavaScript, there are several methods to remove the first element from an array. The most common approaches are using the shift() method, slice() method, or the Underscore.js _.rest() method. Using shift() Method (Recommended) The shift() method removes and returns the first element from an array, modifying the original array. Syntax array.shift() Example Remove first element using shift() ...

Read More

Removing an element from a given position of the array in Javascript

Sai Teja Kotha
Sai Teja Kotha
Updated on 15-Mar-2026 377 Views

In JavaScript, you can remove an element from a specific position in an array using several methods. Each approach has different behaviors and use cases. Syntax Here are the main syntaxes for removing elements: // Delete operator (leaves undefined) delete arr[index]; // Slice method (creates new array) arr.slice(0, index).concat(arr.slice(index + 1)); // Splice method (modifies original array) arr.splice(index, 1); Using delete Operator The delete operator removes an element but leaves undefined in its place, keeping the array length unchanged. Remove element using delete ...

Read More

Searching an element in Javascript Array

Sharon Christine
Sharon Christine
Updated on 15-Mar-2026 613 Views

JavaScript provides several methods to search for elements in arrays. Each method has its own use case depending on whether you're searching for primitive values or complex objects. Using indexOf() for Primitive Values The indexOf() method searches through the array and returns the index of the first matching element, or -1 if not found. let people = ["Harry", "Martha", "John", "Sam"]; console.log(people.indexOf("John")); console.log(people.indexOf("Jim")); 2 -1 Using find() for Complex Objects The find() method returns the first element that matches the condition provided in the callback function. let people ...

Read More
Showing 3671–3680 of 6,519 articles
« Prev 1 366 367 368 369 370 652 Next »
Advertisements