Found 10710 Articles for Web Development

Volume difference of cuboids in JavaScript

AmitDiwan
Updated on 19-Apr-2021 11:08:58

90 Views

ProblemWe are required to write a JavaScript function that takes in two arrays, specifying the lengths, widths, and heights of two cuboids.Our function should calculate the volume of both cuboids and return their absolute difference.ExampleFollowing is the code − Live Democonst h1 = 10; const w1 = 12; const l1 = 15; const h2 = 12; const w2 = 15; const l2 = 9; const findVolumeDifference = (l1, w1, h1, l2, w2, h2) => {    const v1 = l1 * w1 * h1;    const v2 = l2 * w2 * h2;    const diff = Math.abs(v1 - v2);    return diff; }; console.log(findVolumeDifference(l1, w1, h1, l2, w2, h2));Output180

Finding the length of the diagonal of a cuboid using JavaScript

AmitDiwan
Updated on 19-Apr-2021 11:08:36

175 Views

ProblemWe are required to write a JavaScript function that takes in the length, width and height of a cuboid and return the length of its diagonal.ExampleFollowing is the code − Live Democonst height = 10; const width = 12; const length = 15; const findDiagonal = (l, w, h) => {    const ll = l * 2;    const ww = w * 2;    const hh = h * 2;    const sum = ll + ww + hh;    const diagonal = Math.sqrt(sum);    return diagonal; }; console.log(findDiagonal(length, width, height));Output8.602325267042627

Integers have sum of squared divisors as perfect square in JavaScript

AmitDiwan
Updated on 19-Apr-2021 11:08:20

156 Views

ProblemWe are required to write a JavaScript function that takes in a range specified by an array of two numbers m and n.Our function is supposed to find all integers between m and n (m and n integers such as 1

Finding one missing number in a scrambled sequence using JavaScript

AmitDiwan
Updated on 19-Apr-2021 11:08:03

157 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers containing numbers from 1 to n.The problem is that one number from the array goes missing and the array is not sorted as well. Our function should find and return that one number missing from the array.ExampleFollowing is the code − Live Democonst arr = [4, 7, 1, 8, 9, 5, 2, 3]; const findMissing = (arr = []) => {    const sumArr = arr.reduce((acc, val) => acc + val);    const { length: len } = arr;    const sumFirst = (len + 1) * (len + 2) * .5;    const missing = sumFirst - sumArr;    return missing; }; console.log(findMissing(arr));Output6

Finding all solutions of a Diophantine equation using JavaScript

AmitDiwan
Updated on 19-Apr-2021 11:07:46

215 Views

ProblemWe are required to write a JavaScript function that takes in a number n. Our function should find all such number x and y such that −x^2 - 4y^2 = n.And it should return an array of all such pairs.ExampleFollowing is the code − Live Democonst num = 90005; const findSolution = (num = 1) => {    const res = [];    let a, b;    for(let a = 1; a

Smallest possible number divisible by all numbers from 1 to n in JavaScript

AmitDiwan
Updated on 19-Apr-2021 11:05:36

117 Views

ProblemWe are required to write a JavaScript function that takes in a number n. Our function should find and return that smallest possible number which is divisible by all numbers from 1 to n.ExampleFollowing is the code − Live Democonst num = 11; const smallestDivisible = (num = 1) => {    let res = num * (num - 1) || 1;    for (let i = num - 1; i >= 1; i--) {       if (res % i) {          for (let j = num - 1; j >= 1; j--) {             if (!(i % j) && !(res % j)) {                res = i * res / j;                break;             }          }       }    }    return res; } console.log(smallestDivisible(num));Output27720

Merging two sorted arrays into one sorted array using JavaScript

AmitDiwan
Updated on 19-Apr-2021 10:57:27

1K+ Views

ProblemWe are required to write a JavaScript function that takes in two sorted arrays of numbers our function should merge all the elements of both the arrays into a new array and return that new array sorted in the same order.ExampleFollowing is the code − Live Democonst arr1 = [1, 3, 4, 5, 6, 8]; const arr2 = [4, 6, 8, 9, 11]; const mergeSortedArrays = (arr1 = [], arr2 = []) => {    const res = [];    let i = 0;    let j = 0;    while(i < arr1.length && j < arr2.length){       if(arr1[i] ... Read More

Finding the longest non-negative sum sequence using JavaScript

AmitDiwan
Updated on 19-Apr-2021 10:56:55

147 Views

ProblemWe are required to write a JavaScript function that takes in an array containing a sequence of integers, each element of which contains a possible value ranging between -1 and 1.Our function should return the size of the longest sub-section of that sequence with a sum of zero or higher.ExampleFollowing is the code − Live Democonst arr = [-1, -1, 0, 1, 1, -1, -1, -1]; const longestPositiveSum = (arr = []) => {    let sum = 0;    let maxslice = 0;    let length = arr.length;    const sumindex = [];    let marker = length * 2 ... Read More

Is the reversed number a prime number in JavaScript

AmitDiwan
Updated on 19-Apr-2021 10:56:37

206 Views

ProblemWe are required to write a JavaScript function that takes in a number and return true if the reverse of that number is a prime number, false otherwise.ExampleFollowing is the code − Live Democonst num = 13; const findReverse = (num) => {    return +num    .toString()    .split('')    .reverse()    .join(''); }; const isPrime = (num) => {    let sqrtnum = Math.floor(Math.sqrt(num));    let prime = num !== 1;    for(let i = 2; i < sqrtnum + 1; i++){       if(num % i === 0){          prime = false;          break;       };    };    return prime; } const isReversePrime = num => isPrime(findReverse(num)); console.log(isReversePrime(num));Outputtrue

Repeating string for specific number of times using JavaScript

Aayush Mohan Sinha
Updated on 04-Aug-2023 10:28:22

1K+ Views

In the realm of JavaScript programming, the ability to repeat a string for a specific number of times is a significant functionality that can greatly enhance the efficiency and versatility of code. JavaScript, being a flexible and powerful language, provides developers with the tools to accomplish this task effortlessly. Although the concept may seem elementary, mastering the art of repeating a string for a specific number of times requires an understanding of the underlying mechanisms and the appropriate utilization of rarely used functions. In this article, we will explore the intricacies of repeating strings using JavaScript, diving into the less ... Read More

Advertisements