Javascript Articles

Page 459 of 534

Solve the Sherlock and Array problem in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 734 Views

Watson gives Sherlock an array A of length N. Then he asks him to determine if there exists an element in the array such that the sum of the elements on its left is equal to the sum of the elements on its right. We have to write this function, it should take in an array of Numbers, and any such number exists in the array, it should return its index, otherwise it should return -1. So, let's write the code for this function. Algorithm Approach The efficient approach is to: Calculate the total sum of ...

Read More

How to force JavaScript to do math instead of putting two strings together?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 470 Views

JavaScript often concatenates when you expect addition because the + operator works differently with strings and numbers. Here are several methods to force mathematical addition instead of string concatenation. The Problem When JavaScript encounters the + operator with strings, it performs concatenation instead of addition: console.log("3" + "5"); // "35" (concatenation) console.log(3 + 5); // 8 (addition) console.log("3" + 5); // "35" (string wins, concatenation) 35 8 35 Using the Unary Plus Operator ...

Read More

Find unique and biggest string values from an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 277 Views

In JavaScript, we often need to filter arrays to get unique values and sort them by specific criteria. This article demonstrates how to find the longest unique string values from an array of objects. Problem Statement Given an array of objects with text properties, we need to create a function that returns n objects with the longest unique string values. If fewer than n unique objects exist, return all unique objects. const arr = [ {text: 'use'}, {text: 'secur'}, {text: 'form'}, ...

Read More

Check whether a number is a Fibonacci number or not JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 509 Views

We are required to write a JavaScript function that takes in a number and returns a boolean based on the fact whether or not it comes in the fibonacci series. For example − If the function call is like this − fibonacci(12); fibonacci(89); fibonacci(55); fibonacci(534); Then the output should be − false true true false What is the Fibonacci Series? The Fibonacci series is a sequence where each number is the sum of the two preceding ones: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

Read More

Finding next n leap years in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 621 Views

We are required to write a function that takes a positive integer n and returns an array of next n leap years. We will break this problem into three parts: Part 1: Finding Current Year via JavaScript The code to find current year via JavaScript will be: // getting the current year from a new instance of Date object const year = new Date().getFullYear(); console.log("Current year:", year); Current year: 2024 Part 2: Checking for Leap Year We will now write a function isLeap() that takes in a number and returns ...

Read More

Reverse sum array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 516 Views

We are required to write a function, say reverseSum() that takes in two arrays of Numbers, let's say first and second and returns a new array that contains: Sum of first element of first array and last element of second array as first element, sum of second element of first array and second last element of second array, and so on. When any of the array runs out of element before the other, we simply push all the remaining elements to the array. Syntax const reverseSum = ...

Read More

Solution to the clumsy factorial problem in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 415 Views

Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n. For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order. For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + ...

Read More

Reverse index value sum of array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 273 Views

Suppose we have an array of numbers like this: const arr = [3, 6, 7, 3, 1, 4, 4, 3, 6, 7]; This array contains 10 elements, so the index of the last element is 9. We need to write a function that calculates the sum of each element multiplied by its reverse index position. The reverse index means we multiply each element by (array.length - currentIndex - 1). For our example: // Element at index 0: 3 * (10-0-1) = 3 * 9 = 27 // Element at index 1: 6 * ...

Read More

Searching objects by value in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 235 Views

When working with complex JavaScript objects, you often need to find all keys that contain a specific value. This is particularly useful for nested objects where the same value might appear at multiple levels. Consider this example object with nested structure: const obj = { "name": "Vivek Sharma", "occupation": "Software Engineer", "age": 23, "contacts": [{ "name": "Mukul Sharma", "occupation": "Software Engineer", ...

Read More

JavaScript array: Find all elements that appear more than n times

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 441 Views

In JavaScript, you can find all elements that appear more than n times in an array using various approaches. This is useful for data analysis, finding duplicates, or filtering frequent items. Problem Overview Given an array of numbers or strings with repeated entries, we need to write a function that takes a positive integer n and returns all elements that appear more than or equal to n times. Using Map() for Frequency Counting The most efficient approach uses a Map to track element frequencies, then filters elements that meet our criteria: const arr = ...

Read More
Showing 4581–4590 of 5,340 articles
« Prev 1 457 458 459 460 461 534 Next »
Advertisements