heapMax remove element

Tell us what’s happening:
I don’t get it why it is not passing and everytime unexpected result
please give it review

Your code so far


var MaxHeap = function() {
this.heap = [null];
this.insert = (ele) => {
  var index = this.heap.length;
  var arr = [...this.heap];
  arr.push(ele);
  while (ele > arr[Math.floor(index / 2)] && index > 1) {
    arr[index] = arr[Math.floor(index / 2)];
    arr[Math.floor(index / 2)] = ele;
    index = Math.floor(index / 2);
  }
  this.heap = arr;
}
this.print = () => {
  return this.heap.slice(1);
}
// Only change code below this line
  this.heapify=(heap)=>{
    var index=heap.length-1;
    for (let i=1;i<=index;i++){
      while (heap[i] >heap[Math.floor(i/2)] && heap[Math.floor(i/2)]!=null){
        let temp=heap[i]
        heap[i]=heap[Math.floor(i/2)];
        heap[Math.floor(i/2)]=temp;
        // index=Math.floor(index/2);
      } 
    }
    
    return heap
  }
  this.remove=()=>{
    this.heap[1]=this.heap[this.heap.length-1];
    this.heap.splice(this.heap.length-1,1)
    // console.log(this.heap)
    return this.heapify(this.heap)
  }
// Only change code above this line
};
let exp=new MaxHeap()
exp.insert(10)
exp.insert(50)
exp.insert(40)
exp.insert(80)
exp.insert(100)
exp.insert(60)
exp.insert(90)
exp.insert(500)
exp.insert(9)
exp.remove()
console.log(exp.heap)

Your browser information:

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

Challenge: Remove an Element from a Max Heap

Link to the challenge:

You’re using the wrong formula for finding the child index in heapify(). You need to use (i*2) + 1 and (i*2) instead of i/2.
I wrote it by finding the greater child and swapping it or swapping it with either child if they’re equal.

Also, remove can just return the root node before it’s heapify()'d