Web Development Articles

Page 166 of 801

Finding two golden numbers in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 204 Views

We are required to write a JavaScript function that takes in two numbers representing a sum and product, and returns two numbers whose sum equals the first parameter and product equals the second parameter. If no such numbers exist, the function should return false. Problem Understanding Given sum s and product p, we need to find two numbers x and y such that: x + y = s x × y = p Mathematical Approach This is essentially solving a quadratic equation. If x + y = s and x × y = p, ...

Read More

Swapping adjacent words of a String in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 556 Views

In JavaScript, swapping adjacent words in a string means exchanging pairs of words - the first word with the second, the third with the fourth, and so on. If there's an odd number of words, the last word remains in its position. Example Here's how to swap adjacent words using the reduce method: const str = "This is a sample string only"; const replaceWords = str => { return str.split(" ").reduce((acc, val, ind, arr) => { if(ind % 2 === 1){ ...

Read More

Changing positivity/negativity of Numbers in a list in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 106 Views

We are required to write a JavaScript function that takes in an array of positive as well as negative Numbers and changes the positive numbers to corresponding negative numbers and the negative numbers to corresponding positive numbers in place. Example The code for this will be − const arr = [12, 5, 3, -1, 54, -43, -2, 34, -1, 4, -4]; const changeSign = arr => { arr.forEach((el, ind) => { arr[ind] *= -1; }); }; changeSign(arr); console.log(arr); ...

Read More

Product of numbers present in a nested array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 304 Views

We are required to write a JavaScript function that takes in an array of nested arrays of Numbers and some falsy values (including 0) and some strings as well and the function should return the product of number values present in the nested array. If the array contains some 0s, we should ignore them as well. Example The code for this will be − const arr = [ 1, 2, null, [ 2, 5, null, undefined, false, 5, [ ...

Read More

Finding sum of every nth element of array in JavaScript

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

We are required to write a JavaScript function that takes in an array of numbers and returns the cumulative sum of every number present at the index that is a multiple of n from the array. Understanding the Problem When we say "every nth element", we mean elements at indices 0, n, 2n, 3n, and so on. For example, if n=3, we sum elements at indices 0, 3, 6, 9, etc. Example Let's find the sum of every 3rd element from an array: const arr = [5, 3, 5, 6, 12, 5, 65, 3, ...

Read More

Smart concatenation of strings in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 222 Views

We need to write a JavaScript function that concatenates two strings with a smart approach. If the last character of the first string matches the first character of the second string, we omit one of those duplicate characters to avoid redundancy. Problem Statement When concatenating strings like "Food" and "dog", a simple join would give "Fooddog". However, since both strings share the character 'd' at the junction, smart concatenation removes the duplicate to produce "Foodog". Example Here's how to implement smart string concatenation: const str1 = 'Food'; const str2 = 'dog'; const concatenateStrings ...

Read More

Picking all the numbers present in a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 180 Views

We are required to write a JavaScript function that takes in a string that contains some one-digit numbers in between and the function should return the sum of all the numbers present in the string. Example The code for this will be − const str = 'uyyudfgdfgf5jgdfj3hbj4hbj3jbb4bbjj3jb5bjjb5bj3'; const sumNum = str => { const strArr = str.split(""); let res = 0; for(let i = 0; i < strArr.length; i++){ if(+strArr[i]){ ...

Read More

Greater possible digit difference of a number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 234 Views

We are required to write a JavaScript function that takes in a number. Then the function should return the greatest difference that exists between any two digits of the number. In other words, the function should simply return the difference between the greatest and the smallest digit present in it. Example Problem If the number is 654646, Then the smallest digit here is 4 and the greatest is 6 Hence, our output should be 2 Using Recursive Approach The recursive solution extracts digits one by one and keeps track of minimum and maximum ...

Read More

Checking for special type of Arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 212 Views

We are required to write a JavaScript function that takes in an array of literals and checks if elements are the same or not if read from front or back. Such arrays are also known by the name of palindrome arrays. Some examples of palindrome arrays are: const arr1 = ['a', 'b', 'c', 'b', 'a']; const arr2 = [4, 7, 7, 4]; const arr3 = [7, 7, 7, 7, 7, 7]; Method 1: Using a For Loop This approach compares elements from both ends moving towards the center: const arr = [1, ...

Read More

Finding count of special characters in a string in JavaScript

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

In JavaScript, you can count special characters in a string by checking each character against a predefined set of special characters. This is useful for text validation, content analysis, or data processing tasks. Problem Statement We need to count occurrences of these special characters in a string: '!', ', ', ''', ';', '"', '.', '-', '?' The function should iterate through each character and increment a counter when it finds a match. Example Implementation const str = "This, is a-sentence;.Is this a sentence?"; const countSpecial = str => { ...

Read More
Showing 1651–1660 of 8,010 articles
« Prev 1 164 165 166 167 168 801 Next »
Advertisements