【问题标题】:number that should be returned is the first or last in the array应该返回的数字是数组中的第一个或最后一个
【发布时间】:2013-12-16 17:25:32
【问题描述】:

我的 jasmine 中有某些案例的测试场景.. 但我无法获得这三个场景的测试用例...... 你能告诉我怎么做吗...

  • 应该返回的数字是数组中的第一个或最后一个 数组中未定义的值(或 NaN、Infinity、...)\ 数字与 绝对值相同(例如 -0.01 和 0.01)

也提供我的小提琴

http://jsfiddle.net/YYg8U/15/

var myNumbersToSort = [-1, 2, -3, 4, 0.3,1,-0.001];


function getClosestToZero(set) {
  if(0 === set.length) return null;
  var closest = Math.abs(set[0]), result = 0;
  for(var i in set) {
     var next = Math.abs(set[i]);
     if(closest > next) {
       result = i;
       closest = next;
     }
  }
  return closest;  
}

document.getElementById('output').innerHTML = (getClosestToZero(myNumbersToSort));

茉莉花

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 );
    } );
} );

【问题讨论】:

  • 查看代码,我想您的测试没有通过第二个测试,但应该通过第三个测试。这是因为您比较并返回值的绝对值。所以第二个测试将返回 0.01 而不是 -0.01
  • @cbayram:感谢您的回复..您能编辑我的小提琴以及如何通过第二次测试jsfiddle.net/YYg8U/15

标签: javascript jquery unit-testing testing jasmine


【解决方案1】:

我将维护您当前的代码,而不是对其进行优化。

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;  
}

http://jsfiddle.net/N4HLT/

您可以使用 isNaN() 函数来测试条目是否不是数字,或者如果您不希望使用布尔值和字符串数值(例如“2”, false 将是根据 isNaN 测试的数字)

更新

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;
}

http://jsfiddle.net/hB2T8/看看NaN版本如何处理null、false和“2.12”

【讨论】:

  • 感谢您的回复...但是在这种情况下,应该返回的数字是数组中的第一个或最后一个未定义值(或 NaN,Infinity,...)\ 数字与绝对值相同(例如 -0.01 和 0.01)
  • @user3102494 我很困惑,不明白这意味着什么。您的意思是如果函数最接近零,则该函数应返回 -0.01 和 0.01?此外,“数组中的数组未定义值(或NaN,Infinity,...)\”是什么意思?
  • 您的意思是,如果函数最接近于零,则该函数应返回 -0.01 和 0.01?...不确定在这种情况下要返回什么...您的建议
  • @user3102494,哈哈,我不明白你的情况。此外,我对它返回的内容漠不关心。如果想得到最接近零的数字,顾名思义,请查看更新的答案部分。
  • @cybayram:感谢您的回复...与其编写三个不同的函数,是否可以将其组合在一个函数中...我的意思是在一个场景中的所有内容...jsfiddle.net/hB2T8
猜你喜欢
  • 2014-09-16
  • 2021-02-15
  • 2021-03-05
  • 2019-07-17
  • 2013-09-14
  • 2014-10-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多