在 JavaScript 中制作一个 one rep max 计算器,不确定为什么我的函数不起作用

Making a one rep max calculator in JavaScript, not sure why my function isn't working

我正在使用 Epley 公式制作一个重复次数最多的计算器。当我调用该函数时,它说它未定义。但是,我认为通过使用也被解析为整数的参数weightreps,它们足以实现该功能。

Here is my fiddle.

我做错了什么?

HTML:

<div>
  <h2>Calculator for Epley Formula One Rep Max</h2>
  <p>Use the spaces below to type in the weight and reps for any lift to calculate any estimated 1-Rep Max using the Epley Formula:</p>
  <b>Weight</b>
  <input type="text" id="weight">
  <b>Reps</b>
  <input type="text" id="reps">
  <button id="button">Click Me</button>
  <br></br>
<hr>    
  <div id="demo">Go for it!</div>
</div>

JavaScript:

$("#button").click(function(){
    var weight = parseInt($('#weight').val());
    var reps   = parseInt($('#reps').val());
    $('#demo').html("");
    function calculateMax (weight, reps) {
        weight * (1 + (reps/30))
    };
    $('#demo').html("If you can lift " + weight + " for " + reps + " reps then you have an estimated max of " + calculateMax(weight, reps) + " !");
});

您想要 return 来自 calculateMax 函数的计算值

function calculateMax(weight, reps) {
    return weight * (1 + (reps/30));
}

这就是说,我不明白你为什么要为此使用一个单独的函数,为什么不这样写:

$("#button").click(function(){
    var weight = parseInt($('#weight').val());
    var reps   = parseInt($('#reps').val());
    var max = weight * (1 + (reps/30));
    $('#demo').html("If you can lift " + weight + " for " + reps + " reps then you have an estimated max of " + max + " !");
});

你需要使用return:

function calculateMax (weight, reps) {
    return weight * (1 + (reps/30))
};