I was solving basic algorithm problems, and I got stuck in this code.
The problem was to get string data and remove duplicated charactors. For instance, input: 'ksekkset', output: 'kset'
And this is my solution.
function solution(s) {
let answer = "";
for(let i in s) {
if(s.indexOf(s[i]) == i) answer+=s[i];
}
return answer;
}
My question is, why do I get correct answer only when I put '==' inside if()? When I put '===' why if() only returns false?
If I use for loop, '===' also works..
function solution2(s) {
let answer = "";
for(let i=0; i<s.length; i++) {
if(s.indexOf(s[i])===i) answer+=s[i];
}
return answer;
}
I'm so confused!