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
Front End Technology Articles
Page 303 of 652
Random whole number between two integers JavaScript
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 MoreRecursive loop that prints an inverted count in JavaScript
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 MoreRemove number from array and shift the remaining ones JavaScript
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 MoreReturn the element that appears for second most number of times in the array JavaScript
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 MoreReversing array without changing the position of certain elements JavaScript
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 MoreReversing the words within keeping their order the same JavaScript
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 MoreRound number down to nearest power of 10 JavaScript
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 MoreSorting numbers according to the digit root JavaScript
In this problem statement, our task is to sort numbers according to the digit root and implement this problem with the help of Javascript functionalities. So we can solve this problem with the help of loops in Javascript. What is digit root? The digit root of a given number is basically the sum of its digits. Repeat the calculation until the result is not a single digit. Let's take an example for the digit root, calculate the digit root for 1234, 1 + 2 + 3 + 4 = 10 = 1 + 0 = 1. Similarly the ...
Read MoreSorting odd and even elements separately JavaScript
In JavaScript, we can sort odd and even positioned elements separately within an array while maintaining their relative positions. This approach sorts elements at even indices (0, 2, 4...) and odd indices (1, 3, 5...) independently. Understanding the Problem Given an array, we need to sort elements at even indices separately from elements at odd indices. For example, with array [9, 2, 7, 4, 5, 6, 3, 8, 1]: Even indices (0, 2, 4, 6, 8): [9, 7, 5, 3, 1] → sorted: [1, 3, 5, 7, 9] Odd indices (1, 3, 5, 7): [2, 4, ...
Read MoreSorting strings with decimal points in JavaScript
In JavaScript, sorting strings with decimal points requires converting them to numbers first, since string comparison would treat "10.0" as less than "2.0". This article demonstrates how to sort decimal string arrays properly. Understanding the Problem When sorting strings containing decimal numbers, JavaScript's default string comparison doesn't work numerically. For example, ['3.3', '4.4', '2.3', '1.2'] should become ['1.2', '2.3', '3.3', '4.4'], but string sorting would give incorrect results. The Solution Approach To solve this problem, we need to: Convert decimal strings to numbers using parseFloat() Sort the numbers numerically Convert back to strings if needed ...
Read More