Question: I wrote this function, which returns `-1`.

```js
function sayHello() {
  let arrayEdificiosNiveis = [11,10,10];
  var indexMenor = arrayEdificiosNiveis.indexOf(Math.max(arrayEdificiosNiveis));
  console.log(indexMenor);
}
sayHello();
```

I expected 0, not -1. How do I solve this problem?

Answer: 
The problem in your approach is the complete array you're passing on the function `Math.max`:

```js
const arrayEdificiosNiveis = [11, 10, 10],
  max = Math.max(...arrayEdificiosNiveis);

console.log(arrayEdificiosNiveis.findIndex(elem => elem === max));
```

If it's not possible to use `Spread syntax`, you can call the function `apply` which receives an array as the parameters of function `Math.max`

```js
function sayHello() {
  const arrayEdificiosNiveis = [11, 10, 10],
    max = Math.max.apply(null, arrayEdificiosNiveis),
    indexMenor = arrayEdificiosNiveis.indexOf(max);
  console.log(indexMenor);
}
sayHello();
```
You can ask [Raymond Chen](https://devblogs.microsoft.com/oldnewthing/20140526-00/?p=903). It turns out the obvious C-style way is the best. Perhaps unsurprisingly - C style code is easy for Javascript engines to optimise.

```
function indexOfSmallest(a) {
 var lowest = 0;
 for (var i = 1; i < a.length; i++) {
  if (a[i] < a[lowest]) lowest = i;
 }
 return lowest;
}

```