Naloga 1.3

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">
  <title></title></head>
 <body>
  <script>
   var x = 3, y = 4, tmp;
   tmp = x;
   x = y;
   y = tmp;
   console.log(x);
   console.log(y);
  </script>
 </body></html>

----------

Naloga 2.1

<html>
 <head>
  <meta charset="utf-8">
  <title></title></head>
 <body>
  <script>
   var a = 1, v = 1, s = 1;
   var s_min = v * v / (2 * a);
   if(s_min <= s) {
    console.log("Steza je dovolj dolga.");
   } else {
    console.log("Steza je prekratka.");
   }
  </script>
 </body></html>

----------

Naloga 2.3

<html>
 <head>
  <meta charset="utf-8">
  <title></title></head>
 <body>
  <script>
   var a = -7, b = 3;
   var sgn = 1;
   if(b == 0) {
    console.log(NaN);
   } else {
    if(a < 0) {
     sgn = -1;
    }
    a = Math.abs(a);
    b = Math.abs(b);
    while(a >= b) {
     a = a - b;
    }
    a = a * sgn;
    console.log(a);
   }
  </script>
 </body></html>

----------

Naloga 2.4

<html>
 <head>
  <meta charset="utf-8">
  <title></title></head>
 <body>
  <script>
   var n = 371, m;
   var exp = 0, s = 0, i = 1, d;
   while(n / i >= 1) {
    exp = exp + 1;
    i = i * 10;
   }
   m = n;
   i = 0;
   while(i < exp) {
    d = m % 10;
    m = (m - d) / 10;
    s = s + Math.pow(d, exp);
    i = i + 1;
   }
   if(s == n) {
    console.log("Število je Armstrongovo.");
   } else {
    console.log("Število ni Armstrongovo.");
   }
  </script>
 </body></html>

----------

Naloga 2.7

<html>
 <head>
  <meta charset="utf-8">
  <title></title></head>
 <body>
  <script>
   var letn1 = 1990, letn2 = 2010;
   var l = letn1, n = 0;
   while(l <= letn2) {
    if(l % 400 == 0 || l % 4 == 0 && l % 100 != 0) {
     n = 1;
     console.log(l);
    }
    l = l + 1;
   }
   if(n == 0) {
    console.log("V tem obdobju ni prestopnih let.");
   }
  </script>
 </body></html>

----------

Naloga 2.9

<html>
 <head>
  <meta charset="utf-8">
  <title></title></head>
 <body>
  <script>
   var n = 44044;
   var i = 2;
   while(n > 1) {
    if(n % i == 0) {
     console.log(i);
     n = n / i;
    } else {
     i = i + 1;
    }
   }
  </script>
 </body></html>

----------

Naloga 3.6

<html>
 <head>
  <meta charset="utf-8">
  <title></title></head>
 <body>
  <script>
   var t = [8, 14, -4, 2, 77], n = 5;
   var i, tmp;
   for(i = 0; i < (n - 1) / 2; i = i + 1) {
    tmp = t[i];
    t[i] = t[n - 1 - i];
    t[n - 1 - i] = tmp;
   }
   console.log(t);
  </script>
 </body></html>

----------

Naloga 3.7

<html>
 <head>
  <meta charset="utf-8">
  <title></title></head>
 <body>
  <script>
   var t =  [5, 2, 3, 55, 12, 13], lihi = [0], sodi = [0], i;
   for(i = 1; i <= t[0]; i = i + 1) {
    if(t[i] % 2 == 0) {
     sodi[0] = sodi[0] + 1;
     sodi[sodi[0]] = t[i];
    } else {
     lihi[0] = lihi[0] + 1;
     lihi[lihi[0]] = t[i];
    }
   }
   console.log(lihi, sodi);
  </script>
 </body></html>

----------

Naloga 3.11

<html>
 <head>
  <meta charset="utf-8">
  <title></title></head>
 <body>
  <script>
   var x = [1, 2, 3], y = [3, 2, 1], n = 3;
   var i, prod = 0;
   for(i = 0; i < n; i = i + 1) {
    prod = prod + x[i] * y[i];
   }
   console.log(prod);
  </script>
 </body></html>

----------

Naloga 3.12

<html>
 <head>
  <meta charset="utf-8">
  <title></title></head>
 <body>
  <script>
   var n = 4;
   var v, p = [1], i, pn;
   for(v = 1; v <= n; v = v + 1) {
    pn = [1];
    for(i = 1; i < v; i = i + 1) {
     pn[i] = p[i - 1] + p[i];
    }
    pn[v] = 1;
    p = pn;
   }
   console.log(p);
  </script>
 </body></html>

