Found 6710 Articles for Javascript

Removing first k characters from string in JavaScript

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

172 Views

We are required to write a JavaScript function that takes in a string and a number, say k and returns another string with first k characters removed from the string.For example: If the original string is −const str = "this is a string"and, n = 4then the output should be −const output = " is a string"ExampleThe code for this will be −const str = 'this is a string'; const removeN = (str, num) => {    const { length } = str;    if(num > length){       return str;    };    const newStr = str.substr(num, length ... Read More

Smart concatenation of strings in JavaScript

AmitDiwan
Updated on 15-Oct-2020 08:43:22

171 Views

We are required to write a JavaScript function that takes in two strings and concatenates the second string to the first string.If the last character of the first string and the first character of the second string are the same then we have to omit one of those characters.ExampleThe code for this will be −const str1 = 'Food'; const str2 = 'dog'; const concatenateStrings = (str1, str2) => {    const { length: l1 } = str1;    const { length: l2 } = str2;    if(str1[l1 - 1] !== str2[0]){       return str1 + str2;    }; ... Read More

Check for perfect square in JavaScript

AmitDiwan
Updated on 15-Oct-2020 08:41:47

1K+ Views

We are required to write a JavaScript function that takes in a number and returns a boolean based on the fact whether or not the number is a perfect square.Examples of perfect square numbers −Some perfect square numbers are −144, 196, 121, 81, 484ExampleThe code for this will be −const num = 484; const isPerfectSquare = num => {    let ind = 1;    while(ind * ind

Finding sum of every nth element of array in JavaScript

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

1K+ Views

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.ExampleThe code for this will be −const arr = [5, 3, 5, 6, 12, 5, 65, 3, 2]; const num = 3; const nthSum = (arr, num) => {    let sum = 0;    for(let i = 0; i < arr.length; i++){       if(i % num !== 0){          continue;       };       sum += arr[i];    };    return sum; }; console.log(nthSum(arr, num));OutputThe output in the console −76

Excluding extreme elements from average calculation in JavaScript

AmitDiwan
Updated on 14-Oct-2020 08:21:22

205 Views

We are required to write a JavaScript function that takes in an array of Number. Then the function should return the average of its elements excluding the smallest and largest Number.ExampleThe code for this will be −const arr = [5, 3, 5, 6, 12, 5, 65, 3, 2]; const findExcludedAverage = arr => {    const creds = arr.reduce((acc, val) => {       let { min, max, sum } = acc;       sum += val;       if(val > max){          max = val;       };       if(val < min){          min = val;       };       return { min, max, sum };    }, {       min: Infinity,       max: -Infinity,       sum: 0    });    const { max, min, sum } = creds;    return (sum - min - max) / (arr.length / 2); }; console.log(findExcludedAverage(arr));OutputThe output in the console −8.666666666666666

Equality of corresponding elements in JavaScript

AmitDiwan
Updated on 14-Oct-2020 08:19:50

180 Views

We are required to write a JavaScript function that takes in two arrays of literals. The function should check the corresponding elements of the array. The function should return true if all the corresponding elements of the array are equal otherwise it should return false.ExampleThe code for this will be −const arr1 = [6, 7, 8, 9, 10, 11, 12, 14]; const arr2 = [6, 7, 8, 9, 10, 11, 12, 14]; const areEqual = (first, second) => {    if(first.length !== second.length){       return false;    };    for(let i = 0; i < first.length; i++){       if(first[i] === second[i]){          continue;       }       return false;    };    return true; }; console.log(areEqual(arr1, arr2));OutputThe output in the console −True

Splitting a string into parts in JavaScript

AmitDiwan
Updated on 14-Oct-2020 08:18:15

362 Views

We are required to write a JavaScript function that takes in a string and a number n (such that n exactly divides the length of string) and we need to return an array of string of length n containing n equal parts of the string.ExampleThe code for this will be −const str = 'we will be splitting this string into parts'; const num = 6; const divideEqual = (str, num) => {    const len = str.length / num;    const creds = str.split("").reduce((acc, val) => {       let { res, currInd } = acc;       ... Read More

Find the second most frequent element in array JavaScript

AmitDiwan
Updated on 14-Oct-2020 08:16:41

690 Views

We are required to write a JavaScript function that takes in a string and returns the character from the string that appears for the second most number of times.ExampleThe code for this will be −const arr = [5, 2, 6, 7, 54, 3, 2, 2, 5, 6, 7, 5, 3, 5, 3, 4]; const secondMostFrequent = arr => {    const map = arr.reduce((acc, val) => {       if(acc.has(val)){          acc.set(val, acc.get(val) + 1);       }else{          acc.set(val, 1);       };       return acc;    }, new Map);    const frequencyArray = Array.from(map);    return frequencyArray.sort((a, b) => {       return b[1] - a[1];    })[1][0]; }; console.log(secondMostFrequent(arr));OutputThe output in the console −2

Product of numbers present in a nested array in JavaScript

AmitDiwan
Updated on 14-Oct-2020 08:14:52

253 Views

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.ExampleThe code for this will be −const arr = [    1, 2, null, [       2, 5, null, undefined, false, 5, [          1, 3, false, 0, 2       ], 4, false    ], 4, 6, 0 ... Read More

Number of times a string appears in another JavaScript

AmitDiwan
Updated on 14-Oct-2020 08:07:16

229 Views

We are required to write a JavaScript function that takes in two strings and returns the count of the number of times the str1 appears in the str2.ExampleThe code for this will be −const main = 'This is the is main is string'; const sub = 'is'; const countAppearances = (main, sub) => {    const regex = new RegExp(sub, "g");    let count = 0;    main.replace(regex, (a, b) => {       count++;    });    return count; }; console.log(countAppearances(main, sub));OutputThe output in the console −4

Advertisements