Found 6683 Articles for Javascript

Implementing a custom function like Array.prototype.filter() function in JavaScript

AmitDiwan
Updated on 21-Apr-2021 06:52:46

799 Views

ProblemWe are required to write a JavaScript function that lives on the prototype Object of the Array class.Our function should take in a callback function as the only argument. This callback function should be called for each element of the array.And that callback function should take in two arguments the corresponding element and its index. If the callback function returns true, we should include the corresponding element in our output array otherwise we should exclude it.ExampleFollowing is the code − Live Democonst arr = [5, 3, 6, 2, 7, -4, 8, 10]; const isEven = num => num % 2 === ... Read More

Summing cubes of natural numbers within a range in JavaScript

AmitDiwan
Updated on 21-Apr-2021 06:52:13

99 Views

ProblemWe are required to write a JavaScript function that takes in a range array of two numbers. Our function should find the sum of all the cubes of the numbers that falls in the specified range.ExampleFollowing is the code − Live Democonst range = [4, 11]; const sumCubes = ([l, h]) => {    const findCube = num => num * num * num;    let sum = 0;    for(let i = l; i

Calculating and adding the parity bit to a binary using JavaScript

AmitDiwan
Updated on 21-Apr-2021 06:51:26

522 Views

Parity BitA parity bit, or check bit, is a bit added to a string of bits to ensure that the total number of 1-bits in the string is even or odd.ProblemWe are required to write a JavaScript function that takes in two parameters, one being the wanted parity (always 'even' or 'odd'), and the other being the binary representation of the number we want to check.The task of our function is to return an integer (0 or 1), which is the parity bit we need to add to the binary representation so that the parity of the resulting string is ... Read More

Sorting binary string having an even decimal value using JavaScript

AmitDiwan
Updated on 21-Apr-2021 06:49:07

126 Views

ProblemWe are required to write a JavaScript function that takes in a string that contains binary strings of length 3 all separated by spaces.Our function should sort the numbers in ascending order but only order the even numbers and leave all odd numbers in their place.ExampleFollowing is the code − Live Democonst str = '101 111 100 001 010'; const sortEvenIncreasing = (str = '') => {    const sorter = (a, b) => {       const findInteger = bi => parseInt(bi, 2);       if(findInteger(a) % 2 === 1 || findInteger(b) % 2 === 1){          return 0;       };       return findInteger(a) - findInteger(b);    };    const res = str    .split(' ')    .sort(sorter)    .join(' ');    return res; }; console.log(sortEvenIncreasing(str));Output101 111 100 001 010

Calculating variance for an array of numbers in JavaScript

AmitDiwan
Updated on 21-Apr-2021 06:48:38

1K+ Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers sorted in increasing order.Our function should calculate the variance for the array of numbers. Variance of a set of numbers is calculated on the basis of their mean.$Mean (M) = ( \sum_{i=0}^{n-1} arr[i])$ / nAnd variance (V) = $(\sum_{i=0}^{n-1} (arr[i] - M)^2)$ / nExampleFollowing is the code − Live Democonst arr = [4, 6, 7, 8, 9, 10, 10]; const findVariance = (arr = []) => {    if(!arr.length){       return 0;    };    const sum = arr.reduce((acc, val) => acc + val); ... Read More

Finding the nth power of array element present at nth index using JavaScript

AmitDiwan
Updated on 21-Apr-2021 06:46:31

246 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers. Our function should map the input array to another array in which each element is raised to its 0-based index.And finally, our function should return this new array.ExampleFollowing is the code − Live Democonst arr = [5, 2, 3, 7, 6, 2]; const findNthPower = (arr = []) => {    const res = [];    for(let i = 0; i < arr.length; i++){       const el = arr[i];       const curr = Math.pow(el, i);       res[i] = curr;    };    return res; }; console.log(findNthPower(arr));Output[ 1, 2, 9, 343, 1296, 32 ]

Evaluating a mathematical expression considering Operator Precedence in JavaScript

AmitDiwan
Updated on 21-Apr-2021 06:46:11

379 Views

ProblemWe are required to write a JavaScript function that takes in a mathematical expression as a string and return its result as a number.We need to support the following mathematical operators −Division / (as floating-point division)Addition +Subtraction -Multiplication *Operators are always evaluated from left-to-right, and * and / must be evaluated before + and -.ExampleFollowing is the code − Live Democonst exp = '6 - 4'; const findResult = (exp = '') => {    const digits = '0123456789.';    const operators = ['+', '-', '*', '/', 'negate'];    const legend = {       '+': { pred: 2, func: ... Read More

Deleting occurrences of an element if it occurs more than n times using JavaScript

Aayush Mohan Sinha
Updated on 04-Aug-2023 09:26:25

196 Views

In the realm of JavaScript programming, effectively managing the occurrences of elements within an array is of paramount importance. Specifically, the ability to delete instances of an element if it surpasses a certain threshold, denoted by the variable "n, " can greatly enhance the efficiency and accuracy of data manipulation tasks. By harnessing the power of JavaScript, developers can employ a robust approach to selectively remove redundant element occurrences from arrays. In this article, we will embark on a comprehensive exploration of the step-by-step process for deleting occurrences of an element if it occurs more than "n" times using JavaScript, ... Read More

Sorting 2-D array of strings and finding the diagonal element using JavaScript

AmitDiwan
Updated on 21-Apr-2021 06:44:33

85 Views

ProblemWe are required to write a JavaScript function that takes in an array of n strings. And each string in the array consists of exactly n characters.Our function should first sort the array in alphabetical order. And then return the string formed by the characters present at the principal diagonal starting from the top left corner.ExampleFollowing is the code − Live Democonst arr = [    'star',    'abcd',    'calm',    'need' ]; const sortPickDiagonal = () => {    const copy = arr.slice();    copy.sort();    let res = '';    for(let i = 0; i < copy.length; i++){ ... Read More

Finding the sum of all common elements within arrays using JavaScript

AmitDiwan
Updated on 21-Apr-2021 06:43:56

206 Views

ProblemWe are required to write a JavaScript function that takes in three arrays of numbers. Our function should return the sum of all those numbers that are common in all three arrays.ExampleFollowing is the code − Live Democonst arr1 = [4, 4, 5, 8, 3]; const arr2 = [7, 3, 7, 4, 1]; const arr3 = [11, 0, 7, 3, 4]; const sumCommon = (arr1 = [], arr2 = [], arr3 = []) => {    let sum = 0;    for(let i = 0; i < arr1.length; i++){       const el = arr1[i];       const ind2 = arr2.indexOf(el);       const ind3 = arr3.indexOf(el);       if(ind2 !== -1 && ind3 !== -1){          arr2.splice(ind2, 1);          arr3.splice(ind3, 1);          sum += el;       };    };    return sum; }; console.log(sumCommon(arr1, arr2, arr3));Output7

Advertisements