Found 10483 Articles for Web Development

Converting to hex and summing the numeral part in JavaScript

AmitDiwan
Updated on 17-Apr-2021 10:49:30

284 Views

ProblemWe are required to write a JavaScript function that takes in a string. Our function should convert every character of the string to the hex value of its ascii code, then the result should be the sum of the numbers in the hex strings ignoring the letters present in hex.ExampleFollowing is the code − Live Democonst str = "Hello, World!"; const toHexAndSum = (str = '') => {    return str    .split('')    .map(c=>c.charCodeAt())    .map(n=>n.toString(16))    .join('')    .split('')    .filter(c=>'123456789'.includes(c))    .map(d=>parseInt(d))    .reduce((a, b)=>a+b, 0) }; console.log(toHexAndSum(str));OutputFollowing is the console output −91Read More

Finding the nth element of the lucas number sequence in JavaScript

AmitDiwan
Updated on 17-Apr-2021 10:32:56

267 Views

Lucas NumbersLucas numbers are numbers in a sequence defined like this −L(0) = 2 L(1) = 1 L(n) = L(n-1) + L(n-2)ProblemWe are required to write a JavaScript function that takes in a number n and return the nth lucas number.ExampleFollowing is the code − Live Democonst num = 21; const lucas = (num = 1) => {    if (num === 0)       return 2;    if (num === 1)       return 1;    return lucas(num - 1) +       lucas(num - 2); }; console.log(lucas(num));OutputFollowing is the console output −24476

Checking for centrally peaked arrays in JavaScript

AmitDiwan
Updated on 09-Apr-2021 09:09:37

107 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers, arr, as the first and the only argument.Our function should check whether the input array is a centrally peaked array or not. If it is a centrally peaked array, we should return true, false otherwise.The conditions for being a centrally peaked array are −arr.length >= 3There exists some i with 0 < i < arr.length - 1 such that:arr[0] < arr[1] < ... arr[i-1] < arr[i]arr[i] > arr[i+1] > ... > arr[arr.length - 1]For example, if the input to the function is −const arr = ... Read More

Making array unique in JavaScript

AmitDiwan
Updated on 09-Apr-2021 09:11:08

243 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers, arr, as the first and the only argument.A move consists of choosing any arr[i], and incrementing it by 1. Our function is supposed to return the least number of moves to make every value in the array arr unique.For example, if the input to the function is −const arr = [12, 15, 7, 15];Then the output should be −const output = 1;Output ExplanationBecause if we increment any 15 to 16, the array will consist of all unique elements.ExampleThe code for this will be − Live Democonst ... Read More

Validating push pop sequence in JavaScript

AmitDiwan
Updated on 09-Apr-2021 09:15:43

383 Views

ProblemJavaScript function that takes in two arrays, pushed and popped, as the first and the second argument. Both these arrays are guaranteed to consist of unique elements.Our function should return true if and only if this could have been the result of a sequence of push and pop operations on an initially empty stack, false otherwise.For example, if the input to the function is −const pushed = [1, 2, 3, 4, 5]; const popped = [4, 5, 3, 2, 1];Then the output should be −const output = true;Output ExplanationWe might do the following sequence −push(1), push(2), push(3), push(4), pop() -> ... Read More

Rearranging elements of an array in JavaScript

AmitDiwan
Updated on 09-Apr-2021 09:18:11

277 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 array arr, will always be of even length.Our function should return true if and only if it is possible to reorder it such that arr[2 * i + 1] = 2 * arr[2 * i] for every 0 {    const map = arr.reduce((acc, num) => {       acc[num] = (acc[num] || 0) + 1       return acc    }, {});    const keys = Object.keys(map)    .map(key => Number(key))    .sort((a, ... Read More

Index difference of tuples in JavaScript

AmitDiwan
Updated on 09-Apr-2021 09:19:35

150 Views

ProblemWe are required to write a JavaScript function that takes in an array of integers, arr, as the first and the only argument.Suppose two indices, i and j in the array which satisfy the following conditions −i < j, andarr[i] {    let max = 0    const stack = [0]    for (let i = 1; i < arr.length; i++) {       if (arr[i] < arr[stack[stack.length - 1]]) {          stack.push(i)       }    }    for (let i = arr.length - 1; i >= 0; i--) {       ... Read More

Checking for univalued Binary Search Tree in JavaScript

AmitDiwan
Updated on 09-Apr-2021 09:22:00

130 Views

Univalued Binary Search TreeA binary search tree is univalued if every node in the tree has the same value.ProblemWe are required to write a JavaScript function that takes in the root of a BST and returns true if and only if the given tree is univalued, false otherwise.For example, if the nodes of the tree are −const input = [5, 5, 5, 3, 5, 6];Then the output should be −const output = false;ExampleThe code for this will be − Live Democlass Node{    constructor(data) {       this.data = data;       this.left = null;       this.right ... Read More

Finding points nearest to origin in JavaScript

AmitDiwan
Updated on 09-Apr-2021 09:25:12

649 Views

ProblemWe are required to write a JavaScript function that takes in an array of coordinates, arr, as the first argument, and a number, num, as the second argument.Our function should find and return the num closest points to the origin (0, 0).(Here, the distance between two points on a plane is the Euclidean distance.)For example, if the input to the function is −const arr = [[3, 3], [5, -1], [-2, 4]]; const num = 2;Then the output should be −const output = [[3, 3], [-2, 4]];ExampleThe code for this will be − Live Democonst arr = [[3, 3], [5, -1], [-2, ... Read More

Sum which is divisible by n in JavaScript

AmitDiwan
Updated on 09-Apr-2021 09:26:33

228 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers, arr, as the first argument, and a number, num, as the second argument. Our function should return the number of (contiguous, non-empty) subarrays that have a sum divisible by num.For example, if the input to the function is −const arr = [4, 5, 0, -2, -3, 1]; const num = 5;Then the output should be −const output = 7;Output ExplanationThere are 7 subarrays with a sum divisible by 5 −[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, ... Read More

Advertisements