1
var nthUglyNumber = function (n) {3
let uglySet = new Set(); // Set to keep track of all the ugly Numbers to stop repetition5
let minHeap = new MinPriorityQueue(); // Javascript provides inbuilt min and max heaps, constructor does not require callback function for primitive types but a comparator callback for object data types, to know which key corresponds to the priority6
//callback looks like this new MinPriorityQueue((bid) => bid.value)7
// if this is confusing, check the documentation here8
// https://github.com/datastructures-js/priority-queue/blob/master/README.md#constructor10
if (!uglySet.has(uglyNo * 2)) {11
// add only if the set does not have this ugly no.12
minHeap.enqueue(uglyNo * 2, uglyNo * 2); // enqueue takes two inputs element and priority respectively, both are same here13
uglySet.add(uglyNo * 2);15
if (!uglySet.has(uglyNo * 3)) {16
minHeap.enqueue(uglyNo * 3, uglyNo * 3);17
uglySet.add(uglyNo * 3);19
if (!uglySet.has(uglyNo * 5)) {20
minHeap.enqueue(uglyNo * 5, uglyNo * 5);21
uglySet.add(uglyNo * 5);23
uglyNo = minHeap.dequeue().element; //dequeue returns an object with two properties priority and element