2013-12-16 46 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

相關問題