Slice.call won't make object into an array

I have a problem when trying to convert an object into an array. Here arr is an object, and myArr and myOtherArr are supposed to be arrays, but are still objects after using slice.call on them.

What am I doing wrong? I’m doing exactly what the MDN slice.call() page is telling us to do.

Code:

`
function sumAll(arr) {
// on input arr is an object

// myArr is supposed to be an array
var myArr = Array.prototype.slice.call(arr,0);

// myOtherArr is alsosupposed to be an array
var myOtherArr = arr.slice.call(arr,0);

// line below returns NaN because arr is an object
var mini = Math.min(arr);

return arr;
}

sumAll([1, 4]);
`

I am a little confused about what you want to return.
You are passing in an array (not an object) with the [1, 4]. (and object would be something more like {val1: 1, val2: 4}
Your NaN result is because Math.min() does not accept arrays as input but a list of arguments.
I’ll assume you want Math.min to return the lowest value in your array. You might want to look at using something like Math.min.apply().

2 Likes

I’ll check it out.

If that’s what we’re supposed to use, why have we never been taught about it?

Yes the ultimate goal is to get the minimum of what is supposed to be an array. But when I output typeof arr, it says object.

I tried Math.min.apply() and the visualisation tool crashes no matter how I use it.

you need to order the object first than get the last or the first depending of the order…