๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

ํ”„์—” ๊ณต๋ถ€/๐Ÿซง ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณต๋ถ€

[์•Œ๊ณ ๋ฆฌ์ฆ˜ || ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ํ‘ธ๋“œ ํŒŒ์ดํŠธ ๋Œ€ํšŒ

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

 

String.prototype.repeat() - JavaScript | MDN

repeat() ๋ฉ”์„œ๋“œ๋Š” ๋ฌธ์ž์—ด์„ ์ฃผ์–ด์ง„ ํšŸ์ˆ˜๋งŒํผ ๋ฐ˜๋ณตํ•ด ๋ถ™์ธ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

developer.mozilla.org

 

728x90
๋ฐ˜์‘ํ˜•