After many comments, the problem was up to another. As the question had repercussion I will try to give an answer that is what you need.
Need to synchronize the index that is along with the highest value found in another variable, because it is data that needs to be maintained and walk together.
function retornaIndiceMaiorValor() {
let maior = array[0];
let indice = 0;
for (let i = 1; i < array.length; i++) {
if (array[i] > maior) {
maior = array[i];
indice = i;
}
}
return indice;
}
let array = [2, 3, 6, 7, 10, 1];
console.log(retornaIndiceMaiorValor(array));
I put in the Github for future reference.
Note that you can find the index after knowing which is the highest with indexOf()
, but then I think it would only be worth it if I used the max()
to find the biggest.
To learn the basic errors you had in the original code is the answer below. The question borders to be off topic because he was in need of what he actually needed, he’s the one XY problem, if it is to answer completely, the doubt was about the undefined
.
The code makes no sense and has several errors.
- The variable initialization must be outside the loop, otherwise the largest will always be 0.
- You must close the loop until
i
enough in the size of array, and not if i
is smaller than thearray, which is not a number, is a complex object with multiple data inside, so take the right property.
- And it should close before it reaches the end because the array starts at zero, it cannot pick up the index that is equal to the size because this index does not exist, it is like having 10 digits, goes from 0 to 9, there is no 10.
- There compares to
array[i]
is greater than array[i]
, what is obviously not since it is equal, has no different, perhaps the creation of the unnecessary variable has confused. Interestingly writing right you can read easy because you have to buy with maior
even.
- I used 0 to stay in the original rule, but if I allowed negative values then I would have to take the lowest possible negative value or start by taking an initial element to compare. This code works for these numbers, not as a general rule.
- I changed the name of the function to indicate clearly what it does, if I wanted to return the index of the highest value then the name should be
retornaIndiceMaiorValor()
and then the logic would be different because it would have to control which is the index of the highest value also.
- The rest I improved to simplify and organize.
function retornaMaiorValor() {
var maior = 0;
for (let i = 0; i < array.length; i++) if (array[i] > maior) maior = array[i];
return maior;
}
let array = [2, 3, 6, 7, 10, 1];
console.log(retornaMaiorValor(array));
I put in the Github for future reference.
I considered that this is a case where the person is learning to do so, it is a coding exercise, and as it has several problems in the code it would be important to show how to do the same code, but correctly. In codes in production a simpler and more ready code is more interesting, Augusto demonstrated this form.
is not
array.length
has several problems– novic
Catch the biggest Dice or catch the biggest Value?
– Guilherme Nascimento
I wanted to get the highest index, the position say, has the vector for example [0, 1, 2], I wanted it to return the index 2 that would be the highest.
– Saul44
@Saul44 If the array is
[10, 20, 30]
, the largest element is 30 and the highest element index is 2. Which of these results do you want? (in his example it was not clear why the indices are equal to the values)– hkotsubo
As I said, Indice is one thing, value is another, by your example you want the highest value, Indice is something else.
– Guilherme Nascimento
@hkotsubo I want the content of the largest element please excuse me I formulated my question incorrectly.
– Saul44
@Saul44 the Dice of this array
[10, 30, 400]
are 0, 1 and 2 ... the values are 10, 30, 400, the value 10 is in Dice 0, the value 30 is in Dice 1 and the value 400 in Dice 3. Comprises?– Guilherme Nascimento
So 10 is at 0, 30 at 1, and 400 at 2, wouldn’t that be it? @Guilhermenascimento ?
– Saul44
That Saul, then the title of the question speaks take the biggest Dice, but the answers return you the greatest value, there is the doubt, you want to take the biggest Dice (from 0 to 5 in your example, the result would be 5) or you want to take the Dice whose value is the highest (in your example the Dice with the highest value is the Dice 4, which has the value 10) or you want to actually get the highest value (which has nothing to do with indices)?
– Guilherme Nascimento
I want to take the index, in a vector with 5 elements for example, return the value 4, since it goes from 0 to 4.
– Saul44
@Saul44 I think I got it, but this definition I just gave is very ambiguous yet.
– Maniero
You understood my question, there were 3 situations, the Dice falls into two, two are about taking Dice, what changes is the condition to catch such Dice, then I repeat again: You want to take the Dice with the highest VALUE (in YOUR example
[2, 3, 6, 7, 10, 1]
the highest value Index is the4
, for the value is10
)? Or actually take the "biggest" Dice (which in your case is the last ever[2, 3, 6, 7, 10, 1]
, in this case SPECIFIC would sufficelet array = [2, 3, 6, 7, 10, 1]; let maior = array.length - 1; console.log(maior);
)? Which of the two?– Guilherme Nascimento
The highest value in the case of what returns with array.lenght -1, that’s right. In the case you said it would be 4.
– Saul44
Thank you, I expressed myself badly, I will endeavor to in the next doubt explain in an understandable way.
– Saul44