我有我的茉莉花某些情况下的测试场景.. 但我无法获得这三个场景的测试用例... 可以吗告诉我该怎么办呢?应该返回的数字是阵列中的第一个或最后一个
- 应返回的数字是在该阵列 未定义值的第一个或最后一个阵列中(或NaN,无穷远,...)\与 相同的绝对值号码(例如-0.01和0.01)
提供我的小提琴太
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
@cbayram:谢谢你的回复..你可以编辑我的小提琴,以及如何通过第二次测试http://jsfiddle.net/YYg8U/15/ – user3102494