-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New loop coruses, fixing some coures
- Loading branch information
Showing
12 changed files
with
141 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
title: التكرار على مصفوفة باستخدام حلقة | ||
snippet: إحدى المهام الشائعة في الحلقات هي التكرار عبر محتويات المصفوفة | ||
order: 4 | ||
--- | ||
|
||
إحدى المهام الشائعة في الحلقات هي التكرار عبر محتويات المصفوفة. إحدى الطرق | ||
للقيام بذلك هي باستخدام حلقة `for`. سيُخرج هذا الكود كل عنصر من عناصر المصفوفة | ||
`arr` إلى وحدة التحكم: | ||
|
||
```js | ||
const arr = [10, 9, 8, 7, 6]; | ||
|
||
for (let i = 0; i < arr.length; i++) { | ||
console.log(arr[i]); | ||
} | ||
``` | ||
|
||
تذكر أن المصفوفات لها فهرسة صفرية، مما يعني أن الفهرس الأخير للمصفوفة هو | ||
`length - 1`. شرطنا لهذه الحلقة هو `i < arr.length`، والذي يوقف الحلقة عندما | ||
يكون i مساويًا للطول. في هذه الحالة، التكرار الأخير هو `i === 4`، أي عندما يصبح | ||
`i` مساويًا لـ `arr.length - 1` ويخرج `6` إلى وحدة التحكم. ثم يزيد `i` إلى `5`، | ||
وتنتهي الحلقة لأن شرط `i < arr.length` لم يعد صحيح. | ||
|
||
<div class="quiz"> | ||
نعتذر عن عدم وجود اختبار لهذا الدرس حالياً. نحن نعمل بجد لإعداد اختبارات لجميع الدروس وسنقوم بتوفيرها في أقرب وقت ممكن. | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
--- | ||
title: التكرار باستخدام حلقات ( do while ) | ||
snippet: إذا كان لديك مصفوفة متعددة الأبعاد | ||
order: 6 | ||
--- | ||
|
||
النوع التالي من الحلقات التي ستتعلمها يسمى حلقة `do while`. يطلق عليها اسم حلقة | ||
`do while` لأنها ستقوم أولاً بإجراء تمريرة واحدة للكود داخل الحلقة بغض النظر عن | ||
الأمر، ثم تستمر في تشغيل الحلقة أثناء تقييم الشرط المحدد إلى `true`. | ||
|
||
```js | ||
const ourArray = []; | ||
let i = 0; | ||
|
||
do { | ||
ourArray.push(i); | ||
i++; | ||
} while (i < 5); | ||
``` | ||
|
||
يتصرف المثال السابق بشكل مشابه لأنواع أخرى من الحلقات، وسيبدو المصفوفة الناتجة | ||
بالشكل `[0، 1، 2، 3، 4]`. | ||
|
||
ومع ذلك، ما يجعل `do while` مختلفًا عن الحلقات الأخرى هو كيفية تصرفها عند فشل | ||
الشرط عند التحقق الأول. دعونا نرى هذا في مثال عملي. فيما يلي حلقة `while` عادية | ||
تقوم بتشغيل الكود في الحلقة طالما `i < 5`: | ||
|
||
```js | ||
const ourArray = []; | ||
let i = 5; | ||
|
||
while (i < 5) { | ||
ourArray.push(i); | ||
i++; | ||
} | ||
``` | ||
|
||
في هذا المثال، قمنا بتهيئة قيمة `ourArray` إلى مصفوفة فارغة وقيمة `i` إلى `5`. | ||
عندما ننفذ حلقة `while`، يتم تقييم الشرط إلى `false` لأن `i` لا يقل عن `5`، لذلك | ||
لا ننفذ الكود الموجود بداخله الحلقة. والنتيجة هي أن `ourArray` سينتهي بدون أي | ||
قيم مضافة إليه، وسيظل فارغ كما هو عند اكتمال تشغيل كافة التعليمات البرمجية في | ||
المثال السابق. والآن، ألقِ نظرة على حلقة `do while`: | ||
|
||
```js | ||
const ourArray = []; | ||
let i = 5; | ||
|
||
do { | ||
ourArray.push(i); | ||
i++; | ||
} while (i < 5); | ||
``` | ||
|
||
في هذه الحالة، نقوم بتهيئة قيمة `i` إلى `5`، تمامًا كما فعلنا مع حلقة `while`. | ||
عندما نصل إلى السطر التالي، لا يوجد شرط للتقييم، لذلك ننتقل إلى الكود الموجود | ||
داخل الأقواس المتعرجة وننفذه. سنضيف عنصرًا واحدًا إلى المصفوفة ثم نزيد `i` قبل أن | ||
نصل إلى التحقق من الحالة. عندما نقوم أخيرًا بتقييم الشرط `i < 5` في السطر الأخير، | ||
نرى أن `i` الآن `6`، وهو ما فشل في التحقق الشرطي، لذلك نخرج من الحلقة وننتهي. في | ||
نهاية المثال أعلاه، قيمة `ourArray` هي `[5]`. بشكل أساسي، تضمن حلقة `do while` | ||
أن الكود الموجود داخل الحلقة سيتم تشغيله مرة واحدة على الأقل. | ||
|
||
<div class="quiz"> | ||
نعتذر عن عدم وجود اختبار لهذا الدرس حالياً. نحن نعمل بجد لإعداد اختبارات لجميع الدروس وسنقوم بتوفيرها في أقرب وقت ممكن. | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--- | ||
title: حلقات متداخلة | ||
snippet: إذا كان لديك مصفوفة متعددة الأبعاد | ||
order: 5 | ||
--- | ||
|
||
إذا كان لديك مصفوفة متعددة الأبعاد، فيمكنك استخدام نفس المنطق مثل نقطة الطريق | ||
السابقة للتكرار عبر المصفوفة وأي مصفوفات فرعية. مثال: | ||
|
||
```js | ||
const arr = [ | ||
[1, 2], | ||
[3, 4], | ||
[5, 6], | ||
]; | ||
|
||
for (let i = 0; i < arr.length; i++) { | ||
for (let j = 0; j < arr[i].length; j++) { | ||
console.log(arr[i][j]); | ||
} | ||
} | ||
``` | ||
|
||
يؤدي هذا إلى إخراج كل عنصر فرعي في `arr` واحدًا تلو الآخر. لاحظ أنه بالنسبة | ||
للحلقة الداخلية، فإننا نتحقق من `.length` لـ `arr[i]`، نظرًا لأن `arr[i]` هو في | ||
حد ذاته مصفوفة. | ||
|
||
<div class="quiz"> | ||
نعتذر عن عدم وجود اختبار لهذا الدرس حالياً. نحن نعمل بجد لإعداد اختبارات لجميع الدروس وسنقوم بتوفيرها في أقرب وقت ممكن. | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
/* | ||
write any array containing name of anyone you know :) | ||
*/ | ||
const levelsPoints = [50, 40, 30]; | ||
console.log(levelsPoints) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
const x = "Augusta" | ||
console.log(x) | ||
console.log(x[0]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
const strLen = "Nakhlah JS".length; | ||
console.log(strLen); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,3 @@ | ||
const city = "Jerusalem"; // هذا المتغير لا يمكن تغيير قيمته | ||
// city = "Ramallah"; // TypeError: Assignment to constant variable. | ||
let x = 5; // هذا المتغير يمكن تغيير قيمته | ||
x = 6; | ||
console.log(x); // 6 | ||
var y = 7; // هذا المتغير يمكن تغيير قيمته | ||
y = 8; | ||
console.log(y); // 8 | ||
var city = "Jerusalem"; | ||
var city = "Ramallah"; | ||
console.log(city) // تم تجاوزه |