아래의 코드는 중간시험을 보면서 가장 자신 있는 코드라고 생각했는데, 다시 한 번 코드를 보니까 내가 굳이 사용하지 않을 변수를 선언하고 반환한다는 점에서 코드를 작성할 때 잘못된 습관을 가지고 고치지 않는 점을 깨달았다.
// 💩 Bad
function getFind(filter, sentence) {
const arr = sentence.split('');
const result = arr.findIndex((value) => value === filter);
return result;
}
const output = getFind('a', 'I am a hacker')
console.log(output) // --> 2
// ⭐ Good?
function getFind(filter, sentence) {
const arr = sentence.split('');
return arr.findIndex((value) => value === filter);
}
const output = getFind('a', 'I am a hacker')
console.log(output); // --> 2