Learn Basic Algorithmic Thinking by Building a Number Sorter - Step 15

So, I’m a bit confused.

How does the program known what “i” and “num” represent and how to use them?
It feels like the program assumes “i” is the index of each item in the array and “num” is the value, but I wonder how that is.

const updateUI = (array = []) => {
  array.forEach((num, i) => {
    const outputValueNode = document.getElementById(`output-value-${i}`);
    outputValueNode.innerText = num;
  })
}

The reason that num is the value and i is the index is because the way forEach is defined. The code that makes forEach work is expecting a callback function that takes three parameters. The first two are the element and the index of it. So the forEach passes this info to the callback.

1 Like

Ahh…it’s a prepended thing. Gotcha.