Viết hàm nhận một số, trả về số ngẫu nhiên luôn chia hết cho số đó.
Ví dụ
const num = 21;
const specialRandom = (num = 1, limit = 1000000) => {
const random = Math.random() * limit;
const res = Math.round(random / num) * num;
return res;
};
console.log(specialRandom(num));Kết quả
Kết quả in ra console (ví dụ):
6006
Kết quả khác nhau mỗi lần chạy.