-3

Test array: [2, 3, 6, 7, 10, 1]; . Expected value on function return: 4 .

I did it like that but it's returning the value to me. I need the highest value index :

 function highestNumber(array) {
  
  let biggerNumber = 0;
  for (let index = 0; index < array.length; index += 1) {
    
    if (array[index] > biggerNumber) {
      biggerNumber = array[index]
    }
  }
  return biggerNumber;
}

highestNumber([2, 3, 6, 7, 10, 1]);
Andrey
  • 1
  • 2
  • Declare a variable to store the index of the biggest number found and return it after looping – sourcloud Oct 24 '21 at 01:16
  • But remember that copy and pasting code will never help you learn. Figure out how to do it with a pen and paper, then implement it in code – user202729 Oct 24 '21 at 01:22
  • when i do this it returns me undefined. I don't know what I'm doing wrong – Andrey Oct 24 '21 at 01:23

1 Answers1

0

You can do it like this:

function highestNumber(array) {
  let returningIndex = 0;
  for (let index = 0; index < array.length; index += 1) {
    if (array[index] > array[returningIndex]) {
      returningIndex = index;
    }
  }
  return returningIndex;
}

highestNumber([2, 3, 6, 7, 10, 1]);
Veselin Kontić
  • 1,638
  • 2
  • 11
  • 23