Javascript Articles

Page 245 of 534

Nth element of the Fibonacci series JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 1K+ Views

In this article, we'll learn how to find the nth element in the Fibonacci series using JavaScript. The Fibonacci sequence is a mathematical series where each number is the sum of the two preceding numbers. What is the Fibonacci Series? The Fibonacci sequence starts with 0 and 1, and each subsequent number is the sum of the previous two numbers. The series begins as: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89... For example, the 6th number in the series is 8, and the 10th number is 55. Using ...

Read More

Numbers smaller than the current number JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 636 Views

In JavaScript, we often need to count how many numbers in an array are smaller than each element. This problem requires comparing each element against all others to build a result array showing the count of smaller numbers for each position. Understanding the Problem Given an array of integers, we need to return a new array of the same length where each element represents the count of numbers smaller than the corresponding element in the original array. For example: if we have [5, 4, 3, 2, 1], the output should be [4, 3, 2, 1, 0] because: ...

Read More

Queue Reconstruction by Height in JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 286 Views

The Queue Reconstruction by Height problem involves arranging people in a queue based on their height and the number of taller people in front of them. Each person is represented as [h, k] where h is height and k is the count of people in front with height ≥ h. Understanding the Problem Given an array of people where each person is represented by [height, k], we need to reconstruct the queue. The key insight is that taller people don't affect the positioning of shorter people, so we can process people from tallest to shortest. For example, ...

Read More

Random whole number between two integers JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 292 Views

In JavaScript, generating a random whole number between two integers is a common task that requires combining Math.random() with Math.floor() to ensure we get integers within the specified range. Understanding the Problem We need to create a function that returns a random integer between two given numbers (inclusive). For example, if we want a random number between 1 and 5, the possible outputs are: 1, 2, 3, 4, or 5. The Math.random() Method Math.random() generates a random decimal number between 0 (inclusive) and 1 (exclusive). We need to transform this to get integers in our desired ...

Read More

Recursive loop that prints an inverted count in JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 483 Views

In JavaScript, we can create a recursive function to print numbers in descending order from a given number down to zero. This demonstrates how recursion can solve problems by breaking them into smaller subproblems. What is Recursion? Recursion is a programming technique where a function calls itself to solve smaller instances of the same problem. Each recursive call works on a reduced version of the original problem until reaching a base case that stops the recursion. Classic examples include calculating factorials, Fibonacci sequences, and tree traversals. Problem Understanding Our goal is to create a function that ...

Read More

Remove number from array and shift the remaining ones JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 224 Views

In this problem statement, our task is to write a function to remove numbers from an array and shift the remaining elements using JavaScript. We'll explore multiple approaches including the filter method, for loops, and in-place modification to achieve this functionality. Understanding the Problem We need to create a function that removes all occurrences of a given number from an array and shifts the remaining items to fill the gaps. For example, given array [1, 2, 3, 4, 2, 5] and number 2, the result would be [1, 3, 4, 5] with all instances of 2 removed and ...

Read More

Return the element that appears for second most number of times in the array JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 441 Views

In JavaScript, finding the element that appears for the second most number of times in an array requires counting frequencies and identifying the second highest count. We'll use a frequency map and sorting to solve this problem efficiently. Understanding the Problem Given an array of elements, we need to find the element with the second highest frequency. For example, in the array [1, 3, 3, 4, 4, 4], the element 4 appears 3 times (most frequent), and 3 appears 2 times (second most frequent), so we return 3. Algorithm Steps Step 1: Create a frequency map ...

Read More

Reversing array without changing the position of certain elements JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 649 Views

In this problem, we need to reverse an array while keeping certain elements in their original positions. This requires tracking preserved element positions and carefully swapping only the non-preserved elements. Understanding the Problem The goal is to reverse an array but preserve specific elements at their original positions. For example, if we have array [1, 2, 3, 4, 5, 6] and want to preserve elements [2, 4, 6], the result should be [5, 2, 3, 4, 1, 6] instead of a complete reversal. Algorithm Approach The solution involves: Track indices of elements to preserve Create ...

Read More

Reversing the words within keeping their order the same JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 1K+ Views

In JavaScript, we can reverse each word in a string while maintaining their original positions. This means "Hello World" becomes "olleH dlroW" - each word is reversed individually, but their order remains unchanged. Understanding the Problem We need a function that takes a string and returns a new string where each word is reversed, but the word positions stay the same. For example: Input: "Hello World" Output: "olleH dlroW" The word order is preserved, but each word's characters are reversed. Method 1: Using Built-in Methods (Recommended) The most efficient approach uses JavaScript's ...

Read More

Round number down to nearest power of 10 JavaScript

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 15-Mar-2026 1K+ Views

In JavaScript, rounding down a number to the nearest power of 10 involves finding the highest power of 10 that is less than or equal to the given number. For example, 1365 rounds down to 1000, and 987 rounds down to 100. Understanding the Problem The task is to create a function that rounds down any given number to the nearest power of 10. Powers of 10 are numbers like 1, 10, 100, 1000, etc. For a number like 1365, the nearest lower power of 10 is 1000, while for 87, it would be 10. Algorithm ...

Read More
Showing 2441–2450 of 5,340 articles
« Prev 1 243 244 245 246 247 534 Next »
Advertisements