如何将switch语句放入javascript代码?

时间:2015-10-02 10:59:00

标签: javascript jquery

我正忙着制作一个漂亮的工具来测量点击心跳,然后点击它会告诉你endUsers的平均心率。

它工作正常,当10次点击结束时,它将考虑我的数组并计算平均值和alert();具有平均值的用户。

我现在要做的不是用他们的平均心率警告endUser,而是通过诊断提醒他们。因此,当平均值等于59以下的值时,它应该警告(“你的心脏有效且适合”);如果它高于100,它应警惕(“你的心脏无效......”);你明白了。

我的问题:我似乎无法确定我们在哪里放置switch语句,因为错误会告诉我:找不到变量(我想要使用)在switch语句中)或当我将switch语句放在其他地方时,它会以默认情况警告用户..

我应该使用

  

av = average / = count;

我的switch语句?我希望它做的就是根据案例发出警告,这些案件都是基于平均值。

我的代码

没有switch语句的正常工作代码:

<script>
  var lastTapSeconds = 0;
  var bpm = 0;

  //extra variabelen voor functionaliteit uitbreiding.
  var beats = [];
  var average = 0;
  var count = 0;

  var tapDiv = document.getElementById("tapDiv");

  $(tapDiv).on('click', function() {
    var tapSeconds = new Date().getTime();

    bpm = ((1 / ((tapSeconds - lastTapSeconds) / 1000)) * 60);
    lastTapSeconds = tapSeconds;
    tapDiv.innerHTML = '<h1 style="display:inline;">' + Math.floor(bpm) + '</h1><img style="height:256px;width:256px;" src="img/heart.png"/>';

    //extra functionaliteit
    beats.push(Math.floor(bpm));
    average *= count;  //average = average * count
    average += Math.floor(bpm);  //average = average + count
    count++;
    average /= count;  //average = average / counterIncrement


    //als array entries 10 heeft bereikt geef prompt met gemiddelde bpm.
    if(beats.length >= 10) {
      alert("Your Average Beats Per Minute: " + average);
    }
  });
</script>

更新后的代码

 <script>
  var lastTapSeconds = 0;
  var bpm = 0;

  //extra variabelen voor functionaliteit uitbreiding.
  var beats = [];
  var average = 0;
  var count = 0;

  var tapDiv = document.getElementById("tapDiv");

  $(tapDiv).on('click', function() {
    var tapSeconds = new Date().getTime();

    bpm = ((1 / ((tapSeconds - lastTapSeconds) / 1000)) * 60);
    lastTapSeconds = tapSeconds;
    tapDiv.innerHTML = '<h1 style="display:inline;">' + Math.floor(bpm) + '</h1><img style="height:256px;width:256px;" src="img/heart.png"/>';

  //extra functionaliteit
  beats.push(Math.floor(bpm));
  average *= count;  //average = average * count
  average += Math.floor(bpm);  //average = average + count
  count++;
  average /= count;  //average = average / counterIncrement


  //als array entries 10 heeft bereikt geef prompt met gemiddelde bpm.
  if(beats.length >= 10) {
  //alert("Your Average Beats Per Minute: " + average);


  var av = average /= count;


  switch(av) {

    case (average>60 && avarage<100):
    alert("From the measurements, we conclude that you have a normal resting heart rate.");
    break;


    case (average<59):
    alert("From the measurements, we conclude that you have an efficient heart function and better cardiovascular fitness.");
    break;

    case (average>100):
    alert("From the measurements, we conclude that your resting heart has an high average, we might be stated as unefficient and not fit.");
    break;

    default:
    alert("Please measure again, your measurements seem unregular.");
    break;

  }
  var bpm = 0;
  var average = 0;
}
});

2 个答案:

答案 0 :(得分:1)

最好使用多个if-else语句:

if ( average > 60 && average < 100 )
    alert( "From the measurements, we conclude that you have a normal resting heart rate." );
else if ( average < 59 )
    alert( "From the measurements, we conclude that you have an efficient heart function and better cardiovascular fitness." );
else if ( average > 100 )
    alert( "From the measurements, we conclude that your resting heart has an high average, we might be stated as unefficient and not fit." );
else
    alert( "Please measure again, your measurements seem unregular." );

代码中的问题是case中的值是在运行时计算的,并且变为等于truefalse。因此,每次default部分都应该执行

答案 1 :(得分:0)

你的开关错了。您应该阅读使用开关。每种情况:部分应该具有变量av的一个可能值。不是有条件的。在您的情况下,例如,检查av(平均值> 60&amp;&amp; 100&lt; 100),其为真或假。因此,除非av本身是真或假,否则你的开关没有意义。

您应该使用常规if语句解决此问题。它不能用开关完成。