Math.random()
لإنشاء عشري عشوائي. 20
. Math.floor()
الرقم إلى أقرب رقم Math.floor()
له. Math.random()
لا يمكنها أبدًا إرجاع 1
و ، نظرًا لأننا نقوم بالتقريب ، فمن المستحيل الحصول على 20
بالفعل. ستعطينا هذه التقنية عددًا صحيحًا بين 0
و 19
. وضع كل شيء معا ، وهذا هو ما تبدو عليه الكود لدينا: Math.floor(Math.random() * 20);
نحن نطلق على Math.random()
، بضرب النتيجة بـ 20 ، ثم نمرر قيمة Math.floor()
القيمة إلى أقرب رقم Math.floor()
. 0
و 9
. randomWholeNum
.
testString: 'assert(typeof randomWholeNum() === "number" && (function(){var r = randomWholeNum();return Math.floor(r) === r;})(), "The result of randomWholeNum
should be a whole number.");'
- text: يجب أن تستخدم Math.random
لإنشاء رقم عشوائي.
testString: 'assert(code.match(/Math.random/g).length > 1, "You should be using Math.random
to generate a random number.");'
- text: يجب أن تضاعف نتيجة Math.random
بمقدار 10 لجعله رقمًا بين صفر وتسعة.
testString: 'assert(code.match(/\s*?Math.random\s*?\(\s*?\)\s*?\*\s*?10[\D]\s*?/g) || code.match(/\s*?10\s*?\*\s*?Math.random\s*?\(\s*?\)\s*?/g), "You should have multiplied the result of Math.random
by 10 to make it a number that is between zero and nine.");'
- text: يجب عليك استخدام Math.floor
لإزالة الجزء العشري من الرقم.
testString: 'assert(code.match(/Math.floor/g).length > 1, "You should use Math.floor
to remove the decimal part of the number.");'
```