Sorting array and inserting value at specific location

by Akram kamal

HTML

<script src="http://code.jquery.com/jquery-1.11.0.js"></script>

JavaScript

ages = [0, 1, 4, 9, 11, 13, 0, 0, 0]

function insert(val, a) {
  var off = 0
  var aLength=a.length
  for (var i=0;i<aLength;i++){
    if (val<a[i] || a[i]===0 && i>0){
    	a.splice(i,0,val)
      a.length=aLength
		  return a
    }
    
  }
}
console.log(ages.join(","));
a=insert(10, ages)
console.log(ages.join(","));
a=insert(15, ages)
console.log(ages.join(","));

/* How can I end up with just one zero on the end so the finished array looks like this and slice a zero off the end */