[JavaScript] ๋ฐฐ์—ด์—์„œ ์ž„์˜์˜ ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ

2022. 8. 14. 22:11ใ†JavaScript

๐Ÿ’ก Math.floor Math.random

 

let strArr = ["A","B","C", "D"];
let selectString = strArr[Math.floor(Math.random() * strArr.length)];

Math.random() ํ•จ์ˆ˜๋Š” 0~1 ์‚ฌ์ด์˜ ์ž„์˜์˜ ์ˆซ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๊ทธ ์ˆซ์ž์— ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ฅผ ๊ณฑํ•ด์ฃผ๋ฉด ๋ฐฐ์—ด์˜ ์ˆซ์ž ์† ์ž„์˜์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.