Javascript Articles - Page 144 of 534

Returning an array containing last n even numbers from input array in JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:43:44

209 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers as the first argument and a number as the second argument.Our function should pick and return an array of last n even numbers present in the input array.ExampleFollowing is the code − Live Democonst arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const num = 3; const pickEvens = (arr = [], num = 1) => {    const res = [];    for(let index = arr.length - 1; index >= 0; index -= 1){       if (res.length === num){          break;       };       const number = arr[index];       if (number % 2 === 0){          res.unshift(number);       };    };    return res; }; console.log(pickEvens(arr, num));Output[4, 6, 8]

Finding two numbers given their sum and Highest Common Factor using JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:43:25

264 Views

ProblemWe are required to write a JavaScript function that takes in two numbers. The first number represents the sum of two numbers and second represents their HCF (GCD or Greatest Common Divisor).Our function should find and return those two numbers.ExampleFollowing is the code − Live Democonst sum = 12; const gcd = 4; const findNumbers = (sum, gcd) => {    const res = [];    if (sum % gcd !== 0){       return -1;    }else{       res.push(gcd);       res.push(sum - gcd);       return res;    }; }; console.log(findNumbers(sum, gcd));Output[4, 8]

Finding array number that have no matching positive or negative number in the array using JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:36:22

194 Views

ProblemWe are required to write a JavaScript function that takes in an array of integers. For each number in the array there will also be its negative or positive compliment present in the array, but for exactly one number, there will be no compliment.Our function should find and return that number from the array.ExampleFollowing is the code − Live Democonst arr = [1, -1, 2, -2, 3]; const findOddNumber = (arr = []) => {    let count = 0;    let number = arr.reduce((total, num) => {       if (num >= 0)          count++       else          count--       return total + num;    }, 0)    return number / Math.abs(count); }; console.log(findOddNumber(arr));Output3

Repeating each character number of times their one based index in a string using JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:42:34

583 Views

ProblemWe are required to write a JavaScript function that takes in a string of english lowercase alphabets.Our function should construct a new string in which each character is repeated the number of times their 1-based index in the string in capital case and different character sets should be separated by dash ‘-’.Therefore, the string ‘abcd’ should become −"A-Bb-Ccc-Dddd"ExampleFollowing is the code − Live Democonst str = 'abcd'; const repeatStrings = (str) => {    const res = [];    for(let i = 0; i < str.length; i++){       const el = str[i];       let temp = el.repeat(i ... Read More

Mobile

Maximum absolute difference of the length of strings from two arrays in JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:33:33

305 Views

ProblemWe are required to write a JavaScript function that takes in two arrays, a1 and a2 of strings. Each string is composed with letters from a to z. Let x be any string in the first array and y be any string in the second array.Our function should find the value of −max(abs(length(x) − length(y)))ExampleFollowing is the code − Live Democonst arr1 = ["hoqq", "bbllkw", "oox", "ejjuyyy", "plmiis", "xxxzgpsssa", "xxwwkktt", "znnnnfqknaz", "qqquuhii", "dvvvwz"]; const arr2 = ["cccooommaaqqoxii", "gggqaffhhh", "tttoowwwmmww"]; const findMaxAbsDiff = (arr1 = [], arr2 = []) => {    if(arr1.length === 0 || arr2.length === 0){       ... Read More

Representing number as the power and product of primes in JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:32:41

255 Views

ProblemWe are required to write a JavaScript function that takes in a positive integer. Our function should represent this number as the sum of some powers of prime numbers.Therefore, for the number n, our function should return a string like this −n = "(p1**n1)(p2**n2)...(pk**nk)"Where p1, p2, p3..pk are prime numbers and n1, n2, ..nk are their non-negative powers and a ** b stands for a raised to the power b.ExampleFollowing is the code −const isPrime = num => {     for(let i = 2; i < num; i++){         if(num % i === 0){     ... Read More

Frequency of elements of one array that appear in another array using JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:31:56

574 Views

ProblemWe are required to write a JavaScript function that takes in two arrays of strings. Our function should return the number of times each string of the second array appears in the first array.ExampleFollowing is the code − Live Democonst arr1 = ['abc', 'abc', 'xyz', 'cde', 'uvw']; const arr2 = ['abc', 'cde', 'uap']; const findFrequency = (arr1 = [], arr2 = []) => {    const res = [];    let count = 0;    for (let i = 0; i < arr2.length; i++){       for (let j = 0; j < arr1.length; j++){          if (arr2[i] === arr1 [j]){             count++;          }       }       res.push(count);       count = 0;    }    return res; }; console.log(findFrequency(arr1, arr2));Output[2, 1, 0]

Sorting string of words based on the number present in each word using JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:28:01

647 Views

ProblemWe are required to write a JavaScript function that takes in a string that represents a sentence. Our function should sort this sentence.Each word in the sentence string contains an integer. Our function should sort the string such that the word that contains the smallest integer is placed first and then in the increasing order.ExampleFollowing is the code − Live Democonst str = "is2 Thi1s T4est 3a"; const sortByNumber = (str = '') => {    const findNumber = (s = '') => s       .split('')       .reduce((acc, val) => +val ? +val : acc, 0);   ... Read More

Returning the value of nth power of iota(i) using JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:27:07

152 Views

ProblemWe are required to write a JavaScript function that takes in a number. Our function should return the value of −(i)nHere,i = -11/2Therefore,i^2 = -1 i^3 = -i i^4 = 1 and so onExampleFollowing is the code − Live Democonst num = 657; const findNthPower = (num = 1) => {    switch(num % 4){       case 0:          return '1';       case 1:          return 'i';       case 2:          return '-1';       case 3:          return '-i';    }; }; console.log(findNthPower(num));Outputi

Finding and returning uncommon characters between two strings in JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:26:27

1K+ Views

ProblemWe are required to write a JavaScript function that takes in two strings. Our function should return a new string of characters which is not common to both the strings.ExampleFollowing is the code − Live Democonst str1 = "xyab"; const str2 = "xzca"; const findUncommon = (str1 = '', str2 = '') => {    const res = [];    for (let i = 0; i < str1.length; i++){       if (!(str2.includes(str1[i]))){          res.push(str1[i])       }    }    for (let i = 0; i < str2.length; i++){       if (!(str1.includes(str2[i]))){          res.push(str2[i])       }    }    return res.join(""); }; console.log(findUncommon(str1, str2));Outputybzc

Advertisements