Javascript or Jquery sum of highest 5 values : Please do not mark this question as **DUPLICATE**. I am really facing the problem.

I have an array of 6 values where 6th value is optional (i.e. if user does not input 6th value, the first 5 values will be calculated). I want to sum highest 5 values of them.

My Javascript Code:

    function calculate_merit_point(){

       var s1 = eval(document.getElementById('sub_no1').value);
	   var s2 = eval(document.getElementById('sub_no2').value);
	   var s3 = eval(document.getElementById('sub_no3').value);
	   var s4 = eval(document.getElementById('sub_no4').value);
	   var s5 = eval(document.getElementById('sub_no5').value);
	   var s6 = eval(document.getElementById('sub_no6').value);
	  
	   var vals = [s1,s2,s3,s4,s5,s6];
	
	  function arraySum(arr) {
		  if (!arr) {
			  return false;
		  } else {
			  var sum = 0;
			  for (var i = 0, len = arr.length; i < len; i++) {
				  sum += arr[i];
			  }
			  return sum;
		  }
	  }

	
	  sum = arraySum(vals.sort(function(a, b) {
		  return b - a;
	  }).slice(0, 5));
	
			
	  if(isNaN(tt)){
		  $('#agr').text('0');
	  } else {
		  $('#agr').text(sum);
	  }
	
	
    }


Now suppose

    s1 = 30
    s2 = 31
    s3 = 32
    s4 = 33
    s5 = 34
    s6 = 35

It should be `31+32+33+34+35 = 165`. but it is displaying the value 162.

As per my requirement (6th value optional), if I do not give any value to `s6`, it is displaying the value **228**.

I have tried [This][1], but if I do not give the 6th (optional) value, it is showing the value `0`. If I give the value 35 in s6, it is showing sum value 233.

**What should I do ?**


  [1]: http://stackoverflow.com/questions/12019803/javascript-sum-of-highest-6-values-in-an-array