AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 383 of 840

Returning the nth even number using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 659 Views

We are required to write a JavaScript function that takes in a number n and returns the nth even number in the sequence of natural numbers. Problem Given a positive integer n, find the nth even number. For example, the 1st even number is 2, the 2nd even number is 4, the 3rd even number is 6, and so on. Understanding the Pattern Even numbers follow a simple pattern: 2, 4, 6, 8, 10, 12... The nth even number can be calculated using the formula: n × 2. Example Implementation Here's how to implement ...

Read More

How to edit values of an object inside an array in a class - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 313 Views

In JavaScript, you can edit values of objects inside an array within a class by using the this keyword. This allows you to reference and modify properties of objects stored in class arrays. Understanding the Structure When working with objects inside arrays in classes, each object can contain its own methods that modify its properties. The key is understanding how this refers to the current object context. Example Here's how to create a class with an array of objects and modify their values: class Employee { constructor() { ...

Read More

Checking for special type of Arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 213 Views

We are required to write a JavaScript function that takes in an array of literals and checks if elements are the same or not if read from front or back. Such arrays are also known by the name of palindrome arrays. Some examples of palindrome arrays are: const arr1 = ['a', 'b', 'c', 'b', 'a']; const arr2 = [4, 7, 7, 4]; const arr3 = [7, 7, 7, 7, 7, 7]; Method 1: Using a For Loop This approach compares elements from both ends moving towards the center: const arr = [1, ...

Read More

Is the second string a rotated version of the first string JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 212 Views

We are required to write a JavaScript function that takes in two strings, say str1 and str2. We are required to determine whether or not the second string is a rotated version of the first string. For example − If the input strings are − const str1 = 'abcde'; const str2 = 'cdeab'; Then the output should be true because str2 is indeed made by shifting 'ab' to the end of string in str1. Understanding String Rotation A string rotation means taking some characters from the beginning of a string and moving them ...

Read More

Difference between first and the second array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 249 Views

When working with arrays in JavaScript, you often need to find elements that exist in one array but not in another. This operation is commonly known as finding the "difference" between two arrays. We'll create a function that takes two arrays and returns all elements from the first array that don't exist in the second array. Example Here's how to implement this functionality: const arr1 = ['1', '2', '3', '4/2', '5/4', '6-2']; const arr2 = ['1', '2', '3', '5/4', '4/2', '6-1', '7/2', '8-2']; const differenceBetween = (arr1 = [], arr2 = []) => { ...

Read More

Returning array of natural numbers between a range in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 238 Views

We need to write a JavaScript function that takes an array of two numbers [a, b] (where a ≤ b) and returns an array containing all natural numbers within that range, including the endpoints. Problem Given a range specified by two numbers, we want to generate all natural numbers between them. Natural numbers are positive integers starting from 1. Using a For Loop The most straightforward approach uses a for loop to iterate through the range and build the result array: const range = [6, 10]; const naturalBetweenRange = ([lower, upper] = [1, ...

Read More

Filtering out numerals from string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 225 Views

We are required to write a JavaScript function that takes in a string, str, which contains a combination of alphabets, special characters and numbers. Our function should return a new string based on the input string that contains only the numbers present in the string str, maintaining their relative order. For example, if the input to the function is: const str = 'revd1fdfdfs2v34fd5gfgfd6gffg7ds'; Then the output should be: '1234567' Using for Loop and Type Coercion This approach iterates through each character and uses the unary plus operator (+) to ...

Read More

Using the get in JavaScript to make getter function

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 285 Views

The get keyword in JavaScript creates a getter method that allows you to access object properties like regular properties while executing custom logic behind the scenes. Syntax const obj = { get propertyName() { // Custom logic return value; } }; Basic Example const studentDetails = { _name: "David Miller", get studentName() { ...

Read More

Finding the longest "uncommon" sequence in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 228 Views

We are required to write a JavaScript function that takes in an array of strings. The function should find the longest uncommon subsequence among the strings of the array. By longest uncommon subsequence we mean the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings. Our function should return the length of this longest uncommon subsequence. For example: If the input array is − const arr = ["aba", "cdc", "eae"]; Then the output should be 3. Understanding the Problem The key ...

Read More

Subarray pairs with equal sums in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 203 Views

We are required to write a JavaScript function that takes in an array of integers as the only argument. The function should determine whether there exists any way in which we can split the array into two subarrays such that the sum of the elements present in the two subarrays are equal. While dividing the elements into subarrays we have to make sure that no element from the original array is left. Problem Understanding For example, if the input array is: const arr = [5, 3, 7, 4, 1, 8, 2, 6]; Then ...

Read More
Showing 3821–3830 of 8,392 articles
« Prev 1 381 382 383 384 385 840 Next »
Advertisements