Javascript Articles

Page 157 of 534

Creating all possible unique permutations of a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

We are required to write a JavaScript function that takes in a string str. Our function should create all permutations of the input string and remove duplicates, if present. This means, we have to shuffle all letters from the input in all possible orders. Problem When generating permutations of strings with repeated characters, we need to avoid duplicate permutations. For example, the string "aabb" should not generate identical permutations like "aabb" appearing multiple times. Algorithm Approach The solution uses recursion with duplicate checking: For each character position, try placing each unique character Skip duplicate ...

Read More

Replacing dots with dashes in a string using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 592 Views

We are required to write a JavaScript function that takes in a string and replaces all appearances of dots(.) in it with dashes(-). Input const str = 'this.is.an.example.string'; Expected Output const output = 'this-is-an-example-string'; All appearances of dots(.) in string str are replaced with dash(-) Method 1: Using replace() with Regular Expression The most efficient approach is using the built-in replace() method with a global regular expression: const str = 'this.is.an.example.string'; const replaceDots = (str = '') => { return str.replace(/\./g, '-'); }; ...

Read More

Converting humanYears into catYears and dogYears in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 361 Views

We need to write a JavaScript function that converts human age into equivalent cat and dog years. The conversion follows specific aging rules for the first two years, then a constant rate afterward. Problem Statement Create a function that takes human age in years and returns an array containing human years, cat years, and dog years. Input: const humanYears = 15; Expected Output: [15, 76, 89] Age Conversion Rules The aging calculation follows these rules: Year 1: Both cats and dogs age ...

Read More

Validating string with reference to array of words using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 327 Views

We need to write a JavaScript function that takes an array of valid words and a string, then checks if the string can be formed by concatenating one or more words from the array. Problem Statement Given an array of words and a target string, determine if the string can be constructed using words from the array. Words can be reused multiple times. Input: const arr = ['love', 'coding', 'i']; const str = 'ilovecoding'; Expected Output: true The string "ilovecoding" can be formed by concatenating "i" + "love" + "coding". ...

Read More

Counting divisors of a number using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 620 Views

We are required to write a JavaScript function that takes in a number and returns the count of its divisors. Problem Statement Input: const num = 30; Expected Output: const output = 8; Because the divisors of 30 are: 1, 2, 3, 5, 6, 10, 15, 30 Method 1: Simple Iteration Approach The straightforward approach is to iterate through all numbers from 1 to the given number and count those that divide evenly. const countDivisorsSimple = (num) => { ...

Read More

Numbers obtained during checking divisibility by 7 using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 708 Views

In mathematics, we can check if a number is divisible by 7 using a special algorithm. For any number of the form 10a + b (where a is all digits except the last, and b is the last digit), we calculate a - 2b. If this result is divisible by 7, then the original number is also divisible by 7. We repeat this process until we get a number with at most 2 digits, since it's easy to check divisibility by 7 for such small numbers. This article demonstrates how to implement this algorithm in JavaScript and count the ...

Read More

Returning lengthy words from a string using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 148 Views

We are required to write a JavaScript function that takes in a sentence of words and a number. The function should return an array of all words greater than the length specified by the number. Problem Statement Input: const str = 'this is an example of a basic sentence'; const num = 4; Expected Output: const output = [ 'example', 'basic', 'sentence' ]; Because these are the only three words with length greater than 4. Using for Loop The most straightforward approach is to split the string into ...

Read More

Finding the greatest and smallest number in a space separated string of numbers using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 348 Views

We are required to write a JavaScript function that takes in a string containing numbers separated by spaces and returns a string with only the greatest and smallest numbers separated by a space. Problem Statement Given a space-separated string of numbers, find the maximum and minimum values and return them as a formatted string. Input: '5 57 23 23 7 2 78 6' Expected Output: '78 2' Because 78 is the greatest and 2 is the smallest number in the string. Using Array.reduce() Method The most efficient approach uses ...

Read More

Preparing numbers from jumbled number names in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 203 Views

Problem Suppose we have the following jumbled number name string: const str = 'TOWNE'; If we rearrange this string, we can find two number names in it: 2 (TWO) and 1 (ONE). Therefore, we expect an output of 21. We need to write a JavaScript function that takes in one such string and returns the numbers present in the string arranged in ascending order. Approach The solution involves: Creating a mapping of number words to their numeric values Generating permutations to check if number words can ...

Read More

Displaying likes on a post wherein array specifies the names of people that liked a particular post using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 314 Views

We need to write a JavaScript function that takes an array of names representing people who liked a post. The function should format the output based on the number of likes: show all names for 3 or fewer likes, or show the first two names plus the remaining count for more than 3 likes. Problem Statement Create a function that displays likes in a user-friendly format: 0 likes: "no one likes this" 1 like: "John likes this" 2 likes: "John and Mary like this" 3 likes: "John, Mary and Bob like this" 4+ likes: "John, Mary and ...

Read More
Showing 1561–1570 of 5,340 articles
« Prev 1 155 156 157 158 159 534 Next »
Advertisements