----------

Naloga 3.2

<html>
 <head>
  <meta charset="utf-8">
  <title></title></head>
 <body>
  <script>
   var x = 97;
   var i, k, h = [0, 0, 0, 0];
   var cifre = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F'];
   for(i = 3; i >= 0; i = i - 1) {
    k = x % 16;
    h[i] = cifre[k];
    x = (x - k) / 16;
   }
   console.log(h);
  </script>
 </body></html>

----------

Program, ki preveri ali je EMŠO pravilen

<html>
 <head>
  <meta charset="utf-8">
  <title></title></head>
 <body>
  <script>
   var EMSO = [2, 5, 0, 5, 9, 9, 7, 5, 0, 0, 1, 5, 8], s;
   s = EMSO[12];
   for(i = 0; i < 6; i = i + 1) {
    s = s + (EMSO[i] + EMSO[i + 6]) * (7 - i);
   }
   if(s % 11 == 0) {
    console.log("EMŠO je v redu.");
   } else {
    console.log("EMŠO ni v redu.");
   }
  </script>
 </body></html>

----------

Program, ki za zamik dnevov oddaljen dan izračuna, kateri dan je to

<html>
 <head>
  <meta charset="utf-8">
  <title></title></head>
 <body>
  <script>
   var dnevi = ["pon", "tor", "sre", "čet", "pet", "sob" ,"ned"], danes = "pon", zamik = -310;
   var i, indeks = -1;
   if(zamik >= 0) {
    while(zamik > 6) {
     zamik = zamik - 7;
    }
   } else {
    while(zamik < 0) {
     zamik = zamik + 7;
    }
   }
   for(i = 0; indeks == -1; i = i + 1) {
    if(dnevi[i] === danes) {
     indeks = i;
    }
   }
   indeks = indeks + zamik;
   if(indeks > 6) indeks = indeks - 7;
   console.log(dnevi[indeks]);
  </script>
 </body></html>

----------

Podprogram, ki izračuna sinus kota v radianih na šest decimalnih mest

<html>
 <head>
  <meta charset="utf-8">
  <title></title>
  <script>
   function sin(x) {
    var i, vsota = x, clen = x;
    for(i = 2; Math.abs(clen) > 1e-7; i = i + 2) {
     clen = - clen * x * x / i / (i + 1);
     vsota = vsota + clen;
    }
    return vsota;
   }
  </script></head>
 <body>
  <script>
   var sinus, kot = Math.PI / 4;
   sinus = sin(kot);
   console.log(sinus);
  </script>
 </body></html>

----------

Naloga 4.6

<html>
 <head>
  <meta charset="utf-8">
  <title></title>
  <script>
   function uredi_bubble_sort(t) {
    var i, j, tmp, exch = 1;
    for(i = 0; t[i + 1] != 0 && exch; i = i + 1) {
     exch = 0;
     for(j = 0; t[j + 1] != 0; j = j + 1) {
      if(t[j] > t[j + 1]) {
       tmp = t[j];
       t[j] = t[j + 1];
       t[j + 1] = tmp;
       exch = 1;
      }
     }
    }
   }
  </script></head>
 <body>
  <script>
   var tab1 = [3, 7, 2, 66, 2, 0];
   var tab2 = [15, 8, 449, 61, 7, 38, 0];
   uredi_bubble_sort(tab1);
   uredi_bubble_sort(tab2);
   console.log(tab1);
   console.log(tab2);
  </script>
 </body></html>

----------

Naloga 3.4

<html>
 <head>
  <meta charset="utf-8">
  <title></title></head>
 <body>
  <script>
   var t = [[1.3, 0.1, 4.6, 3.8, 0],
            [0.3, 1.1, 0],
            [1.5, 2.5, 0.4, 0],
            [0]];
   var i, j, vsota = 0;
   for(i = 0; t[i][0] != 0; i = i + 1) {
    for(j = 0; t[i][j] != 0; j = j + 1) {
     vsota = vsota + t[i][j];
    }
   }
   console.log(vsota);
  </script>
 </body></html>

----------

Naloga 3.15

