Found 10483 Articles for Web Development

Finding average word length of sentences - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:31:28

444 Views

We are required to write a JavaScript function that takes in a string of strings joined by whitespaces. The function should calculate and return the average length of all the words present in the string rounded to two decimal placesExampleFollowing is the code −const str = 'This sentence will be used to calculate average word length'; const averageWordLength = str => {    if(!str.includes(' ')){       return str.length;    };    const { length: strLen } = str;    const { length: numWords } = str.split(' ');    const average = (strLen - numWords + 1) / numWords; ... Read More

Finding lunar sum of Numbers - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:30:04

290 Views

Lunar SumThe concept of lunar sum says that the sum of two numbers is calculated, instead of adding the corresponding digits, but taking the bigger of the corresponding digits.For example −Let’s say, a = 879 and b = 768(for the scope of this problem, consider only the number with equal digits)Then the lunar sum of a and b will be −879We are required to write a JavaScript function that takes in two numbers and returns their lunar sum.ExampleFollowing is the code −const num1 = 6565; const num2 = 7385; const lunarSum = (num1, num2) => {    const numStr1 = ... Read More

Equal partition of an array of numbers - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:28:56

276 Views

We are required to write a function that returns true if we can partition an array into one element and the rest, such that this one element is equal to the product of all other elements excluding itself, false otherwise.For example: If the array is −const arr = [1, 56, 2, 4, 7];Then the output should be trueBecause, 56 is equal to −2 * 4 * 7 * 1ExampleFollowing is the code −const arr = [1, 56, 2, 4, 7]; const isEqualPartition = arr => {    const creds = arr.reduce((acc, val) => {       let { prod, ... Read More

Find distinct elements - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:27:48

141 Views

We are required to write a JavaScript function that takes in an array of literals, such that some array elements are repeated. We are required to return an array that contains that appear only once (not repeated).For example: If the array is:>const arr = [9, 5, 6, 8, 7, 7, 1, 1, 1, 1, 1, 9, 8];Then the output should be −const output = [5, 6];ExampleFollowing is the code −const arr = [9, 5, 6, 8, 7, 7, 1, 1, 1, 1, 1, 9, 8]; const findDistinct = arr => {    const res = [];    for(let i = 0; i < arr.length; i++){       if(arr.indexOf(arr[i]) !== arr.lastIndexOf(arr[i])){          continue;       };       res.push(arr[i]);    };    return res; }; console.log(findDistinct(arr));OutputFollowing is the output in the console −[5, 6]

Filtering out primes from an array - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:26:21

2K+ Views

We are required to write a JavaScript function that takes in the following array of numbers,const arr = [34, 56, 3, 56, 4, 343, 68, 56, 34, 87, 8, 45, 34];and returns a new filtered array that does not contain any prime number.ExampleFollowing is the code −const arr = [34, 56, 3, 56, 4, 343, 68, 56, 34, 87, 8, 45, 34]; const isPrime = n => {    if (n===1){    return false;    }else if(n === 2){       return true;    }else{       for(let x = 2; x < n; x++){          if(n % x === 0){             return false;          }       }       return true;    }; }; const filterPrime = arr => {    const filtered = arr.filter(el => !isPrime(el));    return filtered; }; console.log(filterPrime(arr));OutputFollowing is the output in the console −[    34, 56, 56,  4, 343,    68, 56, 34, 87,   8,    45, 34 ]

Resolving or rejecting promises accordingly - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:25:16

104 Views

We are required to write a JavaScript function that imitates a network request, for that we can use the JavaScript setTimeout() function, that executes a task after a given time interval.Our function should return a promise that resolves when the request takes place successfully, otherwise it rejectsExampleFollowing is the code −const num1 = 45, num2 = 48; const res = 93; const expectedSumToBe = (num1, num2, res) => {    return new Promise((resolve, reject) => {       setTimeout(() => {          if(num1 + num2 === res){             resolve('success');     ... Read More

Frequency distribution of elements - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:22:50

1K+ Views

We are required to write a JavaScript function that contains the following string −const str = 'This string will be used to calculate frequency distribution';We need to return an object that represents the frequency distribution of various elements present in the array.ExampleFollowing is the code −const str = 'This string will be used to calculate frequency distribution'; const frequencyDistribution = str => {    const map = {};    for(let i = 0; i < str.length; i++){       map[str[i]] = (map[str[i]] || 0) + 1;    };    return map; }; console.log(frequencyDistribution(str));OutputFollowing is the output in the console ... Read More

Finding mistakes in a string - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:21:38

483 Views

We are required to write a JavaScript function that takes in two strings. The first string is some mistyped string and the second string is the correct version of this sting. We can assume that the two strings we are getting as argument will always have the same length.We have to return the number of mistakes that exist in the first array.ExampleFollowing is the code −const str1 = 'Tkos am er exakgrg fetwnh'; const str2 = 'This is an example string'; const countMistakes = (mistaken, correct) => {    let count = 0;    for(let i = 0; i < ... Read More

Implementing Priority Sort in JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:20:26

2K+ Views

We are required to write a JavaScript function that takes in two arrays of numbers, second being smaller in size than the first.Our function should be a sorted version of the first array (say in increasing order) but put all the elements that are common in both arrays to the front.For example − If the two arrays are −const arr1 = [5, 4, 3, 2, 1]; const arr2 = [2, 3];Then the output should be −const output = [2, 3, 1, 4, 5];ExampleFollowing is the code −const arr1 = [5, 4, 3, 2, 1]; const arr2 = [2, 3]; // ... Read More

Converting a proper fraction to mixed fraction - JavaScript

AmitDiwan
Updated on 18-Sep-2020 09:19:27

479 Views

Proper FractionA proper fraction is the one that exists in the p/q form (both p and q being natural numbers)Mixed FractionSuppose we divide the numerator of a fraction (say a) with its denominator (say b), to get quotient q and remainder r.The mixed fraction form for fraction (a/b) will be −qrbAnd it is pronounced as "q wholes and r by b”.We are required to write a JavaScript function that takes in an array of exactly two numbers representing a proper fraction and our function should return an array with three numbers representing its mixed formExampleFollowing is the code −const arr ... Read More

Advertisements