Javascript 应该返回的数字是数组中的第一个或最后一个

Javascript 应该返回的数字是数组中的第一个或最后一个,javascript,jquery,unit-testing,testing,jasmine,Javascript,Jquery,Unit Testing,Testing,Jasmine,在我的jasmine中,我有一些特定案例的测试场景。。 但我无法获得这三种场景的测试用例。。。 你能告诉我怎么做吗 应该返回的数字是数组中的第一个或最后一个 数组中未定义的值(或NaN、无穷大…)\n带有 相同的绝对值(例如-0.01和0.01) 也提供我的小提琴 茉莉花 describe( 'getClosestToZero', function () { it( 'finds a positive unique number near zero', function () {

在我的jasmine中,我有一些特定案例的测试场景。。 但我无法获得这三种场景的测试用例。。。 你能告诉我怎么做吗

  • 应该返回的数字是数组中的第一个或最后一个 数组中未定义的值(或NaN、无穷大…)\n带有 相同的绝对值(例如-0.01和0.01)
也提供我的小提琴

茉莉花

describe( 'getClosestToZero', function () {
    it( 'finds a positive unique number near zero', function () {
        expect( getClosestToZero( [-1, 0.5, 0.01, 3, -0.2] ) ).toBe( 0.01 );
    } );

    it( 'finds a negative unique number near zero', function () {
        expect( getClosestToZero( [-1, 0.5, -0.01, 3, -0.2] ) ).toBe( -0.01 );
    } );

    // your method actually doesn't pass this test
    // think about what your method *should* do here and if needed, fix it
    it( 'finds one of multiple identical numbers near zero', function () {
        expect( getClosestToZero( [-1, 0.5, 0.01, 0.01, 3, -0.2] ) ).toBe( 0.01 );
    } );
} );

我将维护您当前的代码,而不是优化它

function getClosestToZero(set) {
  if(0 === set.length) return null;
  // you are assigning the actual value to the closest variable
  var closest = set[0], result = 0;
  for(var i in set) {
    // you are assigning the actual value to the next variable
    var next = set[i];
    // you are comparing the absolute values
    if(Math.abs(closest) > Math.abs(next)) {
      result = i;
      // but you are assigning the actual value
      closest = next;
    }
  }
  // thus your return value is the actual number and not its absolute
  return closest;  
}

您可以使用isNaN()函数来测试条目是否不是数字,或者如果您不需要布尔值和字符串数值(例如,“2”,false将是isNaN测试中的数字),那么typeof next==“number”可能更合适

已更新

function getClosestToZeroWithNaN(set) {
  var closest;
  if(set instanceof Array) {
    for(var i = 0; i < set.length; i++) {
        var val = set[i];
        if(!isNaN(val)) {
            val = Number(val);
            var absVal = Math.abs(val);
            if(typeof closest == 'undefined' || Math.abs(closest) > absVal) {
                closest = val; 
            }
        }
    }
  }
  return closest;
}
function getClosestToZeroOnlyNumbers(set) {
  var closest;
  if(set instanceof Array) {
    for(var i = 0; i < set.length; i++) {
        var val = set[i];
        if(typeof val == "number") {
            var absVal = Math.abs(val);
            if(typeof closest == 'undefined' || Math.abs(closest) > absVal) {
                closest = val; 
            }
        }
    }
  }
  return closest;
}
函数getClosestToZeroWithNaN(设置){ var最近; if(设置数组的instanceof){ 对于(变量i=0;iabsVal){ 最近值=val; } } } } 返回最近的位置; } 函数GetClosestToZeroOnlyNumber(集合){ var最近; if(设置数组的instanceof){ 对于(变量i=0;iabsVal){ 最近值=val; } } } } 返回最近的位置; }
看看NaN版本如何处理null、false和“2.12”

看看代码,我会认为您的测试没有通过第二个测试,但应该通过第三个测试。这是因为比较并返回值的绝对值。因此,第二次测试将返回0.01,而不是-0。01@cbayram:谢谢你的回复..你能编辑我的小提琴和如何通过第二次测试吗谢谢你的回复..但是在这种情况下,应该返回的数字是数组中第一个或最后一个未定义的值(或NaN,无穷大,…)\绝对值相同的数字(例如-0.01和0.01)@user3102494我很困惑,不明白这是什么意思。您的意思是,如果函数最接近于零,则函数应同时返回-0.01和0.01?此外,“数组中未定义的数组值(或NaN,无穷大…)\”是什么意思?您的意思是,如果它们最接近零,则函数应同时返回-0.01和0.01?…不确定在这种情况下返回什么…您需要什么suggest@user3102494哈哈,我不明白你的情况。此外,我对它的回报漠不关心。如果你的想法是按照名字的意思得到最接近于零的数字,请查看更新后的答案部分。@cybayram:谢谢你的回答……与其编写三个不同的函数,不如将其组合到一个函数中……我指的是一个场景中的所有内容。。。
function getClosestToZeroWithNaN(set) {
  var closest;
  if(set instanceof Array) {
    for(var i = 0; i < set.length; i++) {
        var val = set[i];
        if(!isNaN(val)) {
            val = Number(val);
            var absVal = Math.abs(val);
            if(typeof closest == 'undefined' || Math.abs(closest) > absVal) {
                closest = val; 
            }
        }
    }
  }
  return closest;
}
function getClosestToZeroOnlyNumbers(set) {
  var closest;
  if(set instanceof Array) {
    for(var i = 0; i < set.length; i++) {
        var val = set[i];
        if(typeof val == "number") {
            var absVal = Math.abs(val);
            if(typeof closest == 'undefined' || Math.abs(closest) > absVal) {
                closest = val; 
            }
        }
    }
  }
  return closest;
}