내 친구들이 징검다리를 건널꺼야! 하지만 징검다리는 버틸 수 있는 내구도에 한계가 있지!
내 친구들의 몸무게, 돌의 내구도, 친구들의 점프력을 고려하여 내 친구 루비독, 피치피독, 씨-독, 코볼독이
각각 다리를 건널 수 있는지 알아봐줘! 친구들은 더 추가될 수도, 덜 건널 수도 있어!
class Dog {
constructor(name, age, jump, weight) {
this.name = name;
this.age = age;
this.jump = jump;
this.weight = weight;
}
}
const dogList = [
new dog("루비독", "95년생", "3", "4"),
new dog("피치독", "95년생", "3", "3"),
new dog("씨-독", "72년생", "2", "1"),
new dog("코볼독", "59년생", "1", "1"),
]
const stoneDurability = [1, 2, ,1, 4]
ouput : ["씨-독"]
const stoneDurability = [5, 3, 4, 1, 3, 8, 3]
output : ["루비독", "씨-독"]
class Dog {
constructor(name, age, jump, weight) {
this.name = name;
this.age = age;
this.jump = jump;
this.weight = weight;
}
}
function question() {
// 돌의 내구도를 배열로 만들어준다.
const stoneDurability = [1, 2, 1, 4];
// 독들의 정보를 설정해준다.
const dogList = [
new Dog('루비독', '95년생', '3', '4'),
new Dog('피치독', '95년생', '3', '3'),
new Dog('씨-독', '72년생', '2', '1'),
new Dog('코볼독', '59년생', '1', '1')
];
return func(stoneDurability, dogList);
}
function func(stoneDurability, dogList) {
// 다리를 건널 수 있는 독
let list = [];
dogList.forEach((dog) => {
let position = -1; // 현재 독의 위치 (초기 -1)
let check = false; // 독이 다리를 건넜는지 확인
// 독의 위치가 다리를 건널 때 까지 수행
while (position < stoneDurability.length) {
// 독의 점프력을 독위치에 더 해준다.
position += parseInt(dog.jump);
// 현재 독의 위치에 있는 돌의 내구도를 변수로 저장
let stone = stoneDurability[position];
// 독의 위치가 다리를 건넜는지 확인
if (stoneDurability.length < position) {
check = true;
break;
} else {
// 독의 위치의 돌의 내구도를 독의 무게 만큼 감소
stoneDurability[position] = stone - dog.weight;
}
// 돌의 내구도가 독의 무게만큼 감소된 값이 0보다 작을 때 멈춤(커야지 다리를 건널 수 있다.)
if (0 > stone - dog.weight) {
break;
}
}
// 독이 다리를 건넜으면 list에 추가
if (check) {
list.push(dog.name);
}
});
return list;
}