Putting it together · دمج العناصر معًا
Putting it together
- Real programs combine variables, loops, functions, arrays and objects.
- A
for...ofloop reads each item of an array directly — no index needed.
دمج العناصر معاً
- البرامج الحقيقية تجمع المتغيرات والحلقات والدوال والمصفوفات والكائنات.
- حلقة
for...ofتقرأ كل عنصر من مصفوفة مباشرةً — لا حاجة لفهرس.
function total(nums) {
let t = 0;
for (const n of nums) {
t = t + n;
}
return t;
}
console.log(total([4, 8, 6])); // → 18
for...of builds the total · for...of يبني المجموع
for...of reads each item directly. Watch t add them up, one at a time. · for...of يقرأ كل عنصر مباشرة. لاحظ كيف t يجمعها واحدة تلو الأخرى.
Searching and counting
- Loop through the array, and keep a counter (or a flag) as you go.
Searching and counting (البحث والعد)
- مرر عبر المصفوفة، واحتفظ بـ عداد (أو علم) أثناء ذلك.
function countAbove(nums, limit) {
let count = 0;
for (const n of nums) {
if (n > limit) {
count++;
}
}
return count;
}
console.log(countAbove([3, 7, 2, 8], 4)); // → 2
Common mistakes
- Build in small steps and use
console.logto check as you go. - Keep the HTML, CSS and JavaScript doing their own jobs.
أخطاء شائعة
- بناه بخطوات صغيرة واستخدم
console.logللتحقق أثناء العمل. - احتفظ بـ HTML وCSS وJavaScript بأداء أدوارها الخاصة.
Now you try
- Write each function using a loop.
- Press Check answer to test it.
الآن جرب بنفسك
- اكتب كل دالة باستخدام حلقة.
- اضغط على تحقق من الإجابة لاختبارها.
Write a function total(nums) that returns the sum of every number in the array nums. · اكتب دالة total(nums) تُرجع مجموع كل رقم في مصفوفة nums.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Write a function biggest(nums) that returns the largest value in the array. · اكتب دالة biggest(nums) تُرجع القيمة الأكبر في المصفوفة.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Write a function countAbove(nums, limit) that returns how many numbers are greater than limit. · اكتب دالة countAbove(nums, limit) تُرجع عدد الأرقام التي تجاوز limit.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.