Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Front End Technology Articles
Page 368 of 652
Swap kth element of array - JavaScript
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 MoreSquared sum of n odd numbers - JavaScript
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 MoreArrays Data Structure in Javascript
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 MoreAdding an element at the end of the array in Javascript
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 MoreChange string based on a condition - JavaScript
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 MoreAdding an element at the start of the array in Javascript
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 MoreDiagonal product of a matrix - JavaScript
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 MoreRemoving an element from the start of the array in javascript
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 MoreRemoving an element from a given position of the array in Javascript
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 MoreSearching an element in Javascript Array
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