警报返回不止一次 [英] Alert returning more than once

查看:113
本文介绍了警报返回不止一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习JS,我正在尝试解决编码挑战。我想在我输入参数时告诉用户总发电机数量和总瓦数。问题是代码阅读器说我警告不止一个。我这样做是为了多次调用警报?这是我的第一次尝试:

I'm learning JS and I'm trying to solve a coding challenge. I'm suppose to have an alert tell the user the total generator amount and the total watts when I input the parameters. Problem is the code reader says that I'm alerting more than one. What am I doing that is making the alert be called multiple times? Here was my first attempt:

function changePowerTotal(totalMW,genID,status,powerMW){

  if(typeof(status) == "on" || typeof(status) == "ON"){
    alert("Generator #"+genID+" is now on, adding "+powerMW+" MW, for a total of "+ (totalMW) +" MW!");
    return false;
  } else {

    if(totalMW == 0){

       alert("Generator #"+genID+" is now off, removing "+powerMW+" MW, for a total of "+ (powerMW) +" MW!");

    } else {

       alert("Generator #"+genID+" is now off, removing "+powerMW+" MW, for a total of "+ (totalMW - powerMW) +" MW!");
    }

    return false;
  }
}
changePowerTotal(0,2,"off",62);

我也试过这个:

function changePowerTotal(totalMW,genID,status,powerMW){

  var genStatus = "";

  if(status === "on"){

   genStatus = " is now on, adding "
   totalMW = totalMW + powerMW;

  } else {

   genStatus = " is now off, removing "
   totalMW = totalMW - powerMW;

  }

  alert("Generator #"+genID+genStatus+powerMW+" for a total of "+totalMW+" MW!");

}

changePowerTotal(142,2,"off",62);


推荐答案

功能没有问题,我猜你偶然叫它两次?
请检查一下,这个功能无法提醒两次。你两次打电话一定是犯了错误。

There is no problem in the function, I am guessing you per accidentally called it twice? Please check if you did, there is no way this functions can alert twice. You must have made a mistake in calling it twice.

这篇关于警报返回不止一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