From 7728a4f46c636ca968cbec6a047d6ad65cce0229 Mon Sep 17 00:00:00 2001 From: Mohammed <88824957+majhcc@users.noreply.github.com> Date: Wed, 7 Feb 2024 21:39:02 +0400 Subject: [PATCH] Add introduction to loops and example code and make quiz on intro/array --- courses/arrays/intro.md | 39 +++++++++++++++++++++++++++++++++++++++ precodes/arrays/intro.js | 9 +++++++++ testcases/arrays/intro.js | 30 ++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 precodes/arrays/intro.js create mode 100644 testcases/arrays/intro.js diff --git a/courses/arrays/intro.md b/courses/arrays/intro.md index eaca357..f07fca9 100644 --- a/courses/arrays/intro.md +++ b/courses/arrays/intro.md @@ -57,3 +57,42 @@ Array[0] = 15; ``` لدينا الآن قيمة المصفوفة `[30, 40, 15]`. + +# الحلقات - Loops + +الحلقات هي بنيات أساسية في جافا سكريبت تمكنك من تكرار الأوامر بشكل متكرر. مثلا تكرار طباعة الأرقام من 1 إلى 10. + +## مثال: +```js +for (let i = 1; i <= 10; i++) { + console.log(i); +} +``` +النتيجة ستكون: + +```js +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +``` + +# إستخدام الحلقات مع المصفوفات + +يمكنك استخدام الحلقات للوصول إلى كل عنصر في المصفوفة. مثلا: + +```js +const animals = ['lion', 'tiger', 'cheetah', 'leopard', 'jaguar']; +for (let i = 0; i < animals.length; i++) { + console.log(animals[i]); +} +``` +
+قم بطباعة كل رقم مضاف عليه 10 في المصفوفة numbers باستخدام الحلقة for
+
\ No newline at end of file diff --git a/precodes/arrays/intro.js b/precodes/arrays/intro.js new file mode 100644 index 0000000..1233e87 --- /dev/null +++ b/precodes/arrays/intro.js @@ -0,0 +1,9 @@ +/* +output: +19 +12 +13 +14 +15 +*/ +const numbers = [9, 2, 3, 4, 5]; \ No newline at end of file diff --git a/testcases/arrays/intro.js b/testcases/arrays/intro.js new file mode 100644 index 0000000..6fcc8a6 --- /dev/null +++ b/testcases/arrays/intro.js @@ -0,0 +1,30 @@ +// check if output is = [19, 12, 13, 14, 15]; +function handleCodeRun(code) { + try { + const capturedOutput = []; + const originalConsoleLog = console.log; + console.log = (...args) => { + capturedOutput.push( + args.map((arg) => { + if (typeof arg === "object" && arg !== null) { + return JSON.stringify(arg); + } + return arg.toString(); + }).join(" "), + ); + originalConsoleLog(...args); + }; + if (code) { + eval(code); + } + console.log = originalConsoleLog; + return capturedOutput.join("\n"); + } catch (error) { + return `${error}`; + } +} +const output = handleCodeRun(code); +if (output === "19\n12\n13\n14\n15") { + isPass = true; + msg = "Pass"; +} \ No newline at end of file