Skip to content

Instantly share code, notes, and snippets.

@dstyle0210
Created July 13, 2016 04:45
Show Gist options
  • Select an option

  • Save dstyle0210/bdd81153829b9808ab30bdaa8addb23d to your computer and use it in GitHub Desktop.

Select an option

Save dstyle0210/bdd81153829b9808ab30bdaa8addb23d to your computer and use it in GitHub Desktop.
[코딩도장] 다음 입사문제 중에서
// 코딩도장 공부
// http://codingdojang.com/scode/408
// 임시 데이터 생성.
var data = (function(){
var arr = [];
for(i=0;i<20;i++){
arr[i] = Math.floor(Math.random()*500);
};
// 데이터 정렬
arr.sort(function(a, b){
return a-b
});
// 데이터 중복 제거
var uniq = arr.reduce(function(a,b){
if (a.indexOf(b) < 0 ) a.push(b);
return a;
},[]);
return uniq;
})();
console.log(data);
// 각 데이터간의 차 구하기.
var min;
var minIdx;
for(idx=0;idx<data.length;idx++){
var m = Number(data[idx+1]) - Number(data[idx]);
if(!min || m<min){
min = m;
minIdx = idx;
console.log("차이"+min+", 인덱스"+minIdx);
};
}
// 최소차이 나는 인덱스 기준으로 결론 도출
console.log( [data[minIdx],data[minIdx+1]] );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment