RangeError: repeat count must be non-negative

๋ฉ”์‹œ์ง€

RangeError: repeat count must be non-negative (Firefox)
RangeError: Invalid count value (Chrome)

์—๋Ÿฌ ํ˜•์‹

RangeError

๋ฌด์—‡์ด ์ž˜๋ชป๋˜์—ˆ์„๊นŒ?

String.prototype.repeat() ๋ฉ”์†Œ๋“œ๊ฐ€ ์‚ฌ์šฉ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฉ”์†Œ๋“œ๋Š” ๋ฌธ์ž์—ด์ด ๋ฐ˜๋ณต๋˜๋Š” ์ˆ˜๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ์นด์šดํŠธ ํŒŒ๋ผ๋ฉ”ํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ํŒŒ๋ผ๋ฉ”ํ„ฐ๋Š” 0๋ณด๋‹ค ํฌ๊ณ , ์–‘์˜ Infinity ๋ณด๋‹ค๋Š” ์ž‘์œผ๋ฉฐ, ์Œ์ˆ˜๋Š” ๋ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์ด ๋ฒ”์œ„๋Š” ์ด๋ ‡๊ฒŒ ํ‘œํ˜„ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. : [0, +โˆž)

์˜ˆ

ํ—ˆ์šฉ๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ

'abc'.repeat(-1); // RangeError 

ํ—ˆ์šฉ๋˜๋Š” ๊ฒฝ์šฐ

'abc'.repeat(0);    // ''
'abc'.repeat(1);    // 'abc'
'abc'.repeat(2);    // 'abcabc'
'abc'.repeat(3.5);  // 'abcabcabc' (์ˆ˜๋Š” ์ •์ˆ˜๋กœ ๋ณ€ํ™˜๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.)

์ฐธ์กฐ