728x90
function solution(food) {
let oneCount = [];
food.map((v, i) => i > 0 && oneCount.push(Math.floor(v / 2)))
const arr = oneCount.map((v, i) => new Array(v).fill(i + 1)).flat();
const rev = [...arr].reverse().join("");
console.log(arr.join("") + "0" + rev)
}
์ฒ์์ ํผ ๋ฐฉ๋ฒ.
๊ทผ๋ฐ ์ฌ๊ธฐ์ ๋ญ๊ฐ ๋ ์ค์ผ ์ ์์ ๊ฒ ๊ฐ์๋ค.
function solution(food) {
const arr = food.map((v, i) => {
return new Array(Math.floor(v / 2)).fill(i);
}).flat();
return arr.join("") + "0" + arr.reverse().join("");
}
์ด๋ ๊ฒ ์ค์๋๋ฐ,
๋ฐํท ์๊ฐ ๋ชปํ์
function solution(food) {
const arr = food.flatMap((v, i) => {
return new Array(Math.floor(v / 2)).fill(i);
})
return arr.join("") + "0" + arr.reverse().join("");
}
์ฐ๋ฆฌ ์คํฐ๋ ํ์ ๋ถ์ ํ์ด๋ ์ข์ ๊ฒ ๊ฐ๋ค.
function solution(food) {
let word = "";
for(let i =1 ; i < food.length; i++){
word += `${i}`.repeat(Math.floor(food[i]/2));
}
return `${word}0${word.split('').reverse().join('')}`
}
string ์ repeat ํ๋ฉด ๋๋๊ตฌ๋..!
์ข์ ๋ฉ์๋๊ฐ ์์๊ตฌ๋ง~~
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/repeat
728x90
๋ฐ์ํ