<html>
 <head>
  <meta charset="utf-8">
  <title></title></head>
 <body>
  <script>
   var n = 4, m = 8, prenos = [[0, 0, 1, 1, 0, 1, 0, 1],
                               [1, 0, 1, 1, 0, 1, 0, 0],
                               [0, 1, 0, 1, 0, 0, 1, 1],
                               [1, 1, 0, 1, 0, 0, 1, 0]];
   var i, j, ones, err = 0;
   for(i = 0; i < n && !err; i = i + 1) {
    ones = 0;
    for(j = 0; j < m; j = j + 1) {
     if(prenos[i][j] == 1) ones = ones + 1;
    }
    if(ones % 2 == 1) {
     err = 1;
    }
   }
   for(i = 0; i < m && !err; i = i + 1) {
    ones = 0;
    for(j = 0; j < n; j = j + 1) {
     if(prenos[j][i] == 1) ones = ones + 1;
    }
    if(ones % 2 == 1) {
     err = 1;
    }
   }
   if(err == 1) {
    console.log("Tabela vsebuje napako.");
   } else {
    console.log("Tabela ne vsebuje napake.");
   }
  </script>
 </body></html>

----------

Naloga 4.1

<html>
 <head>
  <meta charset="utf-8">
  <title></title>
  <script>
   function test(x) {
    console.log(x);
    x = x * 2;
    console.log(x);
    return x;
   }
  </script></head>
 <body>
  <script>
   var x = 0;
   while(x < 10) {
    x = test(x + 1);
   }
  </script>
 </body></html>

----------

Naloga 4.7

<html>
 <head>
  <meta charset="utf-8">
  <title></title>
  <script>
   function nsd(a, b) {
    console.log(a, b);
    while(b != a) {
     while(a > b) {
      a = a - b;
      console.log(a, b);
     }
     while(b > a) {
      b = b - a;
      console.log(a, b);
     }
    }
    return a;
   }
  </script></head>
 <body>
  <script>
   var x = nsd(21, 49);
  </script>
 </body></html>

----------

Funkcija, ki združi dve tabeli zaključeni s čuvajem

<html>
 <head>
  <meta charset="utf-8">
  <title></title>
  <script>
   function zdruziTabeliSCuvajem(tab1, tab2) {
    var i, j, tab = [];
    for(i = 0; tab1[i] != 0; i = i + 1) {
     tab[i] = tab1[i];
    }
    for(j = 0; tab2[j] != 0; i = i + 1, j = j + 1) {
     tab[i] = tab2[j];
    }
    tab[i] = 0;
    return tab;
   }
  </script></head>
 <body>
  <script>
   var t, t1 = [1, 2, 3, 0], t2 = [4, 5, 6, 0];
   t = zdruziTabeliSCuvajem(t1, t2);
   console.log(t);
  </script>
 </body></html>

----------

Funkcija, ki združi dve tabeli s številom elementov na prvem mestu

<html>
 <head>
  <meta charset="utf-8">
  <title></title>
  <script>
   function zdruziTabeliSStevilomElementov(tab1, tab2) {
    var i, j, tab = [];
    for(i = 1; i <= tab1[0]; i = i + 1) {
     tab[i] = tab1[i];
    }
    for(j = 1; j <= tab2[0]; i = i + 1, j = j + 1) {
     tab[i] = tab2[j];
    }
    tab[0] = i - 1;
    return tab;
   }
  </script></head>
 <body>
  <script>
   var t, t1 = [3, 1, 2, 3], t2 = [3, 4, 5, 6];
   t = zdruziTabeliSStevilomElementov(t1, t2);
   console.log(t);
  </script>
 </body></html>

----------

Naloga 4.3

<html>
 <head>
  <meta charset="utf-8">
  <title></title>
  <script>
   function izberiCifro(st, idx) {
    var i, cifra;
    for(i = idx; i <= 3; i = i + 1) {
     cifra = st % 10;
     st = (st - cifra) / 10;
    }
    return cifra;
   }
  </script></head>
 <body>
  <script>
   var x = izberiCifro(259, 0);
   console.log(x);
   x = izberiCifro(17, 2);
   console.log(x);
  </script>
 </body></html>

----------

Nalogi 4.2 in 4.10

<html>
 <head>
  <meta charset="utf-8">
  <title></title>
  <script>
   function dodajElement(tab, el) {
    var i;
    for(i = 0; tab[i] != 0; i = i + 1);
    tab[i] = el;
    tab[i + 1] = 0;
   }
   function rimskoIzdvajanje(deset, ah, al, rh, rl, rimsko) {
    for(; deset >= ah; deset = deset - ah) {
     dodajElement(rimsko, rh);
    }
    if(deset >= ah - al) {
     dodajElement(rimsko, rl);
     dodajElement(rimsko, rh);
     deset = deset - ah + al;
    }
    return deset;
   }
   function desetiskoVRimsko(deset, rimsko) {
    var i, arab = [1000, 500, 100, 50, 10, 5, 1, 0, 0];
    var rim = ['M', 'D', 'C', 'L', 'X', 'V', 'I', 0, 0];
    for(i = 0; deset > 0; i = i + 1) {
     deset = rimskoIzdvajanje(deset, arab[i], arab[i + 2 - i % 2],
                              rim[i], rim[i + 2 - i % 2], rimsko);
    }
   }
  </script></head>
 <body>
  <script>
   var rimsko = [0];
   desetiskoVRimsko(3949, rimsko);
   console.log(rimsko);
  </script>
 </body></html>

