数组中的函数javascript

时间:2015-12-06 17:15:25

标签: javascript

我正在用javascript进行学校练习,我已经模拟了一个 航班预订系统。 要查看谁已经预订我创建了两个布尔数组(每个类一个),当你预订我把它放错的地方时,问题是数组没有被置于假...任何解决方案? 抱歉英文不好!

<script>
   var eco = [true, true, true, true, true];
   var pri = [true, true, true, true, true];
   function economica(n)
      {

        var cambiato = false;
        for (var i = 0; i < 4; i++) {
            if (eco[i] == true) {
                switch (i) {
                    case 0: document.getElementById("e1").innerHTML = n; break;
                    case 1: document.getElementById("e2").innerHTML = n; break;
                    case 2: document.getElementById("e3").innerHTML = n; break;
                    case 3: document.getElementById("e4").innerHTML = n; break;
                    case 4: document.getElementById("e5").innerHTML = n; break;
                }
                cambiato = true;
                eco[i] == false;
                break;
            }
        }
        if (cambiato == false)
        {
            if (confirm("Posti in classe economica esaurini \n Vuoi prenotare un posto in prima classe?") == true) {
                prima(n);
            } else {
                alert("Il prossimo volo parte tra 3 ore.")
            }


        }
    }

    function prima(n)
    {

        var cambiato = false;
        for (var i = 0; i < 4; i++) {
            if (pri[i] == true) {
                switch (i) {
                    case 0: document.getElementById("p1").innerHTML = n; break;
                    case 1: document.getElementById("p2").innerHTML = n; break;
                    case 2: document.getElementById("p3").innerHTML = n; break;
                    case 3: document.getElementById("p4").innerHTML = n; break;
                    case 4: document.getElementById("p5").innerHTML = n; break;
                }
                cambiato = true;
                pri[i] == false;
                break;
            }
        }
        if (cambiato == false) {
            if (confirm("Posti in classe economica esaurini \n Vuoi prenotare un posto in classe economica?") == true) {
                economica(n);
            } else {
                alert("Il prossimo volo parte tra 3 ore.")
            }
        }
    }

   function prenota()
   {
       var n=document.getElementById("n1").value;
       if (n == "")
           alert("Inserire un nome");
       else
       {
           var x=document.getElementById("set").selectedIndex;
           if (x == 0) {
               economica(n);    
           }
           else {
               prima(n);
           }
       }
   }
</script>
<body>
<center>
    <h2 style="color:red"> 
        Benvenuti al sistema di prenotazione<br />
    </h2>
    <form>


    Nome: <input type="text" id="n1" /> <br /><br />
    Classe: <select id="set">
                <option value="Economica" selected="selected">Economica</option>
                <option value="Prima">Prima</option>
            </select>
    <br /><br />
        <button type="button" onclick="prenota()">Prenota</button>
    <br /><br />
     <table id="tabe" border="1">
      <tr>
        <td id="t1" >Seat Number</td>
        <td id="t2">Name</td>       
        <td id="t3">Class</td>
      </tr>
      <tr>
        <td>1</td>
        <td>
            <span id="e1">Libero</span>
        </td>       
        <td>Economica</td>
      </tr>
      <tr>
        <td>2</td>
        <td >
            <span id="e2">Libero</span>
        </td>       
        <td>Economica</td>
      </tr>
         <tr>
        <td>3</td>
        <td>
            <span id="e3">Libero</span>
        </td>       
        <td>Economica</td>
      </tr>
         <tr>
        <td>4</td>
        <td>
            <span id="e4">Libero</span>
        </td>       
        <td>Economica</td>
      </tr>
         <tr>
        <td>5</td>
        <td>
            <span id="e5">Libero</span>
        </td>       
        <td>Economica</td>
      </tr>
         <tr>
        <td>6</td>
        <td>
            <span id="p1">Libero</span>
        </td>       
        <td>Prima</td>
      </tr>
         <tr>
        <td>7</td>
        <td>
            <span id="p2">Libero</span>
        </td>       
        <td>Prima</td>
      </tr>
         <tr>
        <td>8</td>
        <td>
            <span id="p3">Libero</span>
        </td>       
        <td>Prima</td>
      </tr>
         <tr>
        <td>9</td>
        <td>
            <span id="p4">Libero</span>
        </td>       
        <td>Prima</td>
      </tr>
         <tr>
        <td>10</td>
        <td>
            <span id="p5">Libero</span>
        </td>       
        <td>Prima</td>
      </tr>
    </table>

</center>
</form>

1 个答案:

答案 0 :(得分:1)

您有输入错误导致此错误:

eco[i] == false;

应该是

eco[i] = false;

第二个功能中出现同样的错误 - DRY

您还需要将for循环条件更改为i < 5i <=4

两个函数再次出现相同的错误 - DRY