Quantcast
Channel: Smallest number in array and its position - Stack Overflow
Browsing all 8 articles
Browse latest View live

Answer by sporkl for Smallest number in array and its position

Find the smallest value using Math.min and the spread operator:var minimumValue = Math.min(...temp);Then find the index using indexOf:var minimumValueIndex = temp.indexOf(minimumValue);I personally...

View Article



Answer by Apollo for Smallest number in array and its position

You could use reduce, and compare against Infinity.let minIndex = -1;arr.reduce((acc, curr, index) => { if (curr < acc) { minIndex = index; return curr; } else { return acc; }}, Infinity);

View Article

Answer by EugenSunic for Smallest number in array and its position

Here is a solution using simple recursion. The output is an object containing the index position of the min number and the min number itselfconst findMinIndex = (arr, min, minIndex, i) => { if...

View Article

Answer by Raku Zeta for Smallest number in array and its position

See this answer of "find max" version of this question. It is simple and good. You can use the index to get the element afterward.

View Article

Answer by Tony for Smallest number in array and its position

One-liner:alist=[5,6,3,8,2]idx=alist.indexOf(Math.min.apply(null,alist))

View Article


Answer by MattDiamant for Smallest number in array and its position

You want to use indexOfhttp://www.w3schools.com/jsref/jsref_indexof_array.aspUsing the code that you had before, from the other question:temp = new Array();temp[0] = 43;temp[1] = 3;temp[2] =...

View Article

Answer by Guffa for Smallest number in array and its position

Just loop through the array and look for the lowest number:var index = 0;var value = temp[0];for (var i = 1; i < temp.length; i++) { if (temp[i] < value) { value = temp[i]; index = i; }}Now value...

View Article

Smallest number in array and its position

What I am trying to achieve is to find smallest number in array and its initial position. Here's an example what it should do:temp = new Array();temp[0] = 43;temp[1] = 3;temp[2] = 23;So in the end I...

View Article

Browsing all 8 articles
Browse latest View live




Latest Images