Viktor Viktor - 4 months ago 11
Javascript Question

How to find Number in Number (2 in 12)

Can someone write a code that can search array to check if the number (like 2) is in the numbers (like 12 22 32).

Thanks in advance.

Answer

Try it :

<html>
    <head>
        <meta charset="utf-8">
    </head>
    <body>
        <script>
            var digit = 442444;
            var str = digit.toString();
            var fund = false;
            for(var i=0;i<str.length;i++)
                if(str[i]=="2") {
                    fund = true;
                    break;
                }
            if(fund)
                console.log ("2 is fund");
            else
                console.log("No fund");
                    
           
        </script>
     </body>
</html>

Or use regex :

<html>
    <head>
        <meta charset="utf-8">
    </head>
    <body>
        <script>
            var digit = 444427884;
            var patt = /[2]/g;
            if(patt.test(digit))
                console.log("2 is fund");
            else
                console.log("no fund");
        </script>
     </body>
</html>

Comments