Challenge Insert an Element into a Max Heap is not evaluating code correctly

Tell us what’s happening:
Describe your issue in detail here.
Completed challenge Insert an Element into a Max Heap and created a heap that satisfied the max heap property but for some reason I still fail the last test case

The insert method should add elements according to the max heap property.<<<

I’m not sure if it is evaluating the heap produced against a specific max heap but the one I produce

[ null, 800, 51, 700, 32, 50, 100 ]

Does satisfy the max heap property

  **Your code so far**

var MaxHeap = function() {
// Only change code below this line
this.heap = [null];

this.insert = (ele) => {
  var arr = [...this.heap];
  arr.push(ele);
  var idx = arr.length - 1;
  while(idx > 1 && arr[Math.floor(idx/2)] < ele) {
    arr[idx] = arr[Math.floor(idx/2)];
    arr[Math.floor(idx/2)] = ele;
    idx = Math.floor(idx/2);
  }
  this.heap = arr;
}

this.print = () => {
  console.log(this.heap);
} 
// Only change code above this line
};
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36

Challenge: Insert an Element into a Max Heap

Link to the challenge:

Finally, add a print method which returns an array of all the items that have been added to the heap.

Your print isn’t quite right.

Yeah, changed it to to return this.heap and it works

That’s my fault for not reading I guess

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.