IT-Academy

Polia Viacrozmerne JS

Mar 9th, 2015
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3.   <head>
  4.   <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  5.   <meta name="generator" content="PSPad editor, www.pspad.com">
  6.   <title>Polia a pokrocile metody</title>
  7.   </head>
  8.   <body>
  9.       <script type="text/javascript">
  10.       //<![CDATA[
  11.      
  12.         document.write("<h1>Polia a pokrocile metody</h1>");
  13.        
  14.         document.write("<h2>I. Metoda sort()</h2>");
  15.        
  16.         pole = new Array(5,6,7,8,22,1,33,100,2);
  17.         document.write("Pole: " + pole + "<br />");
  18.        
  19.         pole.sort();
  20.         document.write("Zoradene abecedne: " + pole + "<br />");
  21.        
  22.        
  23.         pole.sort(porovnajVzostupne);    // pozor nie porovnaj()
  24.        
  25.         function porovnajVzostupne(a,b){
  26.           return a - b ;
  27.         }
  28.        
  29.         document.write("<h2>II. Metoda sort() s vlastnou funkciou na vzostupne zoradenie</h2>");
  30.         document.write("Zoradene ciselne vzostupne: " + pole + "<br />");
  31.        
  32.         document.write("<h2>III. Metoda sort() s vlastnou funkciou na zostupne zoradenie</h2>");
  33.        
  34.         pole.sort(porovnajZostupne);    
  35.                
  36.         function porovnajZostupne(a,b){
  37.           return b - a ;
  38.         }
  39.                      
  40.         document.write("Zoradene ciselne zostupne: " + pole + "<br />");
  41.        
  42.         document.write("<h2>IV. Metoda reverse()</h2>");      
  43.         pole.reverse();
  44.         document.write("Pole: " + pole + "<br />");
  45.        
  46.         document.write("<h2>V. Tvorba pola pomocou literatou</h2>");
  47.        
  48.         // mojePole = new Array();  
  49.         mojePole = []; // -> bezpecnejsi sposob, konstruktor Array() je mozne prepisat
  50.         // a nahradit zakernym kodom napr. predstierame ze robime pole a pritom odosielame data
  51.        
  52.         document.write("Pole: " + mojePole + "<br />");
  53.        
  54.         poleCisel = [4,8,15,16,23,42]
  55.         document.write("Pole: " + poleCisel + "<br />");
  56.        
  57.         poleStringov = ["Karol", "Jano", "Adam"]
  58.         document.write("Pole: " + poleStringov + "<br />");
  59.  
  60.         poleRoznychTypov = ["Karol", 1, undefined, 42, "Laco", null, []];
  61.         document.write("Pole: " + poleRoznychTypov + "<br />");
  62.        
  63.        document.write("<h2>VI. Indexovanie pomocou stringu</h2>");
  64.        
  65.        novePole = [];
  66.        novePole["zamestnanec"] = "Karol";
  67.        novePole["sef"] = "Fero";
  68.        
  69.        // Pouzivat asociativne polia sa nedoporucuje lepsie ukladat data do objektov
  70.        document.write("Asociativne Pole: " + novePole["zamestnanec"] + "<br />");
  71.        
  72.       document.write("<h2>VI. Viacrozmerne a vnorene polia</h2>");
  73.       var hierarchia = ["Laco", ["Karol", ["Eva", ["Tomas", "Peter", "Ivana"]]]];
  74.      
  75.       // Escape sekvencie \" pre lomitko
  76.       document.write("Viacrozmerne pole: [\"Laco\", [\"Karol\", [\"Eva\", [\"Tomas\", \"Peter\", \"Ivana\"]]]]" + "<br />");
  77.      
  78.       document.write("Vnorenie Pole hierarchia[0] : " + hierarchia[0] + "<br />");
  79.       document.write("Vnorenie Pole hierarchia[1][0]: " + hierarchia[1][0] + "<br />");
  80.       document.write("Vnorenie Pole hierarchia[1][1][0]: " + hierarchia[1][1][0] + "<br />");
  81.       document.write("Vnorenie Pole hierarchia[1][1][1][0]: " + hierarchia[1][1][1][0] + "<br />");
  82.       document.write("Vnorenie Pole hierarchia[1][1][1][1]: " + hierarchia[1][1][1][1] + "<br />");
  83.          
  84.                
  85.       //]]>
  86.       </script>
  87.      
  88.      
  89.   </body>
  90. </html>
Advertisement
Add Comment
Please, Sign In to add comment