我们需要编写一个以数字作为唯一参数的JavaScript函数。然后,该函数应返回一个随机生成的数字,该数字始终可被参数提供的数字整除。
为此的代码将是-
const num = 21;
//函数生成随机数被n整除的默认值
upper limit of 1000000
const specialRandom = (num = 1, limit = 1000000) => {
//得到一个随机数
const random = Math.random() * limit;
//四舍五入为num-
const res = Math.round( random / num ) * num;
return res;
};
console.log(specialRandom(num));输出结果
控制台中的输出将是-
6006
每次运行此输出可能会有所不同。