Advertisement
Shavit

JavaScript1

Dec 10th, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 4.26 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <meta charset="UTF-8">
  5.         <title>Crazy Table</title>
  6.        
  7.         <style type="text/css">
  8.             #count {width: 5em}
  9.             #margin {height: 20em; width: 20em; border: 0.5em solid red}
  10.             #motherTable {width: 100%; height: 100%; border-collapse: collapse; table-layout: fixed}
  11.             #tableSize {width: 3em}
  12.         </style>
  13.        
  14.         <script type="text/javascript">
  15.             function charCount()
  16.             {
  17.                 (document.getElementById("count")).value = (document.getElementById("textArea")).value.length;
  18.             }
  19.            
  20.             function getRandomColor()
  21.             {
  22.                 var R = Math.floor((Math.random() * 255) + 1);
  23.                 var G = Math.floor((Math.random() * 255) + 1);
  24.                 var B = Math.floor((Math.random() * 255) + 1);
  25.                 return "rgb(" + R + ", " + G + ", " + B + ")";
  26.             }
  27.            
  28.             function changeColor()
  29.             {
  30.                 (document.getElementById("textArea")).style.backgroundColor = getRandomColor();
  31.             }
  32.            
  33.             function findString()
  34.             {
  35.                 var fullText = document.getElementById("textArea").value;
  36.                 var text = document.getElementById("stringFinder").value;
  37.                
  38.                 var count = 0;
  39.                 var end = fullText.length;
  40.                
  41.                 while(end >= text.length)
  42.                 {              
  43.                     fullText = fullText.substring(0, end);
  44.                     var occurence = fullText.lastIndexOf(text);
  45.                    
  46.                     if(occurence != -1)
  47.                     {      
  48.                         count++;
  49.                         end = occurence;
  50.                     }
  51.                     else
  52.                         end--;
  53.                 }
  54.                
  55.                 alert(count + " occurences!");
  56.             }
  57.            
  58.             function isPrime(num)
  59.             {
  60.                 if(num <= 1)
  61.                     return false;
  62.                 if(num == 2)
  63.                     return true;
  64.                 if(num % 2 == 0)
  65.                     return false;
  66.  
  67.                 for(var f = 3, root = Math.sqrt(num); f <= root; f += 2)
  68.                     if(num % f == 0)
  69.                         return false;
  70.  
  71.                 return true;
  72.             }
  73.  
  74.             function findPrimaries(num)
  75.             {          
  76.                 var result = "";
  77.  
  78.                 for(var i = 0; i <= num; i++)
  79.                     if(isPrime(i))
  80.                         result += i + " ";
  81.  
  82.                 alert(result);
  83.             }
  84.            
  85.             function primaryNumbers()
  86.             {
  87.                 var num = prompt("Enter a number please:");
  88.                 findPrimaries(num);
  89.             }
  90.  
  91.             window.onload = function()
  92.             {
  93.                 document.getElementById("line").innerHTML = "This is code generated paragraph";
  94.             }
  95.  
  96.             function createTable()
  97.             {
  98.                 var table = document.getElementById("table");
  99.                 var size = +document.getElementById("tableSize").value;
  100.                 if(!(size > 0))
  101.                     return;
  102.  
  103.                 table.innerHTML = "";
  104.                 for(var i = 0; i < size; ++i)
  105.                 {
  106.                     var row = "<tr>";
  107.                     for(var j = 0; j < size; ++j)
  108.                         row += "<td></td>";
  109.  
  110.                     table.innerHTML += row + "</tr>"
  111.                 }
  112.             }
  113.            
  114.             var key;
  115.  
  116.             function animation()
  117.             {
  118.                 var bt = document.getElementById("animationButton");
  119.                 if(bt.innerHTML == "התחל אנימציה")
  120.                 {
  121.                     var tableSize = document.getElementById("tableSize");
  122.                     if(+tableSize.value <= 0)
  123.                         tableSize.value = 1;
  124.  
  125.                     var size = +tableSize.value;
  126.                     var step = 1;
  127.                    
  128.                     bt.innerHTML = "עצור אנימציה";
  129.                    
  130.                     key = setInterval(function()
  131.                     {
  132.                         if(size >= 10)
  133.                             step = -1;
  134.                         else if(size <= 1)
  135.                             step = 1;
  136.  
  137.                         size += step;
  138.                         tableSize.value = size + "";
  139.                         createTable();
  140.                     }, 200);
  141.                 }
  142.                 else
  143.                 {
  144.                     clearInterval(key);
  145.                     bt.innerHTML = "התחל אנימציה";
  146.                 }
  147.             }
  148.         </script>
  149.        
  150.     </head>
  151.     <body>
  152.         <table>
  153.             <tr valign="bottom">
  154.             <td rowspan="2"><textarea id="textArea" oninput = "charCount()" rows="10" cols="50" placeholder="Enter text here..."></textarea></td>
  155.                 <td>
  156.                     <label>Count</label>
  157.                     <input id="count" type="number" readonly="readonly"/>
  158.                 </td>
  159.             </tr>
  160.             <tr>
  161.                 <td><button onclick="changeColor()">שנה צבע</button></td>
  162.             </tr>
  163.         </table>
  164.         <button onclick="findString()">חפש תווים</button>
  165.         <input id="stringFinder" type="text">
  166.         <br><br>
  167.         <button onclick="primaryNumbers()">מספרים ראשוניים</button>
  168.         <br><br><br>
  169.         <div id="line"></div>
  170.         <br>
  171.         <div id="margin">
  172.             <table border="1" id="motherTable">
  173.                 <tbody id="table">
  174.                 </tbody>
  175.             </table>
  176.         </div>
  177.         <label for="tableSize">גודל הטבלה</label>
  178.         <input id="tableSize" type="number" min="1" max="10" value="1">
  179.         <button onclick="createTable()">צור טבלה</button>
  180.         <button id="animationButton" onclick="animation()">התחל אנימציה</button>
  181.         </body>
  182. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement