1

This is the solution to the problem. What I don't understand is why is it not "if ( i > currentMax)? I also don't understand the nature of numbers[i]. I understand we can reference indexes in arrays doing numbers[0], but numbers[i] is confusing me.

function max(numbers) {

  let currentMax = numbers[0];
  for (let i = 0; i < numbers.length; i++) {
    if (numbers[i] > currentMax) {
      currentMax = numbers[i];
    }
  }
  return currentMax;
}
6
  • 1
    Math.max(...numbers) Commented Jul 30, 2018 at 16:29
  • numbers[i] is exactly as numbers[0] in your example. it is inside a for loop where i will become a number from 0 to the max lenght of numbers. so each time the loop will be executed it will be numbers[0], then numbers[1] and so on Commented Jul 30, 2018 at 16:32
  • After re-reading your question, it seems like you need to do some reading up on JavaScript Loops to understand what i is and why it is used the way it is. Commented Jul 30, 2018 at 16:32
  • @mhodges OP has found somewhere this code that does what he wants but he has no clue why... agree with you about the fact that he needs to learn a lot Commented Jul 30, 2018 at 16:34
  • @LelioFaieta Yeah, that's why I posted the link. I misread the question at first, I thought it was asking how to find the max without using a loop. Commented Jul 30, 2018 at 16:35

4 Answers 4

1

numbers[i] refers to the value stored at position i. If you were to use if (i > currentMax) then you would always return the last element, since the last element always has the greatest index.

Don't reinvent the wheel, use Math.max(...numbers).

Sign up to request clarification or add additional context in comments.

1 Comment

Reinventing wheels is sometikes useful, especially when learning how things work.
1

Say you have an array like:

[1, 2, 4, 2]

This starts by setting currentMax numbers[0] which is one. Then it loops through the array one element at a time. If it finds a larger number during that loop — in other words if (numbers[i] > currentMax) then it sets the currentMax that number instead. For example this will happen the second and thirds times through the loop when i equals 2 & 4. But it won't happen the last time through the loop. An easy way to watch this happen is to print some stuff to the console as it runs:

function max(numbers) {

  let currentMax = numbers[0];
  for (let i = 0; i < numbers.length; i++) {
    console.log("i:", i, "element:", numbers[i], "max:", currentMax)
    if (numbers[i] > currentMax) {
      currentMax = numbers[i];
      console.log("new currentMax:", currentMax)
    }
  }
  return currentMax;
}

max([1, 2, 4, 2])

Comments

1

In this case i is an "index" which allows us to iterate over all the positions in the array (and accessing their values). In this case i=0, i=1,..., i=numbers.length,

if (numbers[i] > currentMax) asks if the number stored in the array in the position i is greater than the currentMax value. This guarantees that from the provided array the maximum number is returned.

If you ask if (i > currentMax) you compare the value of the "index" (i) with the value of the currentMax value. This is incorrect if you want to return the greatest value from an array of numbers.

2 Comments

I wouldn't call i a pointer. That could be misleading since it doesn't point to a position in memory; it's just an integer that's used as an index.
@bstrauch24 but its a slightly related concept, and I don't think that this is a matter here
0

Like you said, you can reference indexes in arrays by doing numbers. Instead of hard coding numbers you can use a variable that has a number as a value.

function max(numbers) {
  // get the value in the first place in the array  
  let currentMax = numbers[0];

  // create a variable called i
  // set it to 0
  // loop through, increasing i each time, for as long as i is less than the length of the array
  // the first time through i = 0
  // the second time through i = 1
  // then i = 2
  // ... repeat until the end
  for (let i = 0; i < numbers.length; i++) {
    // get the value from the array at the i place
    // if it is greater than the current max
    if (numbers[i] > currentMax) {
      // then set current max to it
      currentMax = numbers[i];
    }
  }

  // return current max
  return currentMax;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.