숫자를 유일한 인수로 사용하는 JavaScript 함수를 작성해야 합니다. 그런 다음 함수는 인수에서 제공한 숫자로 항상 나눌 수 있는 임의의 생성된 숫자를 반환해야 합니다.
예시
이에 대한 코드는 -
const num = 21;
// function that generates random numbers divisible by n with a default
upper limit of 1000000
const specialRandom = (num = 1, limit = 1000000) => {
// getting a random number
const random = Math.random() * limit;
// rounding it off to be divisible by num
const res = Math.round( random / num ) * num;
return res;
};
console.log(specialRandom(num)); 출력
콘솔의 출력은 -
6006
이 출력은 실행할 때마다 다를 수 있습니다.