Found 6683 Articles for Javascript

Returning a range or a number that specifies the square root of a number in JavaScript

AmitDiwan
Updated on 20-Apr-2021 08:12:09

126 Views

ProblemWe are required to write a JavaScript function that takes in an integer n and returns either −an integer k if n is a square number, such that k * k == n ora range (k, k+1), such that k * k < n and n < (k+1) * (k+1).ExampleFollowing is the code − Live Democonst num = 83; const squareRootRange = (num = 1) => {    const exact = Math.sqrt(num);    if(exact === Math.floor(exact)){       return exact;    }else{         return [Math.floor(exact), Math.ceil(exact)];    }; }; console.log(squareRootRange(num));OutputFollowing is the console output −[9, 10]

Returning a decimal that have 1s in its binary form only at indices specified by array in JavaScript

AmitDiwan
Updated on 20-Apr-2021 08:10:39

61 Views

ProblemWe are required to write a JavaScript function that takes in an array of unique non-negative integers. Our function should return a 32-bit integer such that the integer, in its binary representation, has 1 at only those indexes (counted from right) which are in the sequence.ExampleFollowing is the code − Live Democonst arr = [1, 2, 0, 4]; const buildDecimal = (arr = []) => {    const bitArr = Array(31).fill(0);    let res = 0;    arr.forEach(el => {       bitArr.splice((31 - el), 1, 1);    })    bitArr.forEach((bit, index) => {       res += (2 * (31-index) * bit);    });    return res; }; console.log(buildDecimal(arr));OutputFollowing is the console output −14

Finding the 1-based index score of a lowercase alpha string in JavaScript

AmitDiwan
Updated on 20-Apr-2021 08:07:12

125 Views

ProblemWe are required to write a JavaScript function that takes in a lowercase alphabet string. The index of ‘a’ in alphabets is 1, of ‘b’ is 2 ‘c’ is 3 … of ‘z’ is 26.Our function should sum all the index of the string characters and return the result.ExampleFollowing is the code − Live Democonst str = 'lowercasestring'; const findScore = (str = '') => {    const alpha = 'abcdefghijklmnopqrstuvwxyz';    let score = 0;    for(let i = 0; i < str.length; i++){       const el = str[i];       const index = alpha.indexOf(el);       score += (index + 1);    };    return score; }; console.log(findScore(str));OutputFollowing is the console output −188

Finding the only even or the only odd number in a string of space separated numbers in JavaScript

AmitDiwan
Updated on 20-Apr-2021 08:04:48

355 Views

ProblemWe are required to write a JavaScript function that takes in a string that contains numbers separated by spaces.The string either contains all odd numbers and only one even number or all even numbers and only one odd number. Our function should return that one different number from the string.ExampleFollowing is the code − Live Democonst str = '2 4 7 8 10'; const findDifferent = (str = '') => {    const odds = [];    const evens = [];    const arr = str    .split(' ')    .map(Number);    arr.forEach(num => {       if(num % 2 ... Read More

Return the nearest greater integer of the decimal number it is being called on in JavaScript

AmitDiwan
Updated on 20-Apr-2021 08:01:59

213 Views

ProblemWe are required to write a JavaScript function that lives in the Math class of JavaScript.Our function should return the nearest greater integer of the decimal number it is being called on.If the number is already an integer, we should return it as it is.ExampleFollowing is the code − Live Democonst num = 234.56; Math.ceil = function(num){    if(typeof num !== 'number'){       return NaN;    };    if(num % 1 === 0){       return num;    };    const [main] = String(num).split('.');      return +main + 1; }; console.log(Math.ceil(num));OutputFollowing is the console output −235

All right triangles with specified perimeter in JavaScript

AmitDiwan
Updated on 20-Apr-2021 07:56:27

71 Views

ProblemWe are required to write a JavaScript function that takes in a number that specifies the perimeter for a triangle. Our function should return an array of all the triangle side triplets whose perimeter is same as specified by the input.ExampleFollowing is the code − Live Democonst perimeter = 120; const findAllRight = (perimeter = 1) => {    const res = [];    for(let a = 1; a

Reducing array elements to all odds in JavaScript

AmitDiwan
Updated on 20-Apr-2021 07:53:59

98 Views

ProblemWe are required to write a JavaScript function that takes in an array. Our function should change the array numbers like this −If the number is odd, leave it changed.If the number is even, subtract 1 from it.And we should return the new array.ExampleFollowing is the code − Live Democonst arr = [5, 23, 6, 3, 66, 12, 8]; const reduceToOdd = (arr = []) => {    const res = [];    for(let i = 0; i < arr.length; i++){       const el = arr[i];       if(el % 2 === 1){          res.push(el);       }else{          res.push(el - 1);       };    };    return res; }; console.log(reduceToOdd(arr));OutputFollowing is the console output −[ 5, 23, 5, 3, 65, 11, 7 ]

Retrieving the decimal part only of a number in JavaScript

Aayush Mohan Sinha
Updated on 04-Aug-2023 10:32:04

924 Views

Precision and accuracy play vital roles in numerical computations, and in the realm of JavaScript programming, the ability to extract the decimal part of a number is a crucial skill. Whether it is for rounding, comparison, or further manipulation, retrieving only the decimal part of a number can significantly enhance the precision and control over calculations. This article aims to explore a comprehensive approach to retrieving the decimal part exclusively in JavaScript, employing lesser-known techniques and rarely used functions. By mastering this technique, developers can ensure greater control over numeric operations, paving the way for more sophisticated algorithms and precise ... Read More

Validating a string with numbers present in it in JavaScript

AmitDiwan
Updated on 20-Apr-2021 07:48:45

90 Views

ProblemWe are required to write a JavaScript function that takes in a string str. Our function should validate the alphabets in the string based on the numbers before them.We need to split the string by the numbers, and then compare the numbers with the number of characters in the following substring. If they all match, the string is valid and we should return true, false otherwise.For example −5hello4from2meshould return trueBecause when split by numbers, the string becomes ‘hello’, ‘from’, ‘me’ and all these strings are of same length as the number before themExampleFollowing is the code − Live Democonst str = ... Read More

Realtime moving average of an array of numbers in JavaScript

AmitDiwan
Updated on 20-Apr-2021 07:45:35

802 Views

ProblemWe are required to write a JavaScript function that takes in an array. Our function should construct a new array that stores the moving average of the elements of the input array. For instance −[1, 2, 3, 4, 5] → [1, 1.5, 3, 5, 7.5]First element is the average of the first element, the second element is the average of the first 2 elements, the third is the average of the first 3 elements and so on.ExampleFollowing is the code − Live Democonst arr = [1, 2, 3, 4, 5]; const movingAverage = (arr = []) => {    const res ... Read More

Advertisements