需要帮助清除简单 jQuery 函数中的编译器错误

Need help clearing compiler errors from simple jQuery function

在我的 jQuery 文件中,我有以下脚本:

function incDistInput(){

$(".incInputBtn").on("click", function() {

    var $button = $(this),
        oldValue = $button.siblings("input").val(),
        quantity = oldValue.split(' miles');

    if ($button.hasClass('plusBtn')) {
        var newVal = parseFloat(quantity[0]) + 1;
    } else {

    if (quantity[0] > 0) {
        var newVal = parseFloat(quantity[0]) - 1;
        } else {
        newVal = 0;
        }
    }

    $button.siblings("input").val(newVal + ' miles');

});

}

此函数在编译时出现以下错误:

var newVal = parseFloat(quantity[0]) - 1;
'newVal' is defined but never used. — column 17
'newVal' is already defined. — column 24

newVal = 0;
'newVal' used out of scope. — column 13

$button.siblings("input").val(newVal + ' miles');
'newVal' used out of scope. — column 39

如何在不改变函数输出的情况下重新排列或定义这些变量以清除错误?

尝试在 if 语句之外初始化 newVal,以便它可以被识别..

var $button = $(this),
    oldValue = $button.siblings("input").val(),
    quantity = oldValue.split(' miles')
    var newVal = 0;

if ($button.hasClass('plusBtn')) {
    newVal = parseFloat(quantity[0]) + 1;
} else {

if (quantity[0] > 0) {
    newVal = parseFloat(quantity[0]) - 1;
    } else {
    newVal = 0;
    }
}

$button.siblings("input").val(newVal + ' miles');