Found 6710 Articles for Javascript

Splitting Number into k length array in JavaScript

AmitDiwan
Updated on 15-Oct-2020 09:12:13

231 Views

We are required to write a JavaScript function that takes in two numbers say m and k, and returns an array of size k with all the elements of the resulting array adding up to m.ExampleThe code for this will be −const len = 30; const sum = 121; const splitNumber = (len, sum) => {    const res = [];      for(let i = 0; i < len; i++){       res.push(sum / len);    };    return res; }; console.log(splitNumber(len, sum));OutputThe output in the console −[    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333,    4.033333333333333, 4.033333333333333 ]

Checking for special type of Arrays in JavaScript

AmitDiwan
Updated on 15-Oct-2020 09:10:17

149 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];ExampleThe code for this will be −const arr = [1, 5, 7, 4, 15, 4, 7, 5, 1]; const isPalindrome = arr => {    const { length: l } = arr;    const mid = Math.floor(l / 2);    for(let i = 0; i

Merge and remove duplicates in JavaScript Array

AmitDiwan
Updated on 15-Oct-2020 09:08:52

384 Views

Suppose, we have two arrays of literals like these −const arr1 = [2, 4, 5, 3, 7, 8, 9]; const arr2 = [1, 4, 5, 2, 3, 7, 6];We are required to write a JavaScript function that takes in two such arrays and returns a new array with all the duplicates removed (should appear only once).ExampleThe code for this will be −const arr1 = [2, 4, 5, 3, 7, 8, 9]; const arr2 = [1, 4, 5, 2, 3, 7, 6]; const mergeArrays = (first, second) => {    const { length: l1 } = first;    const { length: ... Read More

Greater possible digit difference of a number in JavaScript

AmitDiwan
Updated on 15-Oct-2020 09:07:04

174 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.For example:If the number is 654646, Then the smallest digit here is 4 and the greatest is 6 Hence, our output should be 2ExampleThe code for this will be −const num = 654646; const maxDifference = (num, min = Infinity, max = -Infinity) => {    if(num){       const digit ... Read More

Picking all the numbers present in a string in JavaScript

AmitDiwan
Updated on 15-Oct-2020 09:03:15

143 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.ExampleThe 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]){          res += +strArr[i];       };    };    return res; }; console.log(sumNum(str));OutputThe output in the console −35

Addition multiplication ladder in an array in JavaScript

AmitDiwan
Updated on 15-Oct-2020 09:00:59

157 Views

We are required to write a JavaScript function that takes in an array of numbers and returns the alternative multiplicative sum of the elements.For example: If the array is −const arr = [1, 2, 3, 4, 5, 6, 7];Then the output should be calculated like this −1*2+3*4+5*6+7 2+12+30+7And the output should be −51Let’s write the code for this function −ExampleThe code for this will be −const arr = [1, 2, 3, 4, 5, 6, 7]; const alternateOperation = arr => {    const productArr = arr.reduce((acc, val, ind) => {       if(ind % 2 === 1){          return acc;       };       acc.push(val * (arr[ind + 1] || 1));       return acc;    }, []);    return productArr.reduce((acc, val) => acc + val); }; console.log(alternateOperation(arr));OutputThe output in the console −51

Object to Map conversion in JavaScript

AmitDiwan
Updated on 15-Oct-2020 08:59:02

188 Views

Suppose, we have an object like this −const obj = {    name: "Jai",    age: 32,    occupation: "Software Engineer",    address: "Dhindosh, Maharasthra",    salary: "146000" };We are required to write a JavaScript function that takes in such an object with key value pairs and converts it into a Map.ExampleThe code for this will be −const obj = {    name: "Jai",    age: 32,    occupation: "Software Engineer",    address: "Dhindosh, Maharasthra",    salary: "146000" }; const objectToMap = obj => {    const keys = Object.keys(obj);    const map = new Map();    for(let i = ... Read More

Converting multi-dimensional array to string in JavaScript

AmitDiwan
Updated on 15-Oct-2020 08:57:16

1K+ Views

We are required to write a JavaScript function that takes in a nested array of literals and converts it to a string by concatenating all the values present in it to the string. Moreover, we should append a whitespace at the end of each string element while constructing the new string.Let’s write the code for this function −ExampleThe code for this will be −const arr = [    'this', [       'is', 'an', [          'example', 'of', [             'nested', 'array'          ]       ] ... Read More

ASCII sum difference of strings in JavaScript

AmitDiwan
Updated on 15-Oct-2020 08:53:55

634 Views

ASCII Code:ASCII is a 7-bit character code where every single bit represents a unique character. Every English alphabet has a unique decimal ascii code.We are required to write a function that takes in two strings and calculates their ascii scores (i.e., the sum of ascii decimal of each character of string) and returns the difference.Let’s write the code for this function −ExampleThe code for this will be −const str1 = 'This is an example sting'; const str2 = 'This is the second string'; const calculateScore = (str = '') => {    return str.split("").reduce((acc, val) => {       ... Read More

Prime numbers within a range in JavaScript

AmitDiwan
Updated on 15-Oct-2020 08:51:24

691 Views

We are required to write a JavaScript function that takes in two numbers, say, a and b and returns the total number of prime numbers between a and b (including a and b, if they are prime).For example: If a = 21, and b = 38.The prime numbers between them are 23, 29, 31, 37And their count is 4Our function should return 4ExampleThe code for this will be −const isPrime = num => {    let count = 2;    while(count < (num / 2)+1){       if(num % count !== 0){          count++;          continue;       };       return false;    };    return true; }; const primeBetween = (a, b) => {    let count = 0;    for(let i = Math.min(a, b); i

Advertisements