Right,. just storing min and max in a variable as you iterate the array.
@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.
Scroll back up and look at my example.
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.
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.