Web Development Articles

Page 45 of 801

Evaluating a mathematical expression considering Operator Precedence in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 570 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 −const exp = '6 - 4'; const findResult = (exp = '') => {    const digits = '0123456789.';    const operators = ['+', '-', '*', '/', 'negate'];    const legend = {       '+': { pred: 2, func: (a, ...

Read More

Calculating variance for an array of numbers in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 2K+ 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 −const 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

Sorting binary string having an even decimal value using JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 223 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 −const 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

Read More

Calculating and adding the parity bit to a binary using JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 758 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

Summing cubes of natural numbers within a range in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 202 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 −const range = [4, 11]; const sumCubes = ([l, h]) => {    const findCube = num => num * num * num;    let sum = 0;    for(let i = l; i

Read More

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

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 966 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 −const arr = [5, 3, 6, 2, 7, -4, 8, 10]; const isEven = num => num % 2 === 0; ...

Read More

Counting number of triangle sides in an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 258 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers, arr, as the first and the only argument.The task of our function is to count the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle.For example, if the input to the function is −const arr = [2, 2, 3, 4];Then the output should be −const output = 3;Output ExplanationValid combinations are:2, 3, 4 (using the first 2) 2, 3, 4 (using the second 2) 2, 2, 3ExampleFollowing is the code −const arr = ...

Read More

Finding the 1-based index of a character in alphabets using JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 229 Views

ProblemWe are required to write a JavaScript function that takes in a lowercase English alphabet character. Our function should return the character’s 1-based index in the alphabets.ExampleFollowing is the code −const char = 'j'; const findCharIndex = (char = '') => {    const legend = ' abcdefghijklmnopqrstuvwxyz';    if(!char || !legend.includes(char) || char.length !== 1){       return -1;    };    return legend.indexOf(char); }; console.log(findCharIndex(char));Output10

Read More

Constructing an array of addition/subtractions relative to first array element in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 178 Views

ProblemWe are required to write a JavaScript function that takes in an array of positive integers. Our function should map this array to an array of string integers.The array should contain the number we should add/subtract to the first element to achieve the corresponding element.For example[4, 3, 6, 2]should return −['+0', '-1', '+2', '-2']ExampleFollowing is the code −const arr = [4, 3, 6, 2]; const buildRelative = (arr = []) => {    const res = [];    let num = '';    for(let i of arr){       if(i - arr[0] >= 0){          num ...

Read More

Generating desired pairs within a range using JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 274 Views

ProblemWe are required to write a JavaScript function that takes in a number n. Our function should generate an array containing the pairs of integers [a, b] that satisfy the following conditions −0

Read More
Showing 441–450 of 8,006 articles
« Prev 1 43 44 45 46 47 801 Next »
Advertisements