HTML JavaScript

JavaScript Examples:
1) Write a program to accept a no. from user and check whether the no is Palindrome or not.
2) Write a program to accept a no. from user and print whether it is Prime or not.
3) Write a program to accept a no. from user and print Sum of Digits of that no.
4) Write a program to accept a no. and print Fibonacci series upto that terms.
5) Write a program to determine the positive no. that has the largest Persistence among two digit nos. e.g. n=36→18→8  the persistence of 36 is 2
6) Change the background of the page as the combo box option
7) Simple Calculator in JavaScript
8) Font Magic in JavaScript 

Change the background of the page as the combo box option


<html>
<head>
<title>Color Changer-Kashid </title>
<script>
function setcolor()
 {

 document.bgColor=frm.s1.value;

 }
   
</script>
</head>
<body>
<form name=frm>
Select Color:<select name='s1' onChange='setcolor();'>
<option value='#ADFF2F'>Green</option>
<option value='#cccfff'>violet</option>
<option value='#FF6600'>Orange</option>
<option value='gray'>Gray</option>
<option value='pink'>Pink</option>
</form>
</body>
</html>


Simple Calculator in JavaScript



<html>
<head>
<title>JavaScript Examples by Kashid</title>
<script type='text/JavaScript'>
  function solve( cmd)
    {
      var n1,n2
       n1=parseInt(frm.t1.value)
       n2=parseInt(frm.t2.value)
      
        switch (cmd)
          {
            case 1:frm.t3.value=(n1+n2)
                   break;
            case 2:frm.t3.value=(n1-n2)      
                   break;
            case 3:frm.t3.value=(n1*n2)      
                   break;      
            default:frm.t3.value=(n1/n2)      
                   break;      
                   
          } 
   }
 </script>
  </head>
    <body bgcolor=#cccfaa>
    <form name=frm><div style='width:250px'>
  Enter no. 1:<input type='text' name='t1' /><br/>
  Enter no. 2:<input type='text' name='t2' />
              <hr align='left' style='width:250px'/>
  Result--->:<input type='text' name='t3' /><br/>
  <input type='button'  name='b1' value='Add' onclick='solve(1);'/>
  <input type='button' name='b2' value='Sub' onclick='solve(2);'/>
  <input type='button'  name='b3' value='Mul' onclick='solve(3);'/>
  <input type='button' name='b4' value='Div' onclick='solve();'/>    
  </div></form>
  </body>
  </html>


Font Magic in JavaScript


<html>
<head><title>Font Style</title>
<script >
 function changefont()
    {
  
     if(document.frm.c1.checked)
         frm.myname.style.fontWeight="bold"
         else
         frm.myname.style.fontWeight="normal"
         if(document.frm.c2.checked)
         frm.myname.style.fontStyle="italic"
          else
        frm.myname.style.fontStyle="normal"
     
        
  } 

</script>

</head>
<body bgcolor='#cccfaa'>
<form name=frm>
<input type=text value="Font properties- Kashid" id="myname" size='25' >
<br>
 <input type =checkbox name=c1 onchange="changefont();">Bold
 <input type =checkbox name=c2 onchange="changefont();">Italic
 
</form>
</body>
</html>


      Take one text box  and three option buttons to display the given number in Decimal number format or Octal number format or Hexadecimal number format.


<html>
<head>
<script>
 function dec_to_oct()
   {
       var n,x,st
       n=parseInt(frm.t1.value)
       st=""
       while(n>0)
         {
          st=(n%8)+st
          n=Math.floor(n/8)
         
          }
       frm.t2.value=st
      
    }    
   
    function dec_to_hex()
   {
       var n,x,st
       var ch=new Array("A","B","C","D","E","F")
       n=parseInt(frm.t1.value)
       st=""
      
       while(n>0)
         {
              x=n%16
              n=Math.floor(n/16)
               if(x>9)
                   st=ch[x-10]+st
                else
                   st=x+st
          }
       frm.t2.value=st
      
    }     
   
    function dec_to_bin()
   {
       var n,x,st
       n=parseInt(frm.t1.value)
       st=""
       while(n>0)
         {
          x=n%2
          n=Math.floor(n/2)
          st=x+st
          }
       frm.t2.value=st
      
    }         
      
       function num(choice)
     
        {
          switch (choice)
            {
             case 1:dec_to_bin();
                    break;
            
            
            
             case 2:dec_to_oct();
                    break;
            
            
            
             case 3:dec_to_hex();
                    break;
              }        
        
         }
    
</script>
<title>Decimal Converter-Kashid</title>
</head>
<body style='background:#cccfaa;font-family:arial'>
<form name=frm>
 Enter a number <input type='text' name='t1' >
  <br/>
  <input type=radio name='r1' value='binary' onclick='num(1);'>Binary
  <br/>
  <input type=radio name=r1 value='Octal' onclick='num(2);'>Octal<br/>
  <input type=radio name=r1 value='Hexadecimal' onclick='num(3);'>Hexadecimal
 <hr align='left' style='width:260px'/>
 <br/> Result-------->:<input type='text' name='t2' >
    </form>
</body>
</html>




Moving Label with JavaScript setInterval

<HTML>
<HEAD>
<script>
function Startmove()
{

tm=window.setInterval("movetext();",50)

}

function movetext()
{   
   myname.innerHTML="JavaScript Magic- " +parseInt(myname.style.top)+"px"
   myname.style.top=parseInt(myname.style.top)+1
}

function Stopmove()
{
window.clearInterval(tm)
}


</script>
<TITLE>Interval Program </TITLE>
</HEAD>
<BODY >
<div id=myname style="LEFT: 150px; POSITION: absolute; TOP: 0px;background:#cccfff;font-family:verdana">JavaScript Magic</div>
<input type=button value="Stop" onclick="Stopmove()">
<input type=button value="Start" onclick="Startmove()">

</BODY></HTML>


Have a Question ? Need Help in College Assignments, Need Code that is not available here? Just leave a comment & get your code instantly.

Tips to Enhance Your Blog