I used random function, to generate a 4 digit number.
The generated 4 digit random number should not be in the sequence of 1111, 2222, 3333, …. The generated random number could be of any number except 1111, 2222, 3333, 4444,..
I used the below approach,
But, i could get the repeating of digits using the below code. Could somebody please help.
function repeatingDigit(n) {
let num = n.toString();
for (var i = 0; i <= num.length; i++) {
if (num.substr(i) == num.substr(++i)) {
alert('This pattern can not be used');
}
else {
return parseInt(n);
}
}
}
repeatingDigit(Math.floor(1000 + Math.random() * 9000));
Thank you.
>Solution :
You could just check if it’s divisible by 1111:
function repeatingDigit(n) {
if (n % 1111 === 0) {
alert('This pattern can not be used');
}
return n;
}
repeatingDigit(Math.floor(1000 + Math.random() * 9000));