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

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

[์•Œ๊ณ ๋ฆฌ์ฆ˜ || ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ง์‚ฌ๊ฐํ˜• ๋ณ„์ฐ๊ธฐ - String.repeat()

728x90

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/repeat

 

String.prototype.repeat() - JavaScript | MDN

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

developer.mozilla.org

 

process.stdin.setEncoding('utf8');
process.stdin.on('data', data => {
    const n = data.split(" ");
    const a = Number(n[0]), b = Number(n[1]);
    //๊ฐœ ๋งŒํผ์˜ *์„ ๋งŒ๋“ ๋‹ค.
    //๋งŒ๋“  a ๋ฅผ b ๋งŒํผ ์ถœ๋ ฅํ•œ๋‹ค.
    let letterA = '';
    for(i=0; i<a; i++){
        letterA += '*' 
    }
    for(i=0; i<b; i++){
console.log(letterA)
    }
});

for๋ฌธ์œผ๋กœ a์˜ ์ˆ˜ ๋งŒํผ *๋ฅผ ๋”ํ–ˆ๋˜ ๊ณผ์ •์„ repeat ์ด๋ž€ ํ•จ์ˆ˜๋กœ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. 

 

 const row = '*'.repeat(a)

 

repeat(count)

=> count ์ž๋ฆฌ์— ๋ช‡๋ฒˆ ๋ฐ˜๋ณตํ• ์ง€ ์ˆ˜๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค.

0๊ณผ ๋ฌดํ•œ๋Œ€ ์‚ฌ์ด์˜ ์ •์ˆ˜๋ฅผ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค. 

 

    let letterA = '*'.repeat(a)
    for(i=0; i<b; i++){
	console.log(letterA)
    }

์ˆ˜์ •ํ•ด์ฃผ๋‹ˆ ํ›จ์”ฌ ๊น”๋”ํ•˜๊ณ  ๊ฐ€๋…์„ฑ๋„ ์ข‹๋‹ค. 

728x90
๋ฐ˜์‘ํ˜•