Question on 'Where do I belong?'

I was working on my solution and I got very close to Solution 1. However, I was not getting the correct result because I was using ‘i’ in the for loop and not ‘a’.
Can someone explain to me why we use ‘a’ and not ‘i’?

I understand the function in sort() can be any other characters (so say, (d,e) and it would sort the same and then we return d), just wondering if anyone can point me in the right direction, to a reference for understanding this.

Thank you.

It is really impossible to say what is happening without looking at your code. Can you please post your code here?

Currently, my code looks exactly like Solution 1.

function getIndexToIns(arr, num) {
  arr.sort(function(a, b) { return a - b;});

  for (var a = 0; a < arr.length; a++) {
    if (arr[a] >= num) return a;
  return arr.length;

Except instead of ‘a’ I was using ‘i’.
So the problem is solved. I just don’t understand fundamentally why we use ‘a’ and not ‘i’

a is a bit of an unconventional choice, but the code passes fine for me with a instead of i.

Are you asking why the convention is to use i as the default iteration variable in a loop?

(Side note, at this point you shouldn’t be using var anymore. Use const wherever you can, let if you need to, and never var.)

Oh! They used a. Yea, that is strange. I’ll update it.

no, i understand why we use ‘i’ and how that can also be any character. i dont know why im struggling to grasp why we use ‘a’ in the for loop, same letter as the function in sort. why MUST those letters be the same.

Sorry if this question is silly. I’m just really trying to UNDERSTAND JavaScript fully. not just get the cert.

and by the way, yes in my code I use let and var, but thank you.

The letters don’t have to be the same. The solution author made a strange choice.

1 Like

ok. that’s interesting. well, I’m glad it wasnt just me.

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