Javascript Articles

Page 14 of 534

Checking for univalued Binary Search Tree in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 171 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 −class Node{    constructor(data) {       this.data = data;       this.left = null;       this.right = ...

Read More

Validating push pop sequence in JavaScript

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

Making array unique in JavaScript

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

Read More

Checking for centrally peaked arrays in JavaScript

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

Finding the nth element of the lucas number sequence in JavaScript

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

Read More

Converting to hex and summing the numeral part in JavaScript

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

Read More

Checking for a Doubleton Number in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 342 Views

Doubleton NumberWe will call a natural number a "doubleton number" if it contains exactly two distinct digits. For example, 23, 35, 100, 12121 are doubleton numbers, and 123 and 9980 are not.ProblemWe are required to write a JavaScript function that takes in a number and return true if it is a doubleton number, false otherwise.ExampleFollowing is the code −const num = 121212; const isDoubleTon = (num = 1) => {    const str = String(num);    const map = {};    for(let i = 0; i < str.length; i++){       const el = str[i];       if(!map.hasOwnProperty(el)){ ...

Read More

Converting number of corresponding string without using library function in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 386 Views

ProblemWe are required to write a JavaScript function that takes in a number n and converts it to the corresponding string without using the inbuilt functions String() or toString() or using string concatenation.ExampleFollowing is the code −const num = 235456; const convertToString = (num) => {    let res = '';    while(num){       res = (num % 10) + res;       num = Math.floor(num / 10);    };    return res; }; console.log(convertToString(num));OutputFollowing is the console output −235456

Read More

Finding quarter based on month index in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 662 Views

ProblemWe are required to write a JavaScript function that takes in the 1-based month index and return the quarter, which the month falls in.ExampleFollowing is the code −const month = 7; const findQuarter = (month = 1) => {    if (month

Read More

Finding whether a number is triangular number in JavaScript

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 458 Views

Triangular NumberTriangular number is the number of points that can fill an equilateral triangle.For instance − 9 is a triangular number which makes an equilateral triangle with each side of 4 units.ProblemWe are required to write a JavaScript function that takes in a number and returns true if its a triangular number, false otherwise.ExampleFollowing is the code −const num = 9; const isTriangular = (num = 1) => {    let i = 4;    if(num === 1){       return true;    };    if(num === 3){       return true;    };    while(((3 * 1) - 3)

Read More
Showing 131–140 of 5,339 articles
« Prev 1 12 13 14 15 16 534 Next »
Advertisements