我如何计算计算已经运行了多少次

时间:2019-06-10 09:14:03

标签: javascript loops

我正在创建一个程序,该程序需要两个输入,当前权重和目标权重。根据输入的不同,它显示不同的输出,例如电流:87和目标64,每周损失1.38,我希望它计算总和直到达到当前<=目标的次数。

我尝试进行循环,但是我没有足够的技能来完全理解循环的工作原理。

//Declare basic variables, prompt asks for input from user.
var current = prompt("Please enter your current weight");
var target = prompt("Please enter your target weight");
var weeks = 0; 
var loss = (current - target);
// If 0 is entered by user then the input text will display
if (current <= 0){
    document.write("Invalid input, please enter greater than 0 kg"); // Displays answer
}
// If 0 is entered by user then the input text will display
else if (target <= 0){
    document.write("Invalid input, please enter greater than 0 kg"); // Displays answer
}
else if (target >= current){
    document.write("Invalid input, please enter greater than 0 kg"); // Displays answer
}
// Calculate the weeks it takes to lose weight
else if (current > target){
    loss = (target - current);
    weeks = loss / 1.38;
    document.write(weeks.toFixed(0)); // Displays answer
}

我希望电流= 87和目标= 64的预期输出为“ 17周”。

3 个答案:

答案 0 :(得分:0)

首先,您需要替换该行以避免产生负面结果:

 this.medicineSupply = this.formBuilder.group({
 is_partial:[''],
 durationSupply:[''],
 durationSupplyTypeId: [''],
 resultStored:this.formBuilder.array([
  this.formBuilder.group({
      is_partial_id:[''],
      durationSupplyID_id:[''],
      durationSupplyTypeId_id:['']
    })
  ]),
})

具有:

loss = (target - current);

第二,在输出中添加“周”:

loss = (current - target);

document.write(weeks.toFixed(0) + ' weeks');

答案 1 :(得分:0)

我不知道您的代码的一般正确性,但是如果我只是对其进行修改以一遍又一遍地重新运行,直到获得有效的结果,然后算上,我会这样做:

var finished = false;
var count = 0;


while (!finished) {
    count++;
    //Declare basic variables, prompt asks for input from user.
    var current = prompt("Please enter your current weight");
    var target = prompt("Please enter your target weight");
    var weeks = 0; 
    var loss = (current - target);

    // If 0 is entered by user then the input text will display
    if (current <= 0){
       document.write("Invalid input, please enter greater than 0 kg"); // Displays answer
    }
    // If 0 is entered by user then the input text will display
    else if (target <= 0){
       document.write("Invalid input, please enter greater than 0 kg"); // Displays answer
    }
    else if (target >= current){
       document.write("Invalid input, please enter greater than 0 kg"); // Displays answer
    }
    // Calculate the weeks it takes to lose weight
    else if (current > target){
        finished = true;
        loss = (target - current);
        weeks = loss / 1.38;
        document.write(weeks.toFixed(0)); // Displays answer
    }
}

使用finished变量来跟踪是否应该再次运行循环,并且count变量在每次循环时都会递增。得到满意的结果时,将finished设置为true。

答案 2 :(得分:0)

function looseWeightEstimator() {
    let weeksToAchieve, currentWeight, targetWeight;
    weeksToAchieve = 0;
    currentWeight = prompt("Please enter your current weight");
    targetWeight = prompt("Please enter your target weight");



    for (let i = 0; currentWeight > targetWeight; i++) {
        weeksToAchieve = weeksToAchieve + 1
        currentWeight = currentWeight - 1.38;
    }
    alert(`It will take ${weeksToAchieve} weeks to achieve your target weight`);


}

looseWeightEstimator()