Skip to content

Instantly share code, notes, and snippets.

@4ox
Last active November 8, 2017 03:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 4ox/884257f9aabaea1af0ebcc421c3d225e to your computer and use it in GitHub Desktop.
Save 4ox/884257f9aabaea1af0ebcc421c3d225e to your computer and use it in GitHub Desktop.
대각선 배열 위치구하기
//vars
var width = 12;
var height = 9;
var max = width * height;
//기본배열 만들어보기
function initArray() {
var nums = [];
for( var i = 1, row = []; i < max+1; i++) { nums.push(i); }
return nums;
}
//출력용
function view(list) {
var nums = [];
for( var i = 0, row = []; i < list.length; i++) {
if( i % width == 0 ) {
row = [];
}
row.push(list[i]);
if( i % width == 0 ) {
nums.push(row);
}
}
console.table(nums);
}
//핵심로직
function check(list) {
//앞에서부터
for(var i = 1, num = 0; i< width+1; i++ ) {
for( j = 0, jlen = ( i > height ? height : i ); j<jlen; j++ ) {
num = ( j == 0 ) ? i -1 : num + (width-1);
//num 이 배열번호 이번호를 기준으로 이벤트 걸면될듯
list[num] = i;
}
}
//뒤에서부터
var listLen = list.length;
for(var i = 1, num = 0; i< height; i++ ) {
for(var j = 0, jlen = i; j<jlen; j++ ) {
num = ( j == 0 ) ? listLen - i : num - (width-1);
//num 이 배열번호 이번호를 기준으로 이벤트 걸면될듯
list[num] = width + height -i;
}
}
return list;
}
function makeTable() {
//기본 배열 생성
var nums = initArray();
//테이블로 보기
view(nums);
//가공한녀석들 테이블로 보기
nums = check(nums);
view(nums);
}
makeTable();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment