6 Feb 2018

Published 10:04 by with 0 comment

Java Script Part 1

Hi,
I had planned to create java script tutorials for students and readers of my blog. here I will discuss very important points regarding java script.

I will keep updating this post so readers are advised to visit it frequently.

I would also like to share some important javascripts as below.

1. javascript to find first 10 prime numbers.


<html><head>
       <title>first 10 prime numbers </title>
</head>
<body>

<script type="text/javascript">
 
 var count=10,i=0,j=0,k=2;
 document.write("<br/><h2>first 10 prime numbers.</h2>");
 for(i=0;i<count;k++)
 {
        var isprime = true;
     for(j=2;j<=k-1;j++)
     {
              if(k%j == 0){
        isprime = false;
        break;
     }
     
     }
     if(isprime == true){
        document.write("<br/> prime no : "+k);
     i++;
     }
 }

</script>
</body>
</html>


output of the script is as below.


------------------------------------------------------------------------------------------------------------------------
2. Javascript to check if string is palindrome or not




<html>
<head>
       <title>javascript to check if string is palindrome or not ?</title>
</head>
<body>

<script type="text/javascript">
    var s1,s2,len,i,ispalindrome=true;
 s1= prompt("would you please enter string");
 len = s1.length;
    
 document.write("<br/><h2>you have entered "+ s1 +"</h2>");
 
 for(i=0;i<len;i++){
  if(s1[i] != s1[len-1-i]){
    ispalindrome = false;
    break;   
  }
 }
 if(ispalindrome == true )
 {
    document.write("<br/><h2>entered string "+ s1 +" is palindrome</h2>");
 }
 else
 {
    document.write("<br/><h2>entered string "+ s1 +" is NOT palindrome</h2>"); 
 }
</script>
</body>
</html>



output of the script is as below. IT WILL ASK FOR STRING

AND WILL SHOW ANSWER AS BELOW.


Read More
    email this       edit