Javascript Articles

Page 71 of 534

Constructing an object from repetitive numeral string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 168 Views

Suppose, we have a string with digits like this − const str = '11222233344444445666'; We are required to write a JavaScript function that takes in this string and returns an object that represents the count of each number in the string. So, for this string, the output should be − const output = { "1": 2, "2": 4, "3": 3, "4": 7, "5": 1, "6": 3 }; Using a for Loop The most straightforward approach is to iterate ...

Read More

Finding upper elements in array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 177 Views

We are required to write a JavaScript function that takes in an array of numbers as the first argument and a single number as the second argument. The function should return an array of all the elements from the input array that are greater than or equal to the number taken as the second argument. Therefore, let's write the code for this function — Using for Loop The most straightforward approach uses a for loop to iterate through the array and check each element: const arr = [56, 34, 2, 7, 76, 4, 45, 3, ...

Read More

Inverting slashes in a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 404 Views

When working with strings in JavaScript, you may need to replace forward slashes (/) with backslashes (\) or vice versa. This is common when dealing with file paths or URL manipulation. This article demonstrates how to create a function that takes a string containing forward slashes and converts them to backslashes. Problem Statement We need to write a JavaScript function that: Takes a string that may contain forward slashes (/) Returns a new string with all forward slashes replaced by backslashes (\) Method 1: Using a for Loop Here's a solution using a ...

Read More

Picking out uniques from an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 169 Views

Suppose we have an array that contains duplicate elements like this − const arr = [1, 1, 2, 2, 3, 4, 4, 5]; We are required to write a JavaScript function that takes in one such array and returns a new array. The array should only contain the elements that only appear once in the original array. Therefore, let's write the code for this function − Method 1: Using indexOf() and lastIndexOf() This approach compares the first and last occurrence of each element. If they're different, the element appears multiple times: ...

Read More

Merging two arrays in a unique way in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 181 Views

We are required to write a JavaScript function that takes in two arrays and merges the arrays taking elements alternatively from the arrays. For example If the two arrays are − const arr1 = [4, 3, 2, 5, 6, 8, 9]; const arr2 = [2, 1, 6, 8, 9, 4, 3]; Then the output should be − [4, 2, 3, 1, 2, 6, 5, 8, 6, 9, 8, 4, 9, 3] Using Index-Based Approach The code for this will be − const arr1 = [4, 3, 2, ...

Read More

Adding up identical elements in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 138 Views

We are required to write a JavaScript function that takes in an array of numbers and sums all the identical numbers together at one index. Problem Overview When we have an array with duplicate values, we want to combine them into a single occurrence with their sum. The first occurrence of each unique number will contain the total sum of all occurrences. Example Input and Output If the input array is: const arr = [20, 10, 15, 20, 15, 10]; Then the output should be: const output = [40, 20, ...

Read More

Squared concatenation of a Number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 291 Views

We are required to write a JavaScript function that takes in a number and returns a new number in which all the digits of the original number are squared and concatenated. For example: If the number is − 99 Then the output should be − 8181 because 9² is 81 and 9² is 81, so concatenating them gives 8181. Example Let's implement this function step by step: const num = 9119; const squared = num => { const numStr = String(num); ...

Read More

Finding the smallest fitting number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 182 Views

We are required to write a JavaScript function that takes in an array of numbers and returns a number which can exactly divide all the numbers in the array. This is essentially finding the Greatest Common Divisor (GCD) of all numbers. Therefore, let's write the code for this function − Example The code for this will be − const arr = [4, 6, 34, 76, 78, 44, 34, 26, 88, 76, 42]; const dividesAll = el => { const result = []; let num; ...

Read More

Mapping string to Numerals in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 859 Views

We are required to write a JavaScript function that takes in a string. It should print out each number for every corresponding letter in the string. Letter to Number Mapping Each letter corresponds to its position in the alphabet: a = 1 b = 2 c = 3 d = 4 e = 5 ... y = 25 z = 26 Note: The function should remove any special characters and spaces, processing only alphabetic characters. Example Input and Output If the input is: "hello man" Then the output ...

Read More

Writing table of number in array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 167 Views

We are required to write a JavaScript function that takes in two numbers, say m and n, and it returns an array of first n multiples of m. Therefore, let's write the code for this function − Example The code for this will be − const num1 = 4; const num2 = 6; const multiples = (num1, num2) => { const res = []; for(let i = num1; i { return Array.from({length: count}, (_, index) => base * (index + 1)); }; console.log(generateTable(7, 5)); console.log(generateTable(3, 8)); [ 7, 14, 21, 28, 35 ] [ 3, 6, 9, 12, 15, 18, 21, 24 ] Using a Simple For Loop Here's another straightforward approach using a basic for loop: function createMultiplicationTable(number, count) { const table = []; for (let i = 1; i

Read More
Showing 701–710 of 5,340 articles
« Prev 1 69 70 71 72 73 534 Next »
Advertisements