3.4 KiB
3.4 KiB
id | title | challengeType | videoUrl | localeTitle |
---|---|---|---|---|
cf1111c1c12feddfaeb1bdef | Generate Random Whole Numbers with JavaScript | 1 | توليد أرقام كاملة عشوائية مع جافا سكريبت |
Description
- استخدم
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()
. Instructions
0
و 9
. Tests
tests:
- text: يجب أن تكون نتيجة <code>randomWholeNum</code> .
testString: 'assert(typeof randomWholeNum() === "number" && (function(){var r = randomWholeNum();return Math.floor(r) === r;})(), "The result of <code>randomWholeNum</code> should be a whole number.");'
- text: يجب أن تستخدم <code>Math.random</code> لإنشاء رقم عشوائي.
testString: 'assert(code.match(/Math.random/g).length > 1, "You should be using <code>Math.random</code> to generate a random number.");'
- text: يجب أن تضاعف نتيجة <code>Math.random</code> بمقدار 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 <code>Math.random</code> by 10 to make it a number that is between zero and nine.");'
- text: يجب عليك استخدام <code>Math.floor</code> لإزالة الجزء العشري من الرقم.
testString: 'assert(code.match(/Math.floor/g).length > 1, "You should use <code>Math.floor</code> to remove the decimal part of the number.");'
Challenge Seed
var randomNumberBetween0and19 = Math.floor(Math.random() * 20);
function randomWholeNum() {
// Only change code below this line.
return Math.random();
}
After Test
console.info('after the test');
Solution
// solution required