Javascript Articles

Page 163 of 534

Turn each character into its ASCII character code and join them together to create a number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 423 Views

Problem We need to write a JavaScript function that takes a string and converts each character to its ASCII code, joins them to create a number, replaces all instances of 7 with 1, and returns the difference between the original and modified numbers. Understanding the Process The algorithm involves several steps: Convert each character to its ASCII code using charCodeAt() Join all ASCII codes to form one large number Replace all occurrences of digit 7 with 1 Calculate the difference between original and modified numbers ...

Read More

Unique pairs in array that forms palindrome words in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 199 Views

We are required to write a JavaScript function that takes in an array of unique words. Our function should return an array of all such index pairs, the words at which, when combined yield a palindrome word. Problem Given an array of unique words, find all pairs of indices where concatenating the words at those indices creates a palindrome. A palindrome reads the same forwards and backwards, like "racecar" or "abccba". Example Following is the code: const arr = ["abcd", "dcba", "lls", "s", "sssll"]; const findPairs = (arr = []) => { ...

Read More

Sorting according to number of 1s in binary representation using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 217 Views

Problem We need to write a JavaScript function that takes an array of numbers and sorts them in descending order based on the count of 1s in their binary representation. Understanding Binary Representation Every number has a binary representation. For example: 5 → 101 (two 1s) 78 → 1001110 (four 1s) 11 → 1011 (three 1s) Solution Approach We'll create two functions: one to count 1s in binary representation and another to sort the array. const arr = [5, 78, 11, 128, 124, 68, 6]; const countOnes = (num) => ...

Read More

Moving all vowels to the end of string using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 393 Views

In JavaScript, you can move all vowels to the end of a string while maintaining the relative positions of consonants. This is useful for text processing and string manipulation tasks. Problem We need to write a JavaScript function that takes a string and constructs a new string where all consonants maintain their relative positions and all vowels are moved to the end. Approach The solution involves iterating through the string, separating vowels and consonants into different variables, then concatenating them with consonants first and vowels at the end. Example const str = 'sample ...

Read More

Finding number of open water taps after n chances using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 152 Views

Problem Suppose a school organises this game on their Annual Day celebration − There are "n" water taps and "n" students are chosen at random. The instructor asks the first student to go to every tap and open it. Then he has the second student go to every second tap and close it. The third goes to every third tap and, if it is closed, he opens it, and if it is open, he closes it. The fourth student does this to every fourth tap, and so on. After the process is completed with the "n"th student, how many taps ...

Read More

Sum of individual even and odd digits in a string number using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 649 Views

We are required to write a JavaScript function that takes in a string containing digits and our function should return true if the sum of even digits is greater than that of odd digits, false otherwise. Problem Statement Given a string of digits, we need to: Separate even and odd digits Calculate the sum of even digits Calculate the sum of odd digits Compare which sum is greater Example Let's implement a solution that processes each digit and compares the sums: ...

Read More

Finding the sum of all numbers in the nth row of an increasing triangle using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 291 Views

For the purpose of this problem, an increasing triangle is a structure where numbers are arranged in rows, with each row containing one more number than the previous row, and all numbers are consecutive starting from 1. Understanding the Triangle Structure The increasing triangle looks like this: 1 2 3 4 5 6 7 8 9 10 Each row n contains n consecutive numbers. Row 1 has 1 number, row 2 has 2 numbers, row 3 has 3 numbers, and so on. Problem Statement We need ...

Read More

Reversing the bits of a decimal number and returning new decimal number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 322 Views

We are required to write a JavaScript function that takes in a decimal number, converts it into binary and reverses its bits (1 to 0 and 0 to 1) and returns the decimal equivalent of the new binary thus formed. Problem Given a decimal number, we need to: Convert the decimal number to binary Flip each bit (0 becomes 1, 1 becomes 0) Convert the resulting binary back to decimal Example Let's implement the function to reverse bits and convert back to decimal: ...

Read More

Removing least number of elements to convert array into increasing sequence using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 196 Views

We need to write a JavaScript function that removes the minimum number of elements from an array to make it strictly increasing. This is essentially finding the Longest Increasing Subsequence (LIS) and removing all other elements. Problem Analysis The goal is to find the longest possible increasing subsequence and remove the fewest elements. A strictly increasing sequence means each element is greater than the previous one. Example with Corrected Algorithm The original approach has flaws - it doesn't find the optimal solution. Here's a correct implementation using dynamic programming: const arr = [1, 100, ...

Read More

Finding all possible prime pairs that sum upto input number using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 606 Views

Problem We need to write a JavaScript function that takes a number n and returns an array of all prime number pairs that sum to n. Both numbers in each pair must be prime numbers. Understanding Prime Numbers A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. For example: 2, 3, 5, 7, 11, 13, 17, 19, 23. Algorithm Approach The solution involves three main steps: Create a function to check if a number is prime Generate all possible prime numbers up to ...

Read More
Showing 1621–1630 of 5,340 articles
« Prev 1 161 162 163 164 165 534 Next »
Advertisements