Javascript Articles

Page 24 of 534

Difference between numbers and string numbers present in an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 408 Views

ProblemWe are required to write a JavaScript function that takes in a mixed array of number and string representations of integers.Our function should add up okthe string integers and subtract this from the total of the non-string integers.ExampleFollowing is the code −const arr = [5, 2, '4', '7', '4', 2, 7, 9]; const integerDifference = (arr = []) => {    let res = 0;    for(let i = 0; i < arr.length; i++){       const el = arr[i];       if(typeof el === 'number'){          res += el;       }else if(typeof el === 'string' && +el){          res -= (+el);       };    };    return res; }; console.log(integerDifference(arr));OutputFollowing is the console output −10

Read More

Replacing vowels with their 1-based index in a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 652 Views

ProblemWe are required to write a JavaScript function that takes in a string and replaces all occurrences of the vowels in the string with their index in the string (1-based).It means if the second letter of the string is a vowel, it should be replaced by 2.ExampleFollowing is the code −const str = 'cancotainsomevowels'; const replaceVowels = (str = '') => {    const vowels = 'aeiou';    let res = '';    for(let i = 0; i < str.length; i++){       const el = str[i];       if(vowels.includes(el)){            res += (i ...

Read More

Retrieving n smallest numbers from an array in their original order in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 422 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers arr, and a number n.Our function should retrieve the n smallest from the array arr without disturbing their relative order. It means they should not be arranged in increasing or decreasing order rather they should hold their original order.ExampleFollowing is the code −const arr = [6, 3, 4, 1, 2]; const num = 3; const smallestInOrder = (arr = [], num) => {    if(arr.length < num){       return arr;    };    const copy = arr.slice();    copy.sort((a, b) => a - ...

Read More

Sum array of rational numbers and returning the result in simplest form in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 211 Views

ProblemWe are required to write a JavaScript function that takes in an array of exactly two subarrays with two numbers each.Both the subarrays represent a rational number in fractional form. Our function should add the rational numbers and return a new array of two numbers representing the simplest form of the added rational number.ExampleFollowing is the code −const arr = [    [1, 2],    [1, 3] ]; const findSum = (arr = []) => {    const hcf = (a, b) => b ? hcf(b, a % b) : a;    if(!arr.length){       return null;    }; ...

Read More

Decrypting source message from a code based on some algorithm in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 203 Views

ProblemWe are required to write a JavaScript function that takes in a decrypted message and returns its source message.All we know is the algorithm used to encrypt that message.And the algorithm is −Reverse the message string.Replace every letter with its ASCII code in quotes (A to '65', h to '104' and so on).Insert digits and spaces as is.ExampleFollowing is the code −const str = '12 hello world 30'; const decryptString = (str = '') => {    const alpha = 'abcdefghijklmnopqrstuvwxyz';    let res = '';    for(let i = str.length - 1; i >= 0; i--){       ...

Read More

Finding the sum of floors covered by an elevator in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 170 Views

ProblemWe are required to write a JavaScript function that takes in an array that represents the floor numbers at which a building lift stopped during an interval of time.From that data, our function should return the count of total number of floors covered by the lift in that time.ExampleFollowing is the code −const arr = [7, 1, 7, 1]; const floorsCovered = (arr = []) => {    let res = 0;    for (let i = 0; i < arr.length; i++){       if (arr[i] > arr[i+1]){          res += arr[i] - arr[i+1];     ...

Read More

Finding a number, when multiplied with input number yields input number in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 181 Views

ProblemWe are required to write a JavaScript function that takes in a positive integer n written as abcd... (a, b, c, d... being digits) and a positive integer pwe want to find a positive integer k, if it exists, such as the sum of the digits of n taken to the successive powers of p is equal to k * n.In other words −Is there an integer k such as : (a ^ p + b ^ (p+1) + c ^(p+2) + d ^ (p+3) + ...) = n * kIf it is the case, we will return k, if ...

Read More

Counting number of 9s encountered while counting up to n in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 433 Views

ProblemWe are required to write a JavaScript function that takes in a number n. Our function should count and return the number of times we will have to use 9 while counting from 0 to n.ExampleFollowing is the code −const num = 100; const countNine = (num = 0) => {    const countChar = (str = '', char = '') => {       return str       .split('')       .reduce((acc, val) => {          if(val === char){             acc++;          };          return acc;       }, 0);    };    let count = 0;    for(let i = 0; i

Read More

Adding and searching for words in custom Data Structure in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 224 Views

ProblemWe are required to design a data structure in JavaScript that supports the following two operations −addWord, which adds a word to that Data Structure (DS), we can take help of existing DS like arrays or any other DS to store this data, search, which searches a literal word or a regular expression string containing lowercase letters "a-z" or "." where "." can represent any letterFor exampleaddWord("sir") addWord("car") addWord("mad") search("hell") === false search(".ad") === true search("s..") === trueExampleFollowing is the code −class MyData{    constructor(){       this.arr = [];    }; }; MyData.prototype.addWord = function (word) {   ...

Read More

Longest possible string built from two strings in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 553 Views

ProblemWe are required to write a JavaScript function that takes in two strings s1 and s2 including only letters from ato z.Our function should return a new sorted string, the longest possible, containing distinct letters - each taken only once - coming from s1 or s2.ExampleFollowing is the code −const str1 = "xyaabbbccccdefww"; const str2 = "xxxxyyyyabklmopq"; const longestPossible = (str1 = '', str2 = '') => {    const combined = str1.concat(str2);    const lower = combined.toLowerCase();    const split =lower.split('');    const sorted = split.sort();    const res = [];    for(const el of sorted){       ...

Read More
Showing 231–240 of 5,339 articles
« Prev 1 22 23 24 25 26 534 Next »
Advertisements