codewars
Code Kata
高中後比較少練解題的題目
應該說是"玩"解題的題目
解出來的成就感就是棒!!
在CodeWars看到這一到題目
其實挺數學題的
Sample Tests:
fixture.random(10,0);
fixture.random(10,10);
fixture.random(10,100);
fixture.random(10,1000);
Test.describe("Testing with [1, 3, 4]", function() {
Test.assertEquals(findMissing([1, 3, 4]), 2);
});
我的解法在下面先自己做看看再反白,
有興趣的高手懇請提供您的解法讓我學習學習拜託
--------------------------------------------------------------------
var findMissing = function (list) {
var first = list[0];
var last = list[list.length-1];
//取得公差
var com_diff = (last - first)/(list.length);
//拿公差去跑迴圈
for (var i =0 ; i< list.length-1; i++){
if ((list[i+1] - list[i])!=com_diff){
return list[i] + com_diff;
}
}
}
-------------------------------------------------------------
神人解答-