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
Javascript Articles
Page 85 of 534
Returning the expanded form of a number in JavaScript
The expanded form of a number breaks down each digit into its place value. For example, 1234 becomes "1000 + 200 + 30 + 4". This is useful for teaching place value concepts and number decomposition. Problem We need to write a JavaScript function that takes a number and returns a string showing the expanded form, indicating the place value of each non-zero digit. Example const num = 56577; const expandedForm = (num = 0) => { const str = String(num); let res = ''; ...
Read MoreCurrified function that multiples array elements in JavaScript
Problem We need to write a JavaScript function that takes an array and returns another function. This returned function takes a number and produces a new array where each element is the product of the corresponding element from the original array and the number. What is a Curried Function? A curried function breaks down a function that takes multiple arguments into a series of functions that each take a single argument. In this case, instead of multiply(array, number), we have multiply(array)(number). Example Following is the code − const arr = [2, 5, 2, 7, 8, 4]; ...
Read MoreReturning number with increasing digits. in JavaScript
We are required to write a JavaScript function that takes in a number n. Our function should return it with its digits in descending order. Essentially, we should rearrange the digits to create the highest possible number. Problem Given a number, we need to arrange its digits in descending order to form the largest possible number from those digits. Solution The approach is to convert the number to a string, split it into individual digits, sort them in descending order, and then convert back to a number. Example const num = 5423267; ...
Read MoreDeep count of elements of an array using JavaScript
We are required to write a JavaScript function that takes in a nested array of elements and returns the deep count of all elements present in the array, including those in nested subarrays. Problem Given a nested array, we need to count all elements at every level of nesting. Input const arr = [1, 2, [3, 4, [5]]]; Output const output = 7; Because the elements at level 1 are 2, elements at level 2 are 2 and elements at level 3 are 1. Hence the deep count is 7. Using Recursive Reduce Method ...
Read MoreMathematics summation function in JavaScript
Problem We are required to write a JavaScript function that takes in a number n. Our function should return the sum of all the natural numbers from 1 to n including both 1 and n. Example Following is the code − const num = 34; const summation = (num = 1) => { let res = 0; for(let i = 1; i { return (num * (num + 1)) / 2; }; console.log(summationFormula(34)); // Same result console.log(summationFormula(100)); // Testing with larger number ...
Read MoreHours and minutes from number of seconds using JavaScript
We are required to write a JavaScript function that takes in the number of seconds and returns the number of hours and minutes contained in those seconds. Problem Statement Given a number of seconds, we need to convert it into a readable format showing hours and minutes. Input: 3601 seconds Expected Output: "1 hour(s) and 0 minute(s)" Solution Here's how we can convert seconds to hours and minutes: const seconds = 3601; const toTime = (seconds = 60) => { const hR = 3600; // 1 hour ...
Read MoreSeparating data type from array into groups in JavaScript
In JavaScript, you often need to group array elements by their data types. This is useful for data analysis, validation, or organizing mixed-type arrays. We'll create a function that separates elements into groups based on their typeof result. Problem We need to write a JavaScript function that takes an array of mixed data types and returns an object where each key represents a data type and its value is an array containing all elements of that type. Solution Here's how to group array elements by their data types: const arr = [1, 'a', [], ...
Read MoreEven index sum in JavaScript
We need to write a JavaScript function that calculates the sum of all integers at even indices (0, 2, 4, etc.) in an array, then multiplies this sum by the last element of the array. Problem Statement Given an array of integers, find the sum of elements at even indices and multiply by the last element. Input: [4, 1, 6, 8, 3, 9] Even indices: 0, 2, 4 → elements: 4, 6, 3 Sum: 4 + 6 + 3 = 13 Last element: 9 Result: 13 × 9 = 117 Solution const ...
Read MoreSum of all positives present in an array in JavaScript
We are required to write a JavaScript function that takes in an array of numbers (positive and negative). Our function should calculate and return the sum of all the positive numbers present in the array. Method 1: Using reduce() with Helper Function This approach uses a helper function to check if a number is positive and reduce() to accumulate the sum: const arr = [5, -5, -3, -5, -7, -8, 1, 9]; const sumPositives = (arr = []) => { const isPositive = num => typeof num === 'number' && num ...
Read MoreBoolean Gates in JavaScript
We are required to write a JavaScript function that takes in an array of Boolean values and a logical operator. Our function should return a Boolean result based on sequentially applying the operator to the values in the array. Problem Given an array of Boolean values and a logical operator (AND, OR, XOR), we need to apply the operator sequentially to all values and return the final result. Solution Here's the implementation that handles three common Boolean operations: const array = [true, true, false]; const op = 'AND'; function logicalCalc(array, op) { ...
Read More