Sum All Numbers in a Range Glitch?

Hi, I believe I have a valid solution for the Sum All Numbers in a Range problem, but the solution will not pass. I was wondering if there is a known issue with this or perhaps an issue with my code that I do not see??

I have two versions, one for es6 (original) and one for es5 (since fcc does not seem so compatible with es6)

Thanks!

ES6 Solution:

var finalArray = [];

function sumAll(arr) {
	Math.max(...arr);

	let max = Math.max(...arr);

	let min = Math.min(...arr);



	while (max >= min) {

		finalArray.push(max);
		max--;
		console.log('Number pushed =,' + max + 'final array,' + finalArray);
		
	}
	
	const reducer = (accumulator, currentValue) => accumulator + currentValue;
	return finalArray.reduce(reducer);
}
sumAll([1, 4]);

ES5 Solution:

var finalArray = [];

function sumAll(arr) {


	var max =Math.max.apply(null, arr);

	var min = Math.min.apply(null, arr);



	while (max >= min) {

		finalArray.push(max);
		max--;
			
	}
	
  var sum = finalArray.reduce(function (accumulator, currentValue) {
  return accumulator + currentValue;
}, 0);
  
	
	return sum;
}
sumAll([4, 1]);

I've read two posts about unexpected behaviour and in both of them the culprit was the use of global variables.

The importance of declaring the variable inside a function for some cases is amazing.

1 Like