function solution(food) {
// food[1]부터 /2 해서 Math.floor, 그리고 해당 수만큼 i를 추가,
// 마지막 음식까지 넣으면 0, 그리고 reverse한 배열 추가
const result = []
for(let i = 1; i<food.length; i++){
const bite = Math.floor(food[i]/2)
if(bite > 0){
result.push(`${i}`.repeat(bite))
}
}
const reverse = [...result].reverse()
return [...result, 0, ...reverse].join('')
}
function solution(food) {
let res = '';
for (let i = 1; i < food.length; i++) {
res += String(i).repeat(Math.floor(food[i]/2));
}
return res + '0' + [...res].reverse().join('');
}
전체적인 풀이 방식은 비슷하지만 더 깔끔하게 작성하셨다
처음부터 배열일 필요는 없다는 점, 그리고 어차피 repeat은 0이면 빈문자열이 되므로 조건문이 필요가 없는 점. repeat안에 Math.floor을 작성.