在平局的情况下,如何在javascript中返回较小的数字?

时间:2017-05-08 22:06:47

标签: javascript arrays if-statement min

我需要编写一个带两个输入的函数,' target' (整数)和'值' (整数列表),并找出'值中的哪个数字'最接近' target'。我想出了以下内容:

var targetNum = 0;
var valuesArr = [2, 4, 6, 8, 10];

function closestToTarget(target, values) {
    var currVal = values[0];
    var diff = Math.abs(target - currVal);
    for (var i = 0; i < values.length; i++) {
        var currDiff = Math.abs(target - values[i]);
        if (currDiff < diff) {
            diff = currDiff;
            currVal = values[i];
        }
    }
    return currVal;
}
alert(closestToTarget(targetNum, valuesArr));

该功能有效,但在平局的情况下,我无法返回较小的值。我能想到的最好的是以下哪些不起作用:

function closestToTarget(target, values) {
    var currVal = values[0];
    var diff = Math.abs(target - currVal);
    for (var i = 0; i < values.length; i++) {
        var currDiff = Math.abs(target - values[i]);
        if (currDiff < diff) {
            diff = currDiff;
            currVal = values[i];
        }
        else if (currDiff == diff) {
            return Math.min[currVal, values[i]];
        }
        else {
            return currVal[i - 1];
        }
    }
    return currVal;
}

如果是平局,我如何在JavaScript中返回较小的数字?

5 个答案:

答案 0 :(得分:1)

您的代码看起来会起作用尝试将Math.min[currVal, values[i]]更改为Math.min(currVal, values[i]),如果您的值是有序的(简单语法问题),该代码应该有效。

正如kojow7所提到的,只要订购了values,第一个函数也应该有用。

否则代替return Math.min(...),您应该尝试currVal = Math.min(...)并继续迭代,然后在循环后返回currVal。这只是确保您检查values数组中的每个值,以确保您没有错过任何内容(但如果已经订购,那么就不需要继续检查因为差异只会增加。)

答案 1 :(得分:1)

除语法错误(缺少括号)外,您可以使用一些ES6语法编写更简洁的函数:

&#13;
&#13;
function closestToTarget(target, values) {
    const m = Math.min(...values.map( v => Math.abs(target - v)));
    return Math.min(...values.filter( v => Math.abs(target - v) === m ));
}

var valuesArr = [2, 4, 6, 8, 10];
// Try several numbers
for (var i = 1; i < 12; i++) {
    console.log(i, closestToTarget(i, valuesArr));
}
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

答案 2 :(得分:0)

诀窍在于,当您找到新的最佳差异,并记录差异(bestDiff)时,您还会记录导致差异的值(bestVal)。

现在,如果您遇到平局(currDiff == bestDiff),您需要检查它是否更好平局,即检查当前值是否小于录制的(currVal < bestVal)。如果是,则我们会更新最佳值(同时更新bestDiffbestVal)。

function closestToTarget(target, values) {
    var currVal = values[0];
    var bestDiff = Math.abs(target - currVal);
    var bestVal = currVal;

    for (var i = 1; i < values.length; i++) {
        var currVal = values[i];
        var currDiff = Math.abs(target - values[i]);
        if (currDiff < bestDiff || (currDiff == bestDiff && currVal < bestVal)) {
            bestDiff = currDiff;
            bestVal = currVal;
        }
    }
    return bestVal;
}

示例:

closestToTarget(2,[2,4,6,8,10]) // 2
closestToTarget(2,[10,8,6,4,2]) // 2
closestToTarget(3,[10,8,6,4,2]) // 2

答案 3 :(得分:0)

你的想法应该有效,但是你也可以使用你的第一个功能,但最后会添加一些检查结果较低的东西:

function closestToTarget(target, values) {
    var currVal = values[0];
    var diff = Math.abs(target - currVal);
    for (var i = 0; i < values.length; i++) {
        var currDiff = Math.abs(target - values[i]);
        if (currDiff < diff) {
            diff = currDiff;
            currVal = values[i];
        }
    }

    // We found the closest but now check if there's a smaller tie
    if (currVal > target && values.indexOf(target - diff) > -1 ) {
        return target - diff;
    }
    else {
    // if not just return what we originally found
        return currVal;
    }
}

https://jsfiddle.net/vsj0q5u9/2/

答案 4 :(得分:0)

function findClosest(inpt, ar) {
   var minDiff = Math.abs(ar[0] - inpt);
   var res = ar[0];
   for (var i = 1; i < ar.length; i++) {
       if(Math.abs((ar[i] - inpt)) < minDiff)
           res = ar[i];
   }
   return res;
}

这是关于如何为给定输入获取数组中最接近值的简单示例。 如果出现平局,将不会返回较低的数字。如果数组中有多个最接近的整数,则输入之间的差异对于所有平局情况都是相同的。所以不要担心。