----------

Naloga 4.8

<html>
 <head>
  <meta charset="utf-8">
  <title></title>
  <script>
   function uredi(t) {
    console.log(t);
    var i, j, zacasno;
    for(i = 0; t[i] != 0; i = i + 1) {
     j = i;
     while(j > 0 && t[j - 1] > t[j]) {
      zacasno = t[j];
      t[j] = t[j - 1];
      t[j - 1] = zacasno;
      console.log(t);
      j = j - 1;
     }
    }
   }
  </script></head>
 <body>
  <script>
   var tabela = [7, 5, 3, 1, 0];
   uredi(tabela);
   console.log(tabela);
  </script>
 </body></html>

----------

Naloga 4.9

<html>
 <head>
  <meta charset="utf-8">
  <title></title>
  <script>
   function zamenjajTabeli(tab1, tab2) {
    var i;
    for(i = 0; tab1[i] != 0; i = i + 1) {
     tab1[i] = tab1[i] + tab2[i];
     tab2[i] = tab1[i] - tab2[i];
     tab1[i] = tab1[i] - tab2[i];
    }
   }
  </script></head>
 <body>
  <script>
   var t1 = [1, 2, 3, 0];
   var t2 = [4, 5, 6, 0];
   console.log(t1, t2);
   zamenjajTabeli(t1, t2);
   console.log(t1, t2);
   t1 = [1, 2, 3, 0];
   console.log(t1);
   zamenjajTabeli(t1, t1);
   console.log(t1);
  </script>
 </body></html>

----------

Naloga 5.2

<html>
 <head>
  <meta charset="utf-8">
  <title></title>
  <script>
   sklad = Object();
   sklad.podatki = [];
   sklad.vrh = 0;
   sklad.push = function(element) {
    sklad.podatki[sklad.vrh] = element;
    sklad.vrh = sklad.vrh + 1;
   };
   sklad.pop = function() {
    sklad.vrh = sklad.vrh - 1;
    return sklad.podatki[sklad.vrh];
   };
   sklad.size = function() {
    return sklad.vrh;
   };
   sklad.clear = function() {
    sklad.vrh = 0;
   };
   function obrniElemente(t, a, b, s) {
    var i;
    for(i = 0; t[i] != 0; i = i + 1) {
     if(i >= a && i < b) {
      s.push(t[i]);
     }
    }
    for(i = 0; t[i] != 0; i = i + 1) {
     if(i >= a && i < b) {
      t[i] = s.pop();
     }
    }
   }
  </script></head>
 <body>
  <script>
   var tabela = [3, 4, 5, 6, 7, 0];
   obrniElemente(tabela, 0, 10, sklad);
   console.log(tabela);
   tabela = [3, 4, 5, 6, 7, 0];
   obrniElemente(tabela, 0, 2, sklad);
   console.log(tabela);
   tabela = [3, 4, 5, 6, 7, 0];
   obrniElemente(tabela, 1, 4, sklad);
   console.log(tabela);
   tabela = [3, 4, 5, 6, 7, 0];
   obrniElemente(tabela, 1, 2, sklad);
   console.log(tabela);
  </script>
 </body></html>

----------

Naloga 5.4

<html>
 <head>
  <meta charset="utf-8">
  <title></title>
  <script>
   function newDiagLatin(n, diagLatin) {
    var i, j;
    for(i = 0; i < n; i = i + 1) {
     diagLatin[i] = [];
     for(j = 0; j < n; j = j + 1) {
      diagLatin[i][j] = (2 * i + j) % n + 1;
     }
    }
   }
  </script></head>
 <body>
  <script>
   var i, n = 5, d = [];
   newDiagLatin(n, d);
   for(i = 0; i < n; i = i + 1) {
    console.log(d[i]);
   }
  </script>
 </body></html>

----------

Naloga 3.14

