برنامه نویسی
روز 5 در JS: درک Math.Random & Math.floor ، آرایه ، خاصیت طول ..

math.random ():
Math.random() is a built-in function that returns a floating-point number between 0 (inclusive) and 1 (exclusive). This means the result is always >= 0 and < 1.
استفاده اساسی
const randomNumber = Math.random();
console.log(randomNumber); // e.g., 0.34784310291847
یک عدد تصادفی بین 0 و یک عدد خاص دریافت کنید:
const randomUpTo10 = Math.random() * 10; // 0 <= result < 10
ریاضی. کف
Math.floor () روشی است که یک عدد را به نزدیکترین عدد صحیح می رساند.
مثالها:
Math.floor(4.9); // 4
Math.floor(4.1); // 4
Math.floor(4.0); // 4
Math.floor(-4.1); // -5
Math.floor(-4.9); // -5
آرایه در JS:
Array یک ساختار داده است که برای ذخیره چندین مقادیر در یک متغیر واحد استفاده می شود.
ایجاد یک آرایه
const fruits = ["apple", "banana", "cherry"];
const numbers = [1, 2, 3, 4, 5];
طول:
طول خاصیت تعداد عناصر را در یک آرایه تنظیم یا برمی گرداند.
نحو:
array.length
طول یک آرایه را تنظیم کنید:
array.length = number