I was solving a codewars challenge and got stuck in an endless loop. The task is to find from a given input like year 1987 the next greater integer that has only distinct digits. In that case it would be 2013 as 1988, 1998,2000 etc. do not have 4 distinct digits. I wanted to solve this task with an Object and see where the length of the Object.keys(obj) is equal to 4.
Here is my try
function distinctDigitYear(year) {
let obj = {}
let count = 1
let yearCounter = year + count
while (Object.keys(obj).length !==4) {
obj = {}
yearCounter.toString().split('').forEach(el=> {
if(!Object.keys(obj).includes(el)){
obj[el] = 1
} else {
obj[el] +=1
}
}
)
count++
}
return yearCounter + 1
}
distinctDigitYear(2000)
Thanks for reading!