์
๋ ฅ
๋์๋ด๊ตฌ๋ = [1, 2, 1, 4]
๋
= [{
"์ด๋ฆ" : "๋ฃจ๋น๋
",
"๋์ด" : "95๋
์",
"์ ํ๋ ฅ" : "3",
"๋ชธ๋ฌด๊ฒ" : "4",
},{
"์ด๋ฆ" : "ํผ์น๋
",
"๋์ด" : "95๋
์",
"์ ํ๋ ฅ" : "3",
"๋ชธ๋ฌด๊ฒ" : "3",
},{
"์ด๋ฆ" : "์จ-๋
",
"๋์ด" : "72๋
์",
"์ ํ๋ ฅ" : "2",
"๋ชธ๋ฌด๊ฒ" : "1",
},{
"์ด๋ฆ" : "์ฝ๋ณผ๋
",
"๋์ด" : "59๋
์",
"์ ํ๋ ฅ" : "1",
"๋ชธ๋ฌด๊ฒ" : "1",
},
]
์ถ๋ ฅ
์์กด์ : ['์จ-๋
']
[๋ต์]
let ๋ด๊ตฌ๋ = [1, 2, 1, 4];
let dogs = [
{
์ด๋ฆ: "๋ฃจ๋น๋
",
๋์ด: "95๋
์",
์ ํ๋ ฅ: "3",
๋ชธ๋ฌด๊ฒ: "4",
},
{
์ด๋ฆ: "ํผ์น๋
",
๋์ด: "95๋
์",
์ ํ๋ ฅ: "3",
๋ชธ๋ฌด๊ฒ: "3",
},
{
์ด๋ฆ: "์จ-๋
",
๋์ด: "72๋
์",
์ ํ๋ ฅ: "2",
๋ชธ๋ฌด๊ฒ: "1",
},
{
์ด๋ฆ: "์ฝ๋ณผ๋
",
๋์ด: "59๋
์",
์ ํ๋ ฅ: "1",
๋ชธ๋ฌด๊ฒ: "1",
},
];
function ์ง๊ฒ๋ค๋ฆฌ๋ฅผ๊ฑด๋๋ผ(๋ด๊ตฌ๋, ๋
) {
//๋ต์ ๋ฃ์ ์ ์๋ answer
let answer = [];
// ๋
ํ๋ง๋ฆฌ์ฉ ๊ณ์ฐํ๊ธฐ
for (let ๊ฐ๋ณ๋
of ๋
) {
// ๋
์์น๋ฅผ ์์์ผํ๋ค. 0๋ถํฐ ์์
let ๋
์์์น = 0;
//์คํจ์๋๋ฉด answer์ ๋
์ด๋ฆ ์ถ๊ฐ
let ์คํจ = false;
// ๋ด๊ตฌ๋ ๊ธธ์ด๊ฐ ๋๋ค๋ฆฌ ๊ฐฏ์ ๋๋ค๋ฆฌ ๊ฐฏ์๋งํผ ๋๊ฒ ๋ง๋ค๊ธฐ
// ๋
์ ์์น๊ฐ ๋ด๊ตฌ๋๋ณด๋ค ํฌ๋ฉด ๋ฉ์ถ๊ฒ
while (๋
์์์น < ๋ด๊ตฌ๋.length) {
//๋
์ ์์น ์ ํ๋ ฅ๋งํผ ๋ํ๊ธฐ
๋
์์์น += parseInt(๊ฐ๋ณ๋
["์ ํ๋ ฅ"], 10);
//๋ด๊ตฌ๋์ ๋
์ ๋ชธ๋ฌด๊ฒ๋งํผ ๋บด๊ธฐ- [๋
์์์น - 1]ํ ์ด์ ๋ 2์นธ ๊ฐ์ด๋ ๋ด๊ตฌ๋[1]์ด๊ธฐ ๋๋ฌธ
๋ด๊ตฌ๋[๋
์์์น - 1] -= parseInt(๊ฐ๋ณ๋
["๋ชธ๋ฌด๊ฒ"], 10);
//๋ง์ฝ ์ค๊ฐ ๋๋ค๋ฆฌ ๋ด๊ตฌ๋๊ฐ 0๋ณด๋ค ์์ผ๋ฉด ์คํจ ํจ์ ๋ฉ์ถ๊ธฐ
if (๋ด๊ตฌ๋[๋
์์์น - 1] < 0) {
์คํจ = true;
break;
}
}
//์คํจ๊ฐ ์๋๋ฉด answer์ ๋
์ด๋ฆ ๋ฃ๊ธฐ
if (!์คํจ) {
answer.push(๊ฐ๋ณ๋
["์ด๋ฆ"]);
}
}
//answer ์ถ๋ ฅ
return answer;
}
์ง๊ฒ๋ค๋ฆฌ๋ฅผ๊ฑด๋๋ผ(๋ด๊ตฌ๋, dogs);