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
Javascript Articles
Page 67 of 534
Negative number digit sum in JavaScript
We are required to write a JavaScript function that takes in a negative integer and returns the sum of its digits. Example Input For example: If the number is: -5456 Expected Output Then the output should be: 5+4+5+6 = 20 Using String Manipulation and Reduce The following approach converts the number to string, splits it into digits, and uses reduce to sum them: const num = -5456; const sumNum = num => { return String(num).split("").reduce((acc, val, ind) => { ...
Read MoreOmitting false values while constructing string in JavaScript
We have an array that contains some string values as well as some false values like null, undefined, 0, and empty strings. We need to write a JavaScript function that takes this array and returns a string constructed by joining values while omitting all falsy values. Understanding Falsy Values In JavaScript, falsy values include: false, 0, "", null, undefined, and NaN. These evaluate to false in boolean contexts. Method 1: Using reduce() with Logical OR The logical OR operator (||) returns the right operand when the left is falsy: const arr = ["Here", ...
Read MoreFinding two golden numbers in JavaScript
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 MoreSwapping adjacent words of a String in JavaScript
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 MoreChanging positivity/negativity of Numbers in a list in JavaScript
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 MoreProduct of numbers present in a nested array in JavaScript
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 MoreFinding sum of every nth element of array in JavaScript
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 MoreSmart concatenation of strings in JavaScript
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 MorePicking all the numbers present in a string in JavaScript
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 MoreGreater possible digit difference of a number in JavaScript
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