[크레인 인형뽑기 게임]
- for문에서 break 활용하기!!!!!!!!!!
- 반복문이 계속해서 실행되지 않도록 하는 스위치 변수 활용하기!!!!!!!!!!
function solution(board, moves) {
let result = [];
let count = 0;
for (let i = 0; i < moves.length; i++) {
for (let j = 0; j < board.length; j++) {
if (board[j][moves[i] - 1] > 0) {
result.push(board[j][moves[i] - 1]);
board[j][moves[i] - 1] = 0;
if (result[result.length - 1] === result[result.length - 2]) {
console.log("dd");
result.splice(result.length - 2, 2);
count += 2;
}
break;
}
}
}
return count;
}
function solution(board, moves) {
let answer = 0;
const bucket = [];
for (let i = 0; i < moves.length; i++) {
for (let l = 0; l < board.length; l++) {
const doll = board[l][moves[i] - 1];
if (doll !== 0) {
board[l][moves[i] - 1] = 0;
if (doll === bucket[bucket.length - 1]) {
answer += 2;
bucket.pop();
break;
}
bucket.push(doll);
break;
}
}
}
return answer;
}
function solution(board, moves) {
let answer = 0;
const bucket = [];
moves.forEach((move) => {
let stop = false;
board.forEach((location) => {
const doll = location[move - 1];
if (stop === false) {
if (doll !== 0) {
location[move - 1] = 0;
if (doll === bucket[bucket.length - 1]) {
answer += 2;
bucket.pop();
} else {
bucket.push(doll);
}
stop = true;
}
}
});
});
return answer;
}