2016-12-18 3 views
-3

次のコードがあります。何らかの理由で、私が実行したくないcase文(Case '2'の2番目)が正しいもの(Case '4')に加えて実行されています。オンラインで読むと、ほとんどのpplは「break」文を追加することで同様の問題を解決することができました。しかし、それは私のために働いていません。以下advise.Outputは私のブラウザJavaScriptでswitch文が正しく機能しない

var chk = '4' 
 

 
switch (chk) { 
 
    case '4': 
 
    var locations = [ 
 
     ["936001_STURGEON_BAY_MEYER", 44.8358, -87.3305, "LRA", 1], 
 
     ["936087_SHADOW_LAKE", 45.2183, -88.5981, "LRA", 2], 
 
     ["936136_PIG", 44.5925, -88.0808, "OMS", 3], 
 
     ["936136_PIG", 44.5925, -88.0808, "OMS", 4] 
 
    ]; 
 

 
    var map = new google.maps.Map(document.getElementById('map'), { 
 
     zoom: 15, 
 
     center: new google.maps.LatLng(locations[0][1], locations[0][2]), 
 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
 
    }); 
 

 
    var infowindow = new google.maps.InfoWindow({ 
 
     maxWidth: 400000 
 
    }); 
 
    var locations_all_cells1 = locations; 
 
    var marker, i; 
 

 
    for (i = 0; i < locations_all_cells1.length; i++) { 
 
     var type1 = locations_all_cells1[i][3]; 
 

 
     switch (type1) { 
 
     case "OMS": 
 
      marker1 = new google.maps.Marker({ 
 
      position: new google.maps.LatLng(locations_all_cells1[i][1], locations_all_cells1[i][2]), 
 
      map: map, 
 
      icon: 'http://maps.google.com/mapfiles/ms/icons/green-dot.png' 
 
      }); 
 
      google.maps.event.addListener(marker1, 'click', (function(marker1, i) { 
 
      return function() { 
 
       infowindow.setContent(locations_all_cells1[i][0]); 
 
       infowindow.open(map, marker1); 
 
      } 
 
      })(marker1, i)); 
 
      break; 
 

 
     case "LRA": 
 
      marker2 = new google.maps.Marker({ 
 
      position: new google.maps.LatLng(locations_all_cells1[i][1], locations_all_cells1[i][2]), 
 
      map: map, 
 
      icon: 'http://maps.google.com/mapfiles/ms/icons/red-dot.png' 
 
      }); 
 
      google.maps.event.addListener(marker2, 'click', (function(marker2, i) { 
 
      return function() { 
 
       infowindow.setContent(locations_all_cells1[i][0]); 
 
       infowindow.open(map, marker2); 
 
      } 
 
      })(marker2, i)); 
 
      break; 
 

 
     case "UPSAVE": 
 
      marker3 = new google.maps.Marker({ 
 
      position: new google.maps.LatLng(locations_all_cells1[i][1], locations_all_cells1[i][2]), 
 
      map: map, 
 
      icon: 'http://maps.google.com/mapfiles/ms/icons/yellow-dot.png' 
 
      }); 
 
      google.maps.event.addListener(marker3, 'click', (function(marker3, i) { 
 
      return function() { 
 
       infowindow.setContent(locations_all_cells1[i][0]); 
 
       infowindow.open(map, marker3); 
 
      } 
 
      })(marker3, i)); 
 
     } 
 
    } 
 
    break; 
 

 
    case '2': 
 
    var locations = [ 
 
     ["936001_STURGEON_BAY_MEYER", 44.8358, -87.3305, "LRA", 1], 
 
     ["936087_SHADOW_LAKE", 45.2183, -88.5981, "LRA", 2], 
 
     ["936136_PIG", 44.5925, -88.0808, "OMS", 3], 
 
     ["936136_PIG", 44.5925, -88.0808, "OMS", 4] 
 
    ]; 
 

 
    var map = new google.maps.Map(document.getElementById('map'), { 
 
     zoom: 15, 
 
     center: new google.maps.LatLng(locations[0][1], locations[0][2]), 
 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
 
    }); 
 

 
    var infowindow = new google.maps.InfoWindow({ 
 
     maxWidth: 400000 
 
    }); 
 
    var locations_all_cells = ; 
 
    var marker, i; 
 

 
    for (i = 0; i < locations_all_cells.length; i++) { 
 
     var type1 = locations_all_cells[i][3]; 
 
    } 
 
    break; 
 
    
 
    default: 
 
    text = "Looking forward to the Weekend"; 
 
} 
 
console.log(text);

エラー情報

enter image description here

+1

あなたは 'type1'を定義していません!そして、私は間違いなく確信しています。これはケース2には行かないでしょう...もしそうなら、 'chk = '4';'を文字列として作ってください。 –

+0

上記のコードでは、デフォルトのブランチだけが実行されます(どちらのケースも '4'と一致しないため、 '2'も '4'もありません)。 '4!== '4''に注意してください。 – Paulpro

+0

@Paulpro 'switch'は厳密なチェックをしますか? –

答えて

0

を私は最終的に友人からの助けを借りて、私の問題を働きました。 Javascriptは、case文の中で初期化されていない変数を好きではないことが判明しました。ケースステートメントの1つでは、前にロジックに基づいて値を割り当てていた変数を使用していました。このケースのステートメントは「真」と評価されませんでしたが、私のプログラムはエラーを投げていました。 bは/空白nullの場合は、プログラム以下のコードでたとえば

は、mが「偽」であるにもかかわらず、例nとmのためにエラーになります:

switch(expression) { 
case n: 
    break; 
case m: 
    Var a=b; 
    break; 
default: 
    default code block 
} 
1

からMDN Docs for switchからですしてください:

switchステートメントは、最初にそれを評価しますsの表現。次に、式が入力式(厳密な比較を使用して===)の結果と同じ値に評価される最初のcase節を探し、を呼び出して、その句に制御を移し、関連する文を実行します。

switchあなた'4'(文字列)と4(番号)と一致しない厳格なチェックを行います。だから、あなたはあなたのようにコードを変更する必要があります。

var chk = '4'; 

更新

をお使いのブラウザが実際にchk = '4'のように、それを正しくロードされている場合は、それが動作するはずです。ここを参照してください:

var chk = '4'; 
 
// Added this for checking. 
 
var type1 = 'OMS'; 
 

 
switch (chk) { 
 
    case '4': 
 
    switch (type1) { 
 
     case "OMS": 
 
     text = "4 - OMS"; 
 
     break; 
 
     case "LRA": 
 
     text = "4 - LRA"; 
 
     break; 
 
     case "UPSAVE": 
 
     text = "4 - UPSAVE"; 
 
    } 
 
    break; 
 
    case '2': 
 
    // ####Do some Stuff here## 
 
    break; 
 
    default: 
 
    text = "Looking forward to the Weekend"; 
 
} 
 
console.log(text);

+0

@ssharmaブラウザのコードが確かに 'chk = '4' 'なら、それは間違いなく人を動かすはずです。あなたは正しいコードを投稿できますか? –

+0

私は実際に変数chkの値を取得していますvar chk = '<%= @tag %>'バックグラウンドでいくつかの他のものを行うことによって。私は私のブラウザをチェックし、出力は実際にvar chk = '4'として来ています。まだ運がない! – ssharma

+0

@ssharmaブラウザに表示されたコードをすべて投稿してください。 –

関連する問題