I tested your code with the string ‘aadb’ which I assume based on your topic title should return ‘d’. This call creates an infinite loop. So you can see what the values of various variables are in your code, I added console.log statements (see below).
function firstNotRepeatingCharacter(s) {
for (let i = 0; i < s.length; i++) {
let repeat = 0;
console.log('i='+i);
for (let j = 0; i < s.length; j++) {
console.log('j='+j);
console.log('s[i]=' + s[i] + ' s[j]=' + s[j]);
console.log('i != j is ' + (i != j) + ', s[i] == s[j] is ' + (s[i] == s[j]));
if (i != j && s[i] == s[j]) {
repeat = 1;
break;
}
}
if (repeat == 0) {
return s[i];
}
console.log('repeat = ' + repeat + '\n');
}
return '';
}
firstNotRepeatingCharacter('aadb');
As you can see below, when the code above runs, once i = 2, j keeps getting larger and your repeat never gets set to 0, which is your exit condition. I only posted up to i=2 and j=5, because it just keeps repeating the same after that.
i=0
j=0
s[i]=a s[j]=a
i != j is false, s[i] == s[j] is true
j=1
s[i]=a s[j]=a
i != j is true, s[i] == s[j] is true
repeat = 1
i=1
j=0
s[i]=a s[j]=a
i != j is true, s[i] == s[j] is true
repeat = 1
i=2
j=0
s[i]=d s[j]=a
i != j is true, s[i] == s[j] is false
j=1
s[i]=d s[j]=a
i != j is true, s[i] == s[j] is false
j=2
s[i]=d s[j]=d
i != j is false, s[i] == s[j] is true
j=3
s[i]=d s[j]=b
i != j is true, s[i] == s[j] is false
j=4
s[i]=d s[j]=undefined
i != j is true, s[i] == s[j] is false
j=5
s[i]=d s[j]=undefined
i != j is true, s[i] == s[j] is false