여러개 포함 여부 확인 "some"
const abc = ["a", "b", "c"];
const str = "addd";
console.log(abc.some((v) => str.includes(v))); // true
정수를 나선형으로 배치 (프로그래머스)
function solution(n) {
let answer = Array.from({length : n}, (_) => Array.from({length: n},(_)=>0));
const clock = (num, idx) => {
const arr = Array.from({length : 4}, (_, i) => {
if (i === 0) {
return Array.from({length: num}, (_, j) => [0+idx, j+idx]);
} else if (i === 1) {
return Array.from({length: num}, (_, j) => [j+idx, num-1+idx]);
} else if (i === 2) {
return Array.from({length: num}, (_, j) => [num-1+idx, num-1-j+idx]);
} else {
return Array.from({length: num}, (_, j) => [num-1-j+idx, 0+idx]);
}
})
const noRepeatArr = arr.reduce((acc, cur) => {
cur.forEach((v) => {
if(!acc.some((_v) => _v[0] === v[0] && _v[1] === v[1])) acc.push(v);
})
return acc;
}, [])
return noRepeatArr;
}
const cycle = n % 2 === 0 ? n / 2 : ~~(n / 2) + 1;
let num = 1;
for (let i = 0; i < cycle; i++) {
const arr = clock(n-(2*i), i);
arr.forEach(([x, y]) => answer[x][y] = num++)
}
return answer;
}