Tell us what’s happening:
Step1: I first sort the array to fetch the min(1st elem) and the max(last elem) of the array
Step2: I then traverse through the 1st and the Last(min & max) to get all the numbers pushed to a new array
Step3: perform a reduce method to get the sum of all the elements in the new array.
Can I consider this as the good method for solving this problem?
Your code so far
function sumAll(arr) {
arr.sort(function(a,b){
return a-b;
});
arr1=[];
for(var i=arr[0];i<=arr[arr.length-1];i++){
arr1.push(i);
}
return arr1.reduce(function(prev,curr){
return prev+curr;
});
}
sumAll([1, 4]);
Your browser information:
Your Browser User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36
.
Link to the challenge: