【问题标题】:How to finish a function when conditions met?当条件满足时如何完成一个函数?
【发布时间】:2016-06-07 02:02:11
【问题描述】:

目前是 JS 的新手,并创建了一个工资计算器,我有 4 个不同的条件可以满足,比如说 a1 a2 b1 b2,每个条件都有不同的工资率。 When the hourly pay rate is selected and the hours worked is input, it simply multiplies the hours worked by the pay rate, and leaves a thank you message in a text box at the bottom.我的问题是,函数不会在它应该的条件下停止,它会一直到最后一个 if 语句。说 a2 是满足的条件,它会执行那个条件,但它不会停止,并继续到最后一个 if 语句。我尝试在每个 if 语句的末尾添加一个返回码,但这会阻止完成函数发布感谢消息。任何帮助表示赞赏,谢谢。

var intName;
var intHours = 0,
  intQualifyType, intAgeType;
var curWage = 0;
var blnContinue;
var strMessage = "";
var intNumCalc = 0;
var curTotal = 0;


function calcWage() {
    intName = (document.getElementById('txtName').value);
    intQualifyType = (document.getElementById('optQualifyType').value);
    intAgeType = (document.getElementById('optAgeType').value);
    intHours = parseInt(document.getElementById('txtHours').value);

    if (intQualifyType == "Code1" || intAgeType == "Years2") {
      curWage = intHours * 1;
    }
    alert(curWage);

    if (intQualifyType == "Code1" || intAgeType == "Years1") {
      curWage = intHours * 2;
    }

    document.getElementById('txtTotal').value = "$" + curWage.toFixed(2);
    curWage = parseFloat(curWage);
    intNumCalc++;

    blnContinue = confirm("Do you have another quote to process");

    if (blnContinue) {
      clearall();
    } else {
      finishUp();
    }


  } //end of calwage

function clearall() {
    document.getElementById('txtName').value = "";
    document.getElementById('txtHours').value = "";
    document.getElementById('msgInvest').value = "";
    document.getElementById('txtTotal').value = "";
    document.getElementById('txtName').focus();

  } // end of clearall

function finishUp() {
    strMessage = "Thank you for using our new Wage Calculator " + intName + "! ";
    strMessage = strMessage + "For your hours this week you are currently owed $" + curWage.toFixed(2);
    document.getElementById('msgFinish').value = strMessage;
    intNumCalc = 0
    curTotal = 0;
  } //end of finishUp

【问题讨论】:

  • 输入是什么样的?


  • 您是否年满 18 岁?

标签: javascript function return


【解决方案1】:

我相信你的问题就在这里:

if(intQualifyType=="Code1" || intAgeType=="Years1")

和

if(intQualifyType=="Code1" || intAgeType=="Years2") 

当您使用“或”逻辑门时,两者都会运行。所以这两个表达式都在询问 intQualifyType 是否等于 Code1。 我认为您正在寻找的是一个“与”逻辑门,例如:

if(intQualifyType=="Code1" && intAgeType=="Years2")
// some more stuff here
if(intQualifyType=="Code1" && intAgeType=="Years2")  

【讨论】:

  • 试过了,没有运气
【解决方案2】:

@Dave Norm 有一个观点,如果那是你想要做的功能。

此代码将接受每个条件的 2 个可能的 ANSWERS 作为 TRUE,因为您使用的是 || > OR 运算符。

if (intQualifyType == "Code1" || intAgeType == "Years2") {
 curWage = intHours * 1;
}

alert(curWage);

if (intQualifyType == "Code1" || intAgeType == "Years1") {
  curWage = intHours * 2;
}

我还请客,您需要为您的流程使用 && AND 运算符。

您可以组合 2 语句为1。喜欢:

if(intQualifyType == "Code1" && intAgeType == "Years2") {
  curWage = intHours * 1;
}else if(intQualifyType == "Code1" && intAgeType == "Years1") {
  curWage = intHours * 2;
}

alert(curWage);

也许你需要添加:

return false;

如果您想在获得正确的条件后终止进程。

【讨论】:

  • 你和@Dave Norm 是对的,只是不得不摆弄我错了的其他一些编码,但你的方法有效。现在一切正常,谢谢!
猜你喜欢
  • 2016-03-09
  • 2020-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-10
  • 1970-01-01
  • 1970-01-01
  • 2023-02-16
相关资源
最近更新 更多