Problem
- Each animal is bringing one dish.
- The dish must start and end with the same letters as the animal’s name.
- Write a function feast that takes the animal’s name and dish as arguments and returns true or false to indicate whether the beast is allowed to bring the dish to the feast.
- 음식의 이름이 동물의 이름과 같은 문자로 시작하고 끝나면 true, 그렇지 않으면 false를 반환한다.
Solution 01
function feast(beast, dish) {
const b1 = beast[0];
const b2 = beast[beast.length - 1];
const d1 = dish[0];
const d2 = dish[dish.length - 1];
return b1 === d1 && b2 === d2 ? true : false;
}
feast('panda', 'potato pizza'); // true
feast('panda', 'hawaiian pizza'); // false
feast('chicken', 'cold chicken'); // true
feast('chicken', 'hot chicken'); // false
Solution 02
function feast(beast, dish) {
const b1 = beast[0];
const b2 = beast[beast.length - 1];
const d1 = dish[0];
const d2 = dish[dish.length - 1];
return b1 === d1 && b2 === d2;
}
feast('panda', 'potato pizza'); // true
feast('panda', 'hawaiian pizza'); // false
feast('chicken', 'cold chicken'); // true
feast('chicken', 'hot chicken'); // false
Solution 03
function feast(beast, dish) {
const b = beast[0] + beast[beast.length - 1];
const d = dish[0] + dish[dish.length - 1];
return b === d;
}
feast('panda', 'potato pizza'); // true
feast('panda', 'hawaiian pizza'); // false
feast('chicken', 'cold chicken'); // true
feast('chicken', 'hot chicken'); // false