1

I'm doing an exercise for school in javascript, I've to simulate a booking system for flights. To see who has already booked I created two boolean arrays ( one per class ) , and when you book the place I put it to false , the problem is that arrays aren't put to false...any solution? and sorry for the bad english !

<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>

2
  • For one you need 5 here: for (var i = 0; i < 5; i++) or give the spans a class and loop over document.querySelectorAll("spanclass") or just if (pri[i] == true) { document.getElementById("p"+(i+1)).innerHTML = n; } Commented Dec 6, 2015 at 17:23
  • The repetitions in your code make my eyes bleed. Please factorise your code. You should have a single function for coach and first, and the switch does not deserve to live. Commented Dec 7, 2015 at 1:11

1 Answer 1

1

You have a typo causing this error:

eco[i] == false;

should be

eco[i] = false;

And the same error in your second function - DRY

You need to change the for loop condition as well, to i < 5 or i <=4

Once again same error in both functions - DRY

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.