JavaScript Array Min Value Index

48 Views Asked by At

It give me correct index when i give fixed array i.e

BT_input = [11, 10, 10, 14]

But when i get array values from user this Return -1 instead of correct index ?

<html>
<body>
<script>
const BT_input = [];
var size = 3;
 for(var a=0; a<=size; a++) 
{
      BT_input[a] = prompt('Enter Brust Time of P ' + (a));
}
function sayHello() {
  var indexMenor = BT_input.indexOf(Math.min(...BT_input));
  
  document.write(indexMenor);
}

sayHello();

</script>
</body>
</html>
1

There are 1 best solutions below

1
On

you need to translate the string to number

like this

const BT_input = [];
var size = 3;
 for(var a=0; a<size; a++) 
{
      BT_input[a] = Number(prompt('Enter Brust Time of P ' + (a)));
}
function sayHello() {
  var indexMenor = BT_input.indexOf(Math.min(...BT_input));
  
  document.write(indexMenor);
}

sayHello()