JavaScript Practice Questions And Answer

Problem:Write a program to toggle case of each character of a string.
Solution:
          <html>
            <head>
              <script>
              function demo1()
              {
                var str = document.getElementById("input1").value
                var newStr = ""
                for (i=0;i<str.length;i++)
                {
                  let chAscii = str.charCodeAt(i)
                  let newCharCode = 0
                    if(chAscii>=65 && chAscii<=90)
                  {
                    document.write("<br>Capital - "+str[i])
                    newCharCode = chAscii+32
                  }
                   if(chAscii>=97 && chAscii<=122)
                  {
                    document.write("<br>Small - "+str[i])
                    newCharCode = chAscii-32
                  }
                    let toggleChar = String.fromCharCode(newCharCode)
                    newStr = newStr + toggleChar
                  }
                   document.write("<br>" + newStr)
                }
               </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

Office no.- 401,Shekhar Central Building ,Palasiya, Pin-code:452001, Indore

Email Us

contact@codebetter.in

Call Us

+91 88230 75444, +91 99939 28766

Loading
Your message has been sent. Thank you!