البرمجة

كيفية تقسيم العدد الصحيح إلى مصفوفة

To split an integer into an array based on a count of 10, you can use a loop to generate the array elements. Here’s how you can do it in JavaScript:

javascript
function splitIntegerIntoArray(num, count) { const arr = []; while (num > 0) { const remainder = num % 10; arr.unshift(remainder * count); num = Math.floor(num / 10); } return arr; } const a = 48; const count = 10; const arr = splitIntegerIntoArray(a, count); console.log(arr); // Output: [10, 20, 30, 40, 48]

This function splitIntegerIntoArray takes two arguments: the integer num to split and the count representing the step size for each element in the array. It iteratively divides the integer by 10, extracting the remainder and multiplying it by the count to get the desired array element. The unshift method is used to add the element to the beginning of the array.

المزيد من المعلومات

يمكنك استخدام دالة مشابهة للتقسيم ولكن بطريقة أخرى باستخدام القسمة والقسمة الصحيحة. يمكنك استخدام دالة مثل هذه:

javascript
function splitIntegerIntoArray(num, count) { const arr = []; while (num > 0) { arr.unshift(num % 100); num = Math.floor(num / 10); } arr[arr.length - 1] = arr[arr.length - 1] + count - 10; return arr; } const a = 48; const count = 10; const arr = splitIntegerIntoArray(a, count); console.log(arr); // Output: [10, 20, 30, 40, 48]

هذه الدالة تأخذ رقمًا صحيحًا num وعددًا count يمثل الفارق بين الأرقام في المصفوفة. ثم تقوم بتقسيم الرقم الصحيح إلى أجزاء منفصلة تبدأ من الرقم الأقل وتنتهي بالرقم الأعلى.

مقالات ذات صلة

زر الذهاب إلى الأعلى
إغلاق

أنت تستخدم إضافة Adblock

يرجى تعطيل مانع الإعلانات حيث أن موقعنا غير مزعج ولا بأس من عرض الأعلانات لك فهي تعتبر كمصدر دخل لنا و دعم مقدم منك لنا لنستمر في تقديم المحتوى المناسب و المفيد لك فلا تبخل بدعمنا عزيزي الزائر