<html>
 <head>
  <meta charset="utf-8">
  <title></title></head>
 <body>
  <script>
   var n = 4, t = [[2, 1, 4, 3],
                   [1, 4, 3, 2],
                   [3, 2, 1, 4],
                   [4, 3, 2, 1]];
   var el, diag, stvr, stst, i, latin = 1;
   for(el = 0; el < n && latin == 1; el = el + 1) {
    for(diag = 0; diag < n && latin == 1; diag = diag + 1) {
     stvr = 0;
     stst = 0;
     for(i = 0; i < n; i = i + 1) {
      if(t[diag][i] == t[0][el]) {
       stvr = stvr + 1;
      }
      if(t[i][diag] == t[0][el]) {
       stst = stst + 1;
      }
     }
     if(stvr != 1 || stst != 1) {
      latin = 0;
     }
    }
   }
   if(latin == 1) {
    console.log("Tabela je latinski kvadrat.");
   } else {
    console.log("Tabela ni latinski kvadrat.");
   }
  </script>
 </body></html>

----------

Naloga 6.1

<html>
 <head>
  <meta charset="utf-8">
  <title></title>
  <script>
   slovar = Object();
   slovar.podatki = [];
   slovar.konec = 0;
   slovar.insert = function(kljuc, vrednost) {
    var i;
    for(i = 0; i < slovar.konec; i = i + 1) {
     if(slovar.podatki[i][0] == kljuc) {
      slovar.podatki[i][1] = vrednost;
      return;
     }
    }
    slovar.podatki[i] = [kljuc, vrednost];
    slovar.konec = slovar.konec + 1;
   }
   slovar.lookup = function(kljuc) {
    var i;
    for(i = 0; i < slovar.konec; i = i + 1) {
     if(slovar.podatki[i][0] == kljuc) {
      return slovar.podatki[i][1];
     }
    }
    return -1;
   }
  </script></head>
 <body>
  <script>
   var i, dec = 0, heks = ['F', 'C', 0, 3, -1];
   var hexadecimal = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', -1];
   var decimal     = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9,  10,  11,  12,  13,  14,  15, -1];
   for(i = 0; hexadecimal[i] != -1; i = i + 1) {
    slovar.insert(hexadecimal[i], decimal[i]);
   }
   for(i = 0; heks[i] != -1; i = i + 1) {
    dec = 16 * dec + slovar.lookup(heks[i]);
   }
   console.log(dec);
  </script>
 </body></html>

----------

Naloga 6.2

<html>
 <head>
  <meta charset="utf-8">
  <title></title>
  <script>
   slovarps = Object();
   slovarps.glava = Object();
   slovarps.rep = slovarps.glava;
   slovarps.insert = function(kljuc, vrednost) {
    var el;
    for(el = slovarps.glava; el != slovarps.rep; el = el.naslednji) {
     if(el.podatek[0] == kljuc) {
      el.podatek[1] = vrednost;
      return;
     }
    }
    slovarps.rep = Object();
    el.podatek = [kljuc, vrednost];
    el.naslednji = slovarps.rep;
   }
   slovarps.lookup = function(kljuc) {
    var el;
    for(el = slovarps.glava; el != slovarps.rep; el = el.naslednji) {
     if(el.podatek[0] == kljuc) {
      return el.podatek[1];
     }
    }
    return -1;
   }
   slovarps.delete = function(kljuc) {
    var el;
    for(el = slovarps.glava; el != slovarps.rep; el = el.naslednji) {
     if(el.podatek[0] == kljuc) {
      if(el.naslednji == slovarps.rep) {
       slovarps.rep = el;
      }
      el.podatek = el.naslednji.podatek;
      el.naslednji = el.naslednji.naslednji;
      return;
     }
    }
   }
  </script></head>
 <body>
  <script>
   slovarps.insert('a', 97);
   slovarps.insert('b', 98);
   slovarps.insert('c', 99);
   console.log(slovarps.lookup('a'));
   console.log(slovarps.lookup('b'));
   console.log(slovarps.lookup('c'));
   slovarps.delete('b');
   console.log(slovarps.lookup('a'));
   console.log(slovarps.lookup('b'));
   console.log(slovarps.lookup('c'));
  </script>
 </body></html>

----------

Določi časovno zahtevnost algoritma:
  for(j = 0; j < n; j = j + 1) {
    for(i = n; i > 0; i = Math.floor(i / 2)) {
      racunaj();
    }
  }

n   št. iter. zun. zanke   št. iter. not. zanke   št. klicev fun. racunaj()
1            1                      1                       1
2            2                      2                       4
3            3                      2                       6
4            4                      3                      12
5            5                      3                      15
6            6                      3                      18
7            7                      3                      21
8            8                      4                      32
...         ...                    ...                     ...
n            n              floor(log2(n))+1        n*(floor(log2(n))+1)

T(n) = O(n*log2(n))