Sum without highest and lowest number help

Hi. I need some help with this kata (CodeWars challenge) in JavaScript. Basically, it wants you to reorder a given array, pop and shift off the highest and lowest, then add the remaining elements together. The challenge can be found here: https://www.codewars.com/kata/576b93db1129fcf2200001e6/train/javascript.

I just need some help figuring out what’s going wrong. Here’s the code I have so far:

function sumArray(array) { //get input
  var numArray = array.sort((a, b) => a - b); //rearrange array order from min to max
  var poppednumArray = numArray.pop(); //pop and shift
  var shiftedArray = poppedArray.shift(); //return array -pop and -shift
  var i;
  var s = 0;
  for (i = 0; i < shiftedArray.length; i++){
    s = s + [i];//add elements together = sum
  }//ends for loop
  return shiftedArray; //return sum
}

Currently, it’s telling me that I don’t have some variables declared.

ReferenceError: poppedArray is not defined
    at sumArray
    at /home/codewarrior/index.js:19:23
    at /home/codewarrior/index.js:20:5
    at Object.handleError
        <anonymous>

It seems like I do, and I’ve switched around let/var, defined them on separate lines, to no luck, so I’m a little confused.

Any help would be appreciated. Thanks!

I don’t see a poppedArray too, I see

var poppednumArray = numArray.pop();

poppednumArray

:wink:
Hope this helps :+1:

1 Like

Thanks for the catch, but I fixed it and now it’s throwing a similar error:

TypeError: poppedArray.shift is not a function
    at sumArray
    at /home/codewarrior/index.js:19:23
    at /home/codewarrior/index.js:20:5
    at Object.handleError
        <anonymous>

.shift() is just a method, right? Why does it seem to treat poppedArray() as a new function?

.sort()
.slice()
.reduce()

use those.

1 Like

You didn’t post your “fix” so can’t really say what the issue is.

But the error implies your “poppedArray” variable is not an array.

1 Like

I just fixed the name of that variable and didn’t have much time to mess with it much more until now:

function sumArray(array) { //get input
  var numArray = array.sort((a, b) => a - b); //rearrange array order from min to max
  var poppedArray = numArray.pop(); //pop and shift
  var shiftedArray = poppedArray.shift(); //return array -pop and -shift
  var i;
  var s = 0;
  for (i = 0; i < shiftedArray.length; i++){
    s = s + [i];//add elements together = sum
  }//ends for loop
  return shiftedArray; //return sum
}

is what it ended up looking like.

var poppedArray = numArray.pop();

makes poppedArray a single value.
example:

let myNumbers = [1,2,3,4,5];
let example = myNumbers.pop();
console.log(example); // 1
console.log(myNumbers); // [2,3,4,5];

var shiftedArray = poppedArray.shift(); // error because .shift() is a method for arrays.

1 Like

I guess I should have mentioned that the inputs from the problem are arrays:

Sum all the numbers of the array (in F# and Haskell you get a list) except the highest and the lowest element (the value, not the index!).
(The highest/lowest element is respectively only one element at each edge, even if there are more than one with the same value!)

Example:

{ 6, 2, 1, 8, 10 } => 16
{ 1, 1, 11, 2, 3 } => 6

Those are not Javascript arrays.

But if you follow my advice earlier., you’ll want to use sort,. to get min and max on ends., slice() to get the numbers in middle and reduce to reduce your array into a single value.

1 Like

.shift(), .pop() work too,. I just never use them because they mutate the original value. I stay away from methods that do that.

1 Like

Ok. I’m not sure why it’s phrased like that, then. I followed your suggestion and got:

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

and now it’s throwing

ReferenceError: find_average is not defined
    at /home/codewarrior/index.js:14:10
    at /home/codewarrior/index.js:16:5
    at Object.handleError
        <anonymous>

Ok, I kind of shifted it around a little and got:

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

but it throws:

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

Not sure why you called it “find_average”… You’re not looking for an average.
Don’t ever use var.
slice([0], [-1]) is not correct syntax. You may want to consider researching these methods you’re using.

Mainly .slice(); // im confident if you check it out,. you’ll see how to use it.

function sumArray(arr){
     const sortedArr = arr.sort(function(a, b){
          return a - b;
     });
}

The error you’re pasting suggests your variable is in a TDZ. Check and ensure you have selected Javascript as the language to solve this problem.

1 Like

Sorry, I had two open and got confused. Yeah, they’re both in JS.

Oddly enough,. the .slice([a], [b]) works,. but it’s not usually written that way.

1 Like

Ok, so

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

is what I got. It throws:

/home/codewarrior/index.js:6
function sumArray.sort(function(a, b){return a-b});//sorts array in ascending order
                 ^

SyntaxError: Unexpected token .
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:616:28)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at [eval]:1:1

function sumArray.sort(

Incorrect syntax.

1 Like

What is “O(n) time”?

Subtracting min and max after iterating it once.

1 Like

I don’t understand. I have “function sumArray.sort(…”