JavaScript Practice Questions And Answer

Problem:Write a program to find total number of alphabets, digits or special character in a string.
Solution:
            <html>
             <head>
              <script>
              function demo1()
          {
              var str = document.getElementById("input1").value
              var alphaCount=0
              var digitCount=0
              var spCount=0
              for (let i = 0; i < str.length; i++)
            {
              let ascii = str.charCodeAt(i)
              if( (ascii>=65 && ascii<=90) ||
              (ascii>=97 && ascii<=122) )
             {
              //document.write(str[i] + " - Alphabet <br>")
              alphaCount++
             }
              else if (ascii>=48 && ascii<=57) 
              {
               //document.write(str[i] + " - digit <br>")
                digitCount++
              }
               else
                {
                 // document.write(str[i] + " - special char <br>")
                   spCount++
                }
              }
                document.write(spCount + " - special char <br>")
                document.write(digitCount + " - digits  <br>")
                document.write(alphaCount + " - alphabets char <br>")
            }
             </script>
            </head>
            <body>
                <input type= "text" id="input1" placeholder="enter data"/>
                <br><br>
                <input type="button" value="Go" onclick="demo1()" /><br>
            </body>
        </html>
          

Contact Us

Our Address

Plot No. 64, PU-4, Scheme 54, Behind C21 Mall near Hotel Holiday , AB Road, Indore Pin-code:452001

Email Us

contact@codebetter.in

Call Us

+91 88230 75444, +91 99939 28766

Loading
Your message has been sent. Thank you!