Web Development Articles

Page 465 of 801

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

How to remove certain number elements from an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 548 Views

We are required to write a function that takes in an array of numbers and a number, and it should remove all the occurrences of that number from the array in-place. Let's explore different approaches to accomplish this task effectively. Method 1: Using Recursion with splice() We can use recursion to remove elements by finding and splicing them one by one. The recursive function continues until no more occurrences are found. const numbers = [1, 2, 0, 3, 0, 4, 0, 5]; const removeElement = (arr, element) => { if(arr.indexOf(element) !== ...

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

Get the index of the nth item of a type in a JavaScript array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 752 Views

When working with JavaScript arrays, you might need to find the index of the nth occurrence of a specific value. This is useful for parsing data, finding patterns, or processing structured arrays. Problem Statement We need to write a function getIndex() that takes three parameters: an array arr, a value txt (string or number), and a number n. The function should return the index of the nth appearance of txt in arr. If txt doesn't appear n times, return -1. Using Array.reduce() Method The reduce() method provides an elegant solution by maintaining a counter and tracking ...

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

Sort a JavaScript array so the NaN values always end up at the bottom.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 661 Views

We have an array that contains String and number mixed data types, we have to write a sorting function that sorts the array so that the NaN values always end up at the bottom. The array should contain all the normal numbers first followed by string literals and then followed by NaN numbers. We know that the data type of NaN is "number", so we can't check for NaN like !number && !string. Moreover, if we simply check the tautology and falsity of elements then empty strings will also satisfy the same condition which NaN or undefined satisfies. ...

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

How to Replace null with "-" JavaScript

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

In JavaScript, you often need to replace null, undefined, empty strings, and other falsy values with a default placeholder like "-". This is common when displaying data in tables or forms where empty values should show a dash instead of being blank. Understanding Falsy Values JavaScript considers these values as falsy: null, undefined, '' (empty string), 0, NaN, and false. We can use this to our advantage when replacing them. Method 1: Using Object.keys() and forEach() This approach iterates through all object keys and replaces falsy values in place: const obj = { ...

Read More

Generate colors between #CCCCCC and #3B5998 for a color meter with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 355 Views

We have to write a function that generates a random color between two given colors. Let's tackle this problem in parts − First → We write a function that generates a random number between two given numbers. Second → Instead of using the hex scale for random color generation, we will map the hex to 0 to 15 decimal scale and use that instead. ...

Read More

Recursively loop through an array and return number of items with JavaScript?

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

We need to write a function that recursively searches through a nested array and counts how many times a specific value appears. This is useful when working with complex nested data structures. Problem Statement Given a nested array, we want to count all occurrences of a search term, including items in nested sub-arrays. const names = ["rakesh", ["kalicharan", "krishna", "rakesh", "james", ["michael", "nathan", "rakesh", "george"]]]; For the above array, searching for "rakesh" should return 3 because it appears once at the top level and twice in nested arrays. Recursive Solution Here's a ...

Read More
Showing 4641–4650 of 8,010 articles
« Prev 1 463 464 465 466 467 801 Next »
Advertisements