Sum without highest and lowest number help

Right,. just storing min and max in a variable as you iterate the array.

2 Likes

@sethstephanz you don’t need the word function in front of that. It’s not a function. It’s an array with a method called .sort() with a anonymous function inside.

1 Like

Scroll back up and look at my example.

1 Like

Ok, I did:

function sumArray(array) {
  let sortArray = sumArray.sort(function(a, b){return a-b});//sorts array in ascending order
	let sliceArray = sortArray.slice([1],[-1]);//slices off ends of array
	let sum = sliceArray.reduce((total, amount) => total + amount);//add remaining array together
	return sum;
}

but it’s throwing

TypeError: sumArray.sort is not a function
    at sumArray
    at /home/codewarrior/index.js:16:23
    at /home/codewarrior/index.js:17:5
    at Object.handleError
        <anonymous>

Ok, got

function sumArray(array) {
  const sortArray = array.sort(function(a, b){return a-b});//sorts array in ascending order
	let sliceArray = sortArray.slice([1],[-1]);//slices off ends of array
	let sum = sliceArray.reduce((total, amount) => total + amount);//add remaining array together
	return sum;
}

which passes the sample test, but when I attempt to submit it, it throws:

TypeError: Cannot read property 'sort' of null
    at sumArray
    at /home/codewarrior/index.js:16:19
    at /home/codewarrior/index.js:54:5
    at Object.handleError
        <anonymous>

At this point., it really wouldn’t help for you to discover the solution. You really need to go over how methods work.

let sortArray = sumArray.sort(function(a, b){return a-b})

.sort() is a method and you’re trying to use it on a function sumArray which does not exist in the current scope. Which is why you’re getting an error.

.sort() only works on arrays.

1 Like

When you see this error you should think this:

null.sort(...)

It’s because your array value is null.

Hmm, I guess I’m kind of lost then.

Regarding null, the exercise does say:

If array is empty, null or None, or if only 1 Element exists, return 0.
Note:In C++ instead null an empty vector is used. In C there is no null. ;-) 


-- There's no null in Haskell, therefore Maybe [Int] is used. Nothing represents null.

Do you know of any sources in particular I should review?

Right… so you should check array value and if it’s null first,. and if it is return 0;

Yeah., this site teaches all this stuff… Just start from beginning.

And this is actually multiple conditions:
If array is empty, null or None, or if only 1 Element exists, return 0;

‘’, null, None in javascript is simply checking if its value is false.

I’ve already worked through FCC’s basic JS course. Is there some fundamental concept that seems to be of issue, or does it just seem to be an issue with syntax?

There’s an advance JS course too.