[Javascript] Prosledjivanje niza u funkciju by Anarhista

ljuskaArchive
//archivePosts: 37Member, Moderator in HTML5 / CSS3 / JS

Ovo je moj javascript kod:

<script type="text/javascript">
                    var grami = new Array(counter);
                    var ids = new Array(counter);
                    var counter = 0;
                    var EV = 0;
                    var proteini = 0;
                    var masti = 0;
                    var UH = 0;
                    var temp;
                    var cellValue = 2;
                    function getProtein(input) {
                    counter++;
                    var id = input;
                    var grama = parseFloat(prompt("Iznos namirnice u gramima: "));
                    document.getElementById(input).innerHTML = grama;
                    grami.push(grama);
                    ids.push(id);

                }

                function calculate() {
                var i;
                var j;
                for(i = 0; i < counter;i++) {
                    for(j = 2; j < 6;j++) {
                    alert(ids[i]);
                var Row = document.getElementById(x);
                var Cells = Row.getElementsByTagName("td");
                alert(Cells[j].innerText);
                }
                }
                }
            </script>

E sad, imajte u vidu da nemam neko znanje iz Javascripta i da sve što vidite jeste potencijalno NOOBish. O čemu se radi? Imam svuda po HTML stranici Button-e koji na klik reaguju i izbacuju korisniku da unese neku float broj. Taj broj se odmah doda u tabelu kako bi on imao uvid koje vrednosti koristi. E sada. Ja sam sve što on unese smestio u nizove. I same ID-ove sam smestio u tabelu da bih znao koji red u tabeli je dobio vrednost (u pitanju su nutritivne vrednosti namirnica. E sada je problem kada on stisne dole button calculate. Očigleno je da niz koji radi gore, ne radi u funkciju calculate. Ja sam proverio i znam da su vrednosti skladištene tu. Ali kada pokušam te vrednosti koristiti iz tabele, neće.
Evo i HTML:

    <!DOCTYPE html>
<html>
    <head>
        <title>Kalkulator namirnica</title>
            <link rel="stylesheet" type="text/css" href="stil.css" />

    </head>

    <body>
        <div id="line">
            <center><img src="v.jpg"></center>
        </div>
        <br />
        <table id="tabela">
            <tr>
                <td>ID</td>
                <td>Ime namirnice</td>
                <td>Energetska vrednost</td>
                <td>Proteini</td>
                <td>Ugljeni hidrati</td>
                <td>Masti</td>
                <td>Unos</td>
                <td>Aktivno</td>
            </tr>
            <tr id="-1">
                <td>1</td>
                <td>Jabuka</td>
                <td>52</td>
                <td>0.26</td>
                <td>13,81</td>
                <td>0.17</td>
                <td><button onclick="getProtein(1)" class="dugme">Dodaj</button></td>
                <td id="1"></td>
            </tr>
            <tr id="-2">
                <td>2</td>
                <td>Kruska</td>
                <td>58</td>
                <td>0.4</td>
                <td>15</td>
                <td>0.1</td>
                <td><button onclick="getProtein(2)" class="dugme">Dodaj</button></td>
                <td id="2"></td>
            </tr>
            <tr id="-3">
                <td>3</td>
                <td>Banana</td>
                <td>90</td>
                <td>1</td>
                <td>13.5</td>
                <td>0.5</td>
                <td><button onclick="getProtein(3)" class="dugme">Dodaj</button></td>
                <td id="3"></td>
            </tr>
            <tr id="-4">
                <td>4</td>
                <td>Borovnica</td>
                <td>57</td>
                <td>0.74</td>
                <td>14.5</td>
                <td>0.25</td>
                <td><button onclick="getProtein(4)" class="dugme">Dodaj</button></td>
                <td id="4"></td>
            </tr>
            <tr id="-5">
                <td>5</td>
                <td>Breskva</td>
                <td>39</td>
                <td>0.91</td>
                <td>11</td>
                <td>0.25</td>
                <td><button onclick="getProtein(5)" class="dugme">Dodaj</button></td>
                <td id="5"></td>
            </tr>
            <tr id="-6">
                <td>6</td>
                <td>Grožđe</td>
                <td>69</td>
                <td>0.72</td>
                <td>7.68</td>
                <td>0.3</td>
                <td><button onclick="getProtein(6)" class="dugme">Dodaj</button></td>
                <td id="6"></td>
            </tr>
            <tr id="-7">
                <td>7</td>
                <td>Jagoda</td>
                <td>32</td>
                <td>0.67</td>
                <td>7.68</td>
                <td>0.30</td>
                <td><button onclick="getProtein(7)" class="dugme">Dodaj</button></td>
                <td id="7"></td>
            </tr>
            <tr id="-8">
                <td>8</td>
                <td>Jogurt</td>
                <td>61</td>
                <td>3.47</td>
                <td>4.66</td>
                <td>3.25</td>
                <td><button onclick="getProtein(8)" class="dugme">Dodaj</button></td>
                <td id="8"></td>
            </tr>
            <tr id="-9">
                <td>9</td>
                <td>Ječam</td>
                <td>354</td>
                <td>12</td>
                <td>73</td>
                <td>2.3</td>
                <td><button onclick="getProtein(9)" class="dugme">Dodaj</button></td>
                <td id="9"></td>
            </tr>
            <tr id="-10">
                <td>10</td>
                <td>Kikiriki</td>
                <td>567</td>
                <td>25</td>
                <td>16.13</td>
                <td>50</td>
                <td><button onclick="getProtein(10)" class="dugme">Dodaj</button></td>
                <td id="10"></td>
            </tr>
            <tr id="-11">
                <td>11</td>
                <td>Kivi</td>
                <td>61</td>
                <td>1.1</td>
                <td>14.66</td>
                <td>0.52</td>
                <td><button onclick="getProtein(11)" class="dugme">Dodaj</button></td>
                <td id="11"></td>
            </tr>
            <tr id="-12">
                <td>12</td>
                <td>Krastavac</td>
                <td>12</td>
                <td>0.59</td>
                <td>2</td>
                <td>0.16</td>
                <td><button onclick="getProtein(12)" class="dugme">Dodaj</button></td>
                <td id="12"></td>
            </tr>
            <tr id="-13">
                <td>13</td>
                <td>Krompir</td>
                <td>58</td>
                <td>2.57</td>
                <td>12</td>
                <td>0.10</td>
                <td><button onclick="getProtein(13)" class="dugme">Dodaj</button></td>
                <td id="13"></td>
            </tr>
            <tr id="-14">
                <td>14</td>
                <td>Kukuruz</td>
                <td>86</td>
                <td>3.22</td>
                <td>19</td>
                <td>1.18</td>
                <td><button onclick="getProtein(14)" class="dugme">Dodaj</button></td>
                <td id="14"></td>
            </tr>
            <tr id="-15">
                <td>15</td>
                <td>Kupina</td>
                <td>43</td>
                <td>1.39</td>
                <td>9.61</td>
                <td>0.49</td>
                <td><button onclick="getProtein(15)" class="dugme">Dodaj</button></td>
                <td id="15"></td>
            </tr>
                <tr id="-16">
                <td>16</td>
                <td>Kupus</td>
                <td>24</td>
                <td>1.21</td>
                <td>10.701</td>
                <td>0.30</td>
                <td><button onclick="getProtein(16)" class="dugme">Dodaj</button></td>
                <td id="16"></td>
            </tr>
            <tr id="-17">
                <td>17</td>
                <td>Limun</td>
                <td>20</td>
                <td>1.21</td>
                <td>10.70</td>
                <td>0.30</td>
                <td><button onclick="getProtein(17)" class="dugme">Dodaj</button></td>
                <td id="17"></td>
            </tr>
            <tr id="-18">
                <td>18</td>
                <td>Lešnik</td>
                <td>628</td>
                <td>15</td>
                <td>16.70</td>
                <td>60</td>
                <td><button onclick="getProtein(18)" class="dugme">Dodaj</button></td>
                <td id="18"></td>
            </tr>
            <tr id="-19">
                <td>19</td>
                <td>Lubenica</td>
                <td>30</td>
                <td>0.61</td>
                <td>7.55</td>
                <td>0.15</td>
                <td><button onclick="getProtein(19)" class="dugme">Dodaj</button></td>
                <td id="19"></td>
            </tr>
            <tr id="-20">
                <td>20</td>
                <td>Malina</td>
                <td>52</td>
                <td>1.2</td>
                <td>12</td>
                <td>0.65</td>
                <td><button onclick="getProtein(20)" class="dugme">Dodaj</button></td>
                <td id="20"></td>
            </tr>
            <tr id="-21">
                <td>21</td>
                <td>Maslinovo ulje</td>
                <td>884</td>
                <td>0</td>
                <td>0</td>
                <td>100</td>
                <td><button onclick="getProtein(21)" class="dugme">Dodaj</button></td>
                <td id="21"></td>
            </tr>
            <tr id="-22">
                <td>22</td>
                <td>Mleko</td>
                <td>49</td>
                <td>3.75</td>
                <td>4.65</td>
                <td>2.08</td>
                <td><button onclick="getProtein(22)" class="dugme">Dodaj</button></td>
                <td id="22"></td>
            </tr>
            <tr id="-23">
                <td>23</td>
                <td>Med</td>
                <td>304</td>
                <td>0</td>
                <td>82</td>
                <td>0</td>
                <td><button onclick="getProtein(23)" class="dugme">Dodaj</button></td>
                <td id="23"></td>
            </tr>
            <tr id="-24">
                <td>24</td>
                <td>Pomorandža</td>
                <td>63</td>
                <td>1.3</td>
                <td>15.5</td>
                <td>0.3</td>
                <td><button onclick="getProtein(24)" class="dugme">Dodaj</button></td>
                <td id="24"></td>
            </tr>
            <tr id="-25">
                <td>25</td>
                <td>Orah</td>
                <td>654</td>
                <td>15.23</td>
                <td>13.71</td>
                <td>65</td>
                <td><button onclick="getProtein(25)" class="dugme">Dodaj</button></td>
                <td id="25"></td>
            </tr>
            <tr id="-26">
                <td>26</td>
                <td>Pasulj</td>
                <td>110</td>
                <td>23</td>
                <td>21</td>
                <td>7</td>
                <td><button onclick="getProtein(26)" class="dugme">Dodaj</button></td>
                <td id="26"></td>
            </tr>
            <tr id="-27">
                <td>27</td>
                <td>Soja</td>
                <td>416</td>
                <td>36</td>
                <td>30</td>
                <td>20</td>
                <td><button onclick="getProtein(27)" class="dugme">Dodaj</button></td>
                <td id="27"></td>
            </tr>
            <tr id="-28">
                <td>28</td>
                <td>Grašak</td>
                <td>81</td>
                <td>5</td>
                <td>14.5</td>
                <td>0.4</td>
                <td><button onclick="getProtein(28)" class="dugme">Dodaj</button></td>
                <td id="28"></td>
            </tr>
            <tr id="-29">
                <td>29</td>
                <td>Kinoa</td>
                <td>368</td>
                <td>14</td>
                <td>64</td>
                <td>6</td>
                <td><button onclick="getProtein(29)" class="dugme">Dodaj</button></td>
                <td id="29"></td>
            </tr>
            <tr id="-30">
                <td>30</td>
                <td>Leblebija</td>
                <td>300</td>
                <td>20</td>
                <td>55</td>
                <td>4</td>
                <td><button onclick="getProtein(30)" class="dugme">Dodaj</button></td>
                <td id="30"></td>
            </tr>
        </table>
        <center><button id="reset" onclick="window.location.reload()">RESET</button>&nbsp;&nbsp;&nbsp;&nbsp;<button id="calculate" onclick="calculate()">CALCULATE</button></center>
        <script type="text/javascript">
                    var grami = new Array(counter);
                    var ids = new Array(counter);
                    var counter = 0;
                    var EV = 0;
                    var proteini = 0;
                    var masti = 0;
                    var UH = 0;
                    var temp;
                    var cellValue = 2;
                    function getProtein(input) {
                    counter++;
                    var id = input;
                    var grama = parseFloat(prompt("Iznos namirnice u gramima: "));
                    document.getElementById(input).innerHTML = grama;
                    grami.push(grama);
                    ids.push(id);

                }

                function calculate() {
                var i;
                var j;
                for(i = 0; i < counter;i++) {
                    for(j = 2; j < 6;j++) {
                    alert(ids[i]);
                var Row = document.getElementById(x);
                var Cells = Row.getElementsByTagName("td");
                alert(Cells[j].innerText);
                }
                }
                }
            </script>
    </body>
</html>

CSS:

* {
    padding:0px;
    margin:0px;
}
body {
    background-color:#fff;
}
#line {
    width:100%;
    height:100px;
    border-top:3px solid #4473f7;
}
table {
    margin-left:auto;
    margin-right:auto;
    border:1px solid #1448b1;
    width:600px;
    border-collapse:collapse;
}
table td {
    text-align:center;
    background-color:#4473f7;
    height:40px;
    border-left:1px solid #1448b1;
    color:#fff;
}
table tr {
    border-bottom:1px dashed #1448b1;
}
.dugme {
    width:60px;
    height:35px;
    background-color:#90a9f4;
    border:1px solid #4473f7;
    color:#fff;
}
#reset {
    width:150px;
    height:40px;
    background-color:#d20101;
    border:1px solid #940000;
    color:#fff;
}
#calculate {
    width:150px;
    height:40px;
    background-color:#0500e3;
    border:1px solid #030080;
    color:#fff;
}
Sign In or Register to comment.

Welcome

It looks like you're new here. If you want to get involved, click one of these buttons!

Discussions