- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 6683 Articles for Javascript
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
69 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
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
502 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
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
165 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
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
169 Views
ProblemWe are required to write a JavaScript function that takes in an array of integers, arr, sorted in increasing order.Our function is supposed to return an array of the squares of each number, also sorted in increasing order.For example, if the input to the function is −const arr = [-2, -1, 1, 3, 6, 8];Then the output should be −const output = [1, 1, 4, 9, 36, 64];ExampleThe code for this will be − Live Democonst arr = [-2, -1, 1, 3, 6, 8]; const findSquares = (arr = []) => { const res = [] let left = 0 let right = arr.length - 1 while (left
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
148 Views
ProblemWe are required to write a JavaScript function that takes in an array, arr, as the first and the only argument.The array arr consists of string equations of one of the two following kinds −‘X ===Y’X!==Y’Here, X and Y can be any variables.Our function is supposed to check whether for all the equations in the array, we can assign some number so that all the equations in the array yield true.For example, if the input to the function is −const arr = ['X===Y', 'Y!==Z', 'X===Z'];Then the output should be −const output = false;Output Explanation:No matter what value we choose for ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
92 Views
ProblemWe are required to write a JavaScript function that takes in two numbers, m and n, as the first and the second argument.Our function is supposed to count the number of minimum operations required to reach n from m, using only these two operations −Double − Multiply the number on the display by 2, or;Decrement − Subtract 1 from the number on the display.For example, if the input to the function is −const m = 5; const n = 8;Then the output should be −const output = 8;Output Explanation:Because the operations are −5 → 4 → 8ExampleThe code for this ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
103 Views
ProblemWe are required to write a JavaScript function that takes in an array of literals, arr, as the first argument. The second argument to our function will be a number, num. Our function should count and return the number of subarrays of the array that contains exactly num distinct elements.For example, if the input to the function is −const arr = [12, 15, 12, 15, 18]; const num = 2;Then the output should be −const output = 7;Output ExplanationSubarrays formed with exactly 2 different elements −[12, 15], [15, 12], [12, 15], [15, 18], [12, 15, 12], [15, 12, 15], [12, ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
78 Views
ProblemWe are required to write a JavaScript function that takes in a 2-D array of numbers, arr, as the only argument.The numbers in the array can be −the value 0 which represents an empty cell;the value 1 which represents a fresh tomato;the value 2 which represents a rotten tomato.Every minute, any fresh tomato that is adjacent (4-directionally) to a rotten tomato becomes rotten.Our function is supposed to return the minimum number of minutes that must elapse until no cell has a fresh tomato. If this is impossible, we should return -1 instead.For example, if the input to the function is ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
70 Views
ProblemWe are required to write a JavaScript function that takes in an array of strings, arr, as the first and the only argument.Our function is supposed to return an array of all characters that show up in all strings within the array arr (including duplicates).For example, if a character occurs 2 times in all strings but not 3 times, we need to include that character 2 times in the final answer.For example, if the input to the function is −const arr = ['door', 'floor', 'crook'];Then the output should be −const output = ['r', 'o', 'o'];ExampleThe code for this will be ... Read More