Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

To check the repeating digits in a single number in JS

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,

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading