2013-12-16 47 views
1

我有我的茉莉花某些情况下的测试场景.. 但我无法获得这三个场景的测试用例... 可以吗告诉我该怎么办呢?应该返回的数字是阵列中的第一个或最后一个

  • 应返回的数字是在该阵列 未定义值的第一个或最后一个阵列中(或NaN,无穷远,...)\与 相同的绝对值号码(例如-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

看看代码,我会想象你的测试没有通过第二个测试,但应该通过第三个测试。这是因为你比较并返回值的绝对值。所以第二个测试将返回0.01而不是-0.01 – cbayram

+0

@cbayram:谢谢你的回复..你可以编辑我的小提琴,以及如何通过第二次测试http://jsfiddle.net/YYg8U/15/ – user3102494

回答

0

我要维持目前的代码,而不是优化。

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()函数来测试条目是否存在不是一个数字或者是未来的typeof ==“号”可能更合适,你应该不希望布尔和字符串数值例如值

修订

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; 
} 
(例如 “2”,假将根据isNaN测试是数字)

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

+0

感谢您的回复...但是这种场景下的数据 应该返回的数字是数组中的第一个或最后一个数组中未定义的值(或NaN,Infinity,...)\具有相同绝对值的数字(例如-0.01和0.01) – user3102494

+0

@ user3102494我很困惑,不明白这意味着什么。你的意思是,如果函数最接近零,函数应该返回-0.01和0.01?此外,数组(或NaN,Infinity,...)\“中的数组未定义值是什么意思? – cbayram

+0

你的意思是说,函数应该返回-0.01和0.01,如果他们是最接近零?...不知道该怎么回报这种情况...你建议 – user3102494

相关问题