Basic Algorithm Scripting - Where do I Belong

function getIndexToIns(arr, num) {
arr.sort((a, b) => a - b);

for (let i = 0; i < arr.length; i++) {
if (arr[i] >= num) return i;
}

return arr.length;
}

I understand that arr[i] represents a number in an iteration. Please what does i stand for. Thanks in anticipation

i represents the index of an element in the array.

arr[i] represents an element of the array. It could be a any data type. In this case, it is a number.

This was helpful. Thanks a bunch!!!

Another question please. If i represent the index of a number in an iteration, why then do we still have to return arr.length since it is the index we are looking for which i represents?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.