주변 지뢰 갯수 세기

Count the number of surrounding bombs

concat을 이용하여 배열과 배열을 합하여 새로운 배열을 만들 수 있다.

//클릭했을 때 주변 지뢰 갯수
                var 부모tr = e.currentTarget.parentNode;
                var 부모tbody = e.currentTarget.parentNode.parentNode;
                var 칸 = Array.prototype.indexOf.call(부모tr.children,e.currentTarget);//children은 유사배열이라 indexOf를 못쓰지만 강제로 쓰는 방법.
                var 줄 = Array.prototype.indexOf.call(부모tbody.children,부모tr);
                if(dataset[줄][칸] === 'X'){
                    e.currentTarget.textContent = '펑';
                } else {
                    var 주변 = [
                        dataset[줄][칸-1],                 dataset[줄][칸+1],
                        
                    ];
                    if(dataset[줄-1]) {//concat은 배열과 배열을 합쳐서 새로운 배열을 만든다.
                        주변 = 주변.concat([dataset[줄-1][칸-1],dataset[줄-1][칸],dataset[줄-1][칸+1]]);
                    }
                    if(dataset[줄+1]) {//concat은 배열과 배열을 합쳐서 새로운 배열을 만든다.
                        주변 = 주변.concat([dataset[줄+1][칸-1],dataset[줄+1][칸],dataset[줄+1][칸+1]]);
                    }

                    e.currentTarget.textContent = 주변.filter(function(v) {
                        return v === 'X';
                    }).length;
                    e.currentTarget.textContent = '';//숫자
                }

Last updated

Was this helpful?