2016-08-03 4 views
-3

if文とor ||を使用しようとしています。部屋のタイプの選択ボックスオプションの値がシングルキング、シングルダブルクイーン、またはシングルツインのいずれかであるかどうかをテストしてから、いくつかのコードを実行します。私は、メッセージ "uncaught構文エラーの不明なトークンを取得し続ける| |"右のところで||演算子はif文の中にあります。javascriptでOr演算子を使用する方法

var hotel = { 
 
\t name:"Benson hotel", 
 
\t rooms:500, 
 
\t jsuites:10, 
 
\t ssuites:10, 
 
\t booked:100, 
 
\t checkAvailability: function(){ 
 
\t return this.rooms - this.booked; 
 
} 
 
     
 
}; 
 

 
hotel.name = "Bonton"; 
 

 
var elName = document.getElementById("hotelN"); 
 
elName.textContent = hotel.name; 
 

 

 
function checkin(){ 
 
    \t var nRooms = document.getElementById("numRooms").value * 1; 
 
    \t var tRoom = document.getElementById("typeRoom"); 
 
    \t if (tRoom.value = "singleK")||(tRoom.value ="singleQ")||(tRoom.value = "singleT") { //*I am getting the following message about above line of code in the console. "uncaught syntax error unidentified token ||" Obviously the above if statement with || operator is not correct// 
 
    \t \t 
 
    \t \t hotel.booked = numRooms + hotel.booked; 
 
    \t 
 
    \t \t if hotel.checkAvailability() < 0 { 
 
    \t \t \t var rAvail = nRooms + (hotel.checkAvailability()); 
 
    \t \t \t if rAvail <= 0 { 
 
    \t \t \t \t var noSay = document.getElementById("say"); 
 
    \t \t \t \t noSay.textContent = "We have no rooms available"; 
 
      } 
 
      
 
      else { 
 
       \t var yesSay = document.getElementById("say"); 
 
      \t yesSay.textContent = "We have" + rAvail + "rooms available"; 
 
      
 
    \t \t  } 
 

 
    \t \t  }
<body> 
 
\t <div id="wrapper"> 
 
\t \t <div id="heading"> 
 
\t \t \t <h1 id="hotelN"></h1> 
 
     /div> 
 
\t \t <div id="main"> 
 
\t \t \t <form id="myForm"> 
 
\t \t \t <fieldset> 
 
\t \t \t <legend>Book a Room</legend><p> 
 
\t \t \t <label for="rm1">Number of Rooms:</label> \t 
 
      <input type="number" max="10" min="1" value="1"name="rm" id="numRooms"> 
 
      Type of room: <select type="text" maxlength="14" id="typeRoom"> 
 
    <option value="singleK">Single King Bed</option> 
 
    <option value="singleQ">Single Queen Bed</option> 
 
    <option value="singleT">Single Two Twins</option> 
 
    <option value="jsuite">Junior One Bedroom Suite</option> 
 
    <option value="srsuite">Senior Two Bedroom Suite</option> 
 
</select></p> 
 
      Occupancy:<select type="text"> 
 
      <option>One adult</option> 
 
      <option>Two adults</option> 
 
      <option>One adult and Child</option> 
 

 
      </select> 
 
      
 
      Check In Date: <input type="date"name="checkin" id="cInDate"> 
 
      Check Out Date: <input type="date" name="checkout" id="cOutDate">   
 
      <button type="button" onclick="checkin()">Submit</button> 
 
</fieldset> 
 
\t \t \t </form> 
 
\t \t \t <H1 class="al">Hotel Room Availability</H1> 
 
\t \t  <p class="al" ><span id="say"></span> and check out on July <span id="dateOut"></span></p> 
 
\t \t \t 
 
      
 
\t \t 
 
\t \t <p id="first">jjjj</p> 
 
     
 
\t \t <p id="second"></p> 
 

 
\t \t </div> \t 
 
</div>

+2

if hotel.checkAvailability()<0' - 実際のコードやペーストエラーでは?すべての条件を括弧で囲む必要があります。 – tymeJV

+0

もし 'if rAvail <= 0'にもエラーがあります。括弧が必要: 'if(rAvail <= 0) ' – 4castle

答えて

4

あなただけの括弧のペアを追加する必要があります。

if ((tRoom.value == "singleK")||(tRoom.value =="singleQ")||(tRoom.value == "singleT")){ 
... 
} 

それとも

if (tRoom.value == "singleK"||tRoom.value =="singleQ"||tRoom.value == "singleT"){ 
... 
} 
+1

' = 'を' ==='に変更 – 4castle

+0

ありがとう!編集が完了しました。 –

+1

括弧内のすべての条件をラップする必要はありません。 1セットですべてをラップすることができます。 – zfrisch

0

あなたif文の条件全体のセクションがでラップする必要があります括弧。 if (condition1) || (condition2)を実行すると、||の前に条件が終了します。

それは、代わりにこのようになります:あなたが探している条件演算子は==、ない=です:さらに if ((condition1) || (condition2))

==に、と2つの値を比較するのに対し、=はに使用されています。

0
if (tRoom.value = "singleK")||(tRoom.value ="singleQ")||(tRoom.value = "singleT") { 

あなたはカント近いif()||

​​
0

あなたはその行と少なくとも2つの問題を持っている:

  1. あなたの主な問題は、あなたが一組のブラケットを逃しているということです条件の全体について:

    if ((tRoom.value = "singleK") || ...) {

あなたのor-operatorはちょうどフローティングです。

  1. 比較を行うには、===を使用する必要があります。 =が割り当てに使用されます。
関連する問題