I have an array with some values, and I want to find the minimum value of that array in order to print out the index that this value has. In this array, one of the values is 0. My guess is that in order to find the index, we must iterate through this array find the minimum, but not the 0, and return the index. Can you help me understand what I am doing wrong with the iterations? I cannot find the minimum number.
This is the array:
[ 10, 5, 6, 5.5, 3.75, 0, 4.25, 3, 5.5, 6.75, 8, 9.25, 4, 15, 4.25, 6, 6, 4.75, 3.75 ]
I am currently stuck here:
var smallest = 0
var biggest = 0
for (let i = 0; i < merged.length; i ) {
if (merged[i] > biggest && merged[i] != 0) {
biggest = merged[i];
} else merged[i] < smallest ? smallest = merged[i] : smallest = merged[i];
console.log('the biggest is', biggest, 'in the iteration', i)
console.log('the smallest is', smallest, 'in the iteration', i)
}
console.log('min-> : ', smallest, biggest);
That gives this:
the biggest is 10 in the iteration 0
the smallest is 0 in the iteration 0
the biggest is 10 in the iteration 1
**the smallest is 5 in the iteration 1**
the biggest is 10 in the iteration 2
**the smallest is 6 in the iteration 2**
the biggest is 10 in the iteration 3
the smallest is 5.5 in the iteration 3
the biggest is 10 in the iteration 4
the smallest is 3.75 in the iteration 4
the biggest is 10 in the iteration 5
the smallest is 0 in the iteration 5
the biggest is 10 in the iteration 6
the smallest is 4.25 in the iteration 6
the biggest is 10 in the iteration 7
the smallest is 3 in the iteration 7
the biggest is 10 in the iteration 8
the smallest is 5.5 in the iteration 8
the biggest is 10 in the iteration 9
the smallest is 6.75 in the iteration 9
the biggest is 10 in the iteration 10
the smallest is 8 in the iteration 10
the biggest is 10 in the iteration 11
the smallest is 9.25 in the iteration 11
the biggest is 10 in the iteration 12
the smallest is 4 in the iteration 12
the biggest is 15 in the iteration 13
the smallest is 4 in the iteration 13
the biggest is 15 in the iteration 14
the smallest is 4.25 in the iteration 14
the biggest is 15 in the iteration 15
the smallest is 6 in the iteration 15
the biggest is 15 in the iteration 16
the smallest is 6 in the iteration 16
the biggest is 15 in the iteration 17
the smallest is 4.75 in the iteration 17
the biggest is 15 in the iteration 18
the smallest is 3.75 in the iteration 18
min-> : 3.75 15
As you can see above, the smallest should not change from 5
to 6
. The minimum should be 3
.
Thank you very much.
Question solved, thank you all for your time and help !!
CodePudding user response:
Simply change your code to the following,
var smallest = 0;
var biggest = 0;
var merged = [ 10, 5, 6, 5.5, 3.75, 0, 4.25, 3, 5.5, 6.75, 8, 9.25, 4, 15, 4.25, 6, 6, 4.75, 3.75 ];
for (let i = 0; i < merged.length; i ) {
if (merged[i] > biggest && merged[i] != 0) {
biggest = merged[i];
}
if(smallest == 0 || (merged[i] < smallest && merged[i] != 0)) {
smallest = merged[i];
}
console.log('the biggest is', biggest, 'in the iteration', i)
console.log('the smallest is', smallest, 'in the iteration', i)
}
I assumed you no need to check the index of 0 valued item in the array.
CodePudding user response:
Regarding your failed function, it is reporting the current iteration but it appears it doesn't keep track of each iteration or better yet it should carry the most minimal number in each iteration.
Spread the array with
...
operator.filter()
array straight through and it will return truthy values which excludes 0.Then use
Math.min()
Finally, get the index with
.indexOf()
let data = [ 10, 5, 6, 5.5, 3.75, 0, 4.25, 3, 5.5, 6.75, 8, 9.25, 4, 15, 4.25, 6, 6, 4.75, 3.75 ];
let out = data.indexOf(Math.min(...data.filter(n => n)))
console.log(out);
CodePudding user response:
You can first find the min without zero then find the index using the following, this can be changed to also find the maximum index.
const arr = [ 10, 5, 6, 5.5, 3.75, 0, 4.25, 3, 5.5, 6.75, 8, 9.25, 4, 15, 4.25, 6, 6, 4.75, 3.75 ]
const minIndex = arr.indexOf(Math.min.apply(null, arr.filter(Boolean)))
console.log(minIndex)
Here's an iterative approach incase it is clearer:
const findMin = (arr) => {
let min;
for(let i =0; i < arr.length; i ){
if(!min && arr[i]!==0) min = arr[i]
if(arr[i] < min && arr[i]!==0) min = arr[i]
}
return arr.indexOf(min)
}
const arr = [ 10, 5, 6, 5.5, 3.75, 0, 4.25, 3, 5.5, 6.75, 8, 9.25, 4, 15, 4.25, 6, 6, 4.75, 3.75 ]
console.log(findMin(arr))
CodePudding user response:
You can scan the array only once to find the minimum as well as the index.
var merged = [
10, 5, 6, 5.5, 3.75, 0, 4.25, 3, 5.5, 6.75, 8, 9.25, 4, 15, 4.25, 6, 6, 4.75,
3.75,
];
let [minVal, minIndex] = merged.reduce(
function ([minVal, minIndex], currentVal, currentIndex) {
if (currentVal!= 0 && minVal > currentVal) {
minVal = currentVal;
minIndex = currentIndex;
}
return [minVal, minIndex];
},
[Infinity, -1]
);
console.log(minVal, minIndex);
Also, it works.
var merged = [
10, 5, 6, 5.5, 3.75, 0, 4.25, 3, 5.5, 6.75, 8, 9.25, 4, 15, 4.25, 6, 6, 4.75,
3.75,
];
var smallest = Infinity;
for (let i = 0; i < merged.length; i ) {
if (merged[i] < smallest && merged[i] != 0) {
smallest = merged[i];
}
console.log("the smallest is", smallest, "in the iteration", i);
}
console.log("min-> : ", smallest);