mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2025-12-31 06:01:50 -05:00
2.6 KiB
2.6 KiB
id, title, challengeType, videoUrl, forumTopicId, dashedName
| id | title | challengeType | videoUrl | forumTopicId | dashedName |
|---|---|---|---|---|---|
| cf1111c1c12feddfaeb1bdef | إنشاء أرقام صحيحة عشوائية باستخدام JavaScript | 1 | https://scrimba.com/c/cRn6bfr | 18186 | generate-random-whole-numbers-with-javascript |
--description--
من الرائع أنه يمكننا توليد أرقام عشرية عشوائية، ولكن من المفيد أكثر إذا استخدمنا ذلك لتوليد أعداد عشوائية صحيحة.
- استخدم
Math.random()لإنشاء رَقَم عشري عشوائي. - ضاعف هذا الرَّقَم العشري العشوائي في
20. - استخدم الوظيفة أخر،
Math.floor()لتقريب الرَّقْم إلى أقرب رَقَم صحيح.
تذكر ألا يمكن Math.random() أن يعيد 1 و لأنك تقريب لأقل رَقْم، من المستحيل في الواقع الحصول على 20. ستعطينا هذه التقنية رَقَما صحيحا بين 0 و 19.
بتجميع كل شيء معًا، هذا هو الكود الخاص بنا:
Math.floor(Math.random() * 20);
نحن نستدعي Math.random() ثم نضرب النتيجة في 20، ثم نمرر القيمة إلى وظيفة Math.floor() لتقريب القيمة إلى أقرب عدد صحيح.
--instructions--
استخدم هذه التقنية لإنشاء وإعادة عدد صحيح عشوائي بين 0 و 9.
--hints--
يجب أن تكون نتيجة randomWholeNum عدداً صحيحاً.
assert(
typeof randomWholeNum() === 'number' &&
(function () {
var r = randomWholeNum();
return Math.floor(r) === r;
})()
);
يجب عليك استخدام Math.random لتوليد رَقَم عشوائي.
assert(code.match(/Math.random/g).length >= 1);
عليك ضرب نتيجة Math.random في 10 لجعلها رقما بين صفر و تسعة.
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)
);
يجب عليك استخدام Math.floor لإزالة الجزء العشري من الرَّقَم.
assert(code.match(/Math.floor/g).length >= 1);
--seed--
--after-user-code--
(function(){return randomWholeNum();})();
--seed-contents--
function randomWholeNum() {
// Only change code below this line
return Math.random();
}
--solutions--
function randomWholeNum() {
return Math.floor(Math.random() * 10);
}