From ab7db6b475ae152a1d75f79c41a0092ba6e68f8a Mon Sep 17 00:00:00 2001 From: Amani Alomaisi <139679770+amaniomaisy90@users.noreply.github.com> Date: Sat, 15 Feb 2025 09:15:06 +0100 Subject: [PATCH 1/4] Updated traffic light exercise --- .DS_Store | Bin 6148 -> 6148 bytes Week1/.DS_Store | Bin 0 -> 6148 bytes Week1/prep-exercises/.DS_Store | Bin 0 -> 6148 bytes .../1-traffic-light/traffic-light-1.js | 13 +++++++++---- 4 files changed, 9 insertions(+), 4 deletions(-) create mode 100644 Week1/.DS_Store create mode 100644 Week1/prep-exercises/.DS_Store diff --git a/.DS_Store b/.DS_Store index 5008ddfcf53c02e82d7eee2e57c38e5672ef89f6..62ff0fe6355e439e25163b19930946ba5d7dc087 100644 GIT binary patch literal 6148 zcmeHKL2uJA6n^fyEn!0H0i+#3io|srU0Y3>xP;OjIB;1II{+#PiMnW6JT)mjRF!gu zzkc!_fGN12-X6FNt9PBHCM z3fwo`lt$=XB1g_y;2+Th5ivXm@W%`L0_Xj)l-H$GI>FNh^ai+wA)YQVHt7tx6S|+#l4qLmkS?8#|=XHC%`JOjB z?x5Z49?j>j^Z42G7pKFE>@t^MF?*Q6j;idT!E4~vEv)@ZFwQfXkKkp-%}8fSmERa$ z{3dTAyY?l+fK}kG6yW{ALt*p{RvOjT zfl6HgfK9lSq0PrTb3B8-!Ac`WU_w)Ynkvi{Lufkuo{94fRvI;(gt>eO^JHOeC_+6Q z`8^d*!q;ePtAJIYuE2)ww)p%%x&8iMC)u7=z$)-xDIl7IV9>{s%-OoKI6iAVlusyZ p99L;n6jbIoRt-LicTtpK%;N^oH&|(e2WI~WC>d;J6}VFc{sPa_ru_f_ delta 66 zcmZoMXfc=|#>AjHu~2NHo+1YW5HK<@2y7PQ5M$Y_z%h$?Gdl-A2T%b}u84%9H(hI5x+KtY8KJ$3PAT diff --git a/Week1/.DS_Store b/Week1/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..b3f87459e4e3ef828d9081573371043dd3f38388 GIT binary patch literal 6148 zcmeHK&ubGw6n@jDX4j$wsnCnC;I-D+hJy66iRqz84|YWlDsi{F=;C%K>~5Mu2<*B2 zW4!v8_`i74@6AjhX{;iGNSFujeQ$oex0!FUZ+3`C^hWU$q81T3P{zh>n4bubvo6Sr z>1hIm%5g*ov`wcJA7%@)ZHHyRGVs4K!0)b0N7SX~cz0*sUwIN`GQudAhAwpbyApUr z_h?83g>*s{rLYM-f}TSQv{EYJm513}j?dR}+*LUe*x$yA2Y(V9jmx3=?O&o{mX=k! z{Z*{2Hr8%6ou;$yybC_caZrWTsO*Ks8*aUnGK;SKFnX0H<3VfVnary&&67d}q;Y~N zZ(pZ*EXO@L%HvYS271789e2>$oJ@B1+FRakr!(F1CQo!z?X~!J zEdos|OUZ!w_c`lJxjMTj%YbFzZy4bE;6WLEgSAFgI$+cl0N6mc68QYVz%iad-(al~ zBM_lOfjU%}D~8bF==aP#-(am#hm$au4`H4x%ne1Trz5|orjzhBT5B1w49qjIs+%o7 z{||1y|IY{6mSw;)uv83)MnCBHuq1P~E-a4ES`Ydj%EI$%jVc7j9LLJxqj(dl1Y;gM WK;K}k5j_z5A)sim#xn3v8TbJP)s`;+ literal 0 HcmV?d00001 diff --git a/Week1/prep-exercises/.DS_Store b/Week1/prep-exercises/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..550fbe48a3c101e8fff082a94b8264015ea416ef GIT binary patch literal 6148 zcmeHKO;6)65FM97Q=t_-fW$EpS3U|x7h?X9lRu7!m!# zmC!YS594exqd0u@rG_GKT7p=oaz{n{F&@S*B_0*`Y<~l^zK~t z|M)IVdU?J6ph`34*|~~W@#ekD@Kz7PJj(mMX4F0ByA!R`cx*@UY0??AYKsRt%cCUg zbWK9i?jYpiEXmq>(A52`-7~qN?eMBzwN+al4!3sdtNwPQF3ubBrae8Wb5I2^-27$0zSA9k};XE%r(k21~{6b6I=VPN_ga66SZKYgzz zZzK!|17FMl?+-qdF>qK|G+PH6Qvv|Xa2tWmKZ%^3aqD?2|iVx+UtXzen%+oQxZ^B6h7Nr#ign`!#aP0@F@%jJr{rcYoNh1sh1K*PY zRXz+4o0ywBTPJ48XRQytg0gU2VevTyhB=B6%SZ7t)Cl}O8^FL}VG$mP{0JBtq!9)_ Gm4PRZa$j2j literal 0 HcmV?d00001 diff --git a/Week1/prep-exercises/1-traffic-light/traffic-light-1.js b/Week1/prep-exercises/1-traffic-light/traffic-light-1.js index f1d9169..afb1ad2 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-1.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-1.js @@ -12,10 +12,15 @@ while (rotations < 2) { const currentState = trafficLight.state; console.log("The traffic light is on", currentState); - // TODO - // if the color is green, turn it orange - // if the color is orange, turn it red - // if the color is red, add 1 to rotations and turn it green + if (currentState === "green") { + trafficLight.state = "orange" ; + + }else if (currentState=== "orange") { + trafficLight.state= "red"; + } else if (currentState === "red") { + rotations++; + trafficLight.state= "green" ; + } } /** From 624189584aea3a29ea1557c8c25a0a86b5f2c56b Mon Sep 17 00:00:00 2001 From: Amani Alomaisi <139679770+amaniomaisy90@users.noreply.github.com> Date: Sat, 15 Feb 2025 09:56:50 +0100 Subject: [PATCH 2/4] Update traffic-light-2.js --- .../1-traffic-light/traffic-light-2.js | 38 ++++++++++--------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/Week1/prep-exercises/1-traffic-light/traffic-light-2.js b/Week1/prep-exercises/1-traffic-light/traffic-light-2.js index 8c6ba95..115e45e 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-2.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-2.js @@ -6,28 +6,32 @@ */ const trafficLight = { possibleStates: ["green", "orange", "red"], - stateIndex: 0, + stateIndex: 0, }; - +localStorage let cycle = 0; -while (cycle < 2) { + +while (cycle < 2) { const currentState = trafficLight.possibleStates[trafficLight.stateIndex]; console.log("The traffic light is on", currentState); - // TODO - // if the color is green, turn it orange - // if the color is orange, turn it red - // if the color is red, add 1 to cycles and turn it green + + if (trafficLight.stateIndex === 0) { + trafficLight.stateIndex = 1; + } else if (trafficLight.stateIndex === 1) { + trafficLight.stateIndex = 2; + } else if (trafficLight.stateIndex === 2) { + cycle++; + trafficLight.stateIndex = 0; + } } /** - * The output should be: - -The traffic light is on green -The traffic light is on orange -The traffic light is on red -The traffic light is on green -The traffic light is on orange -The traffic light is on red - -*/ + * The expected output: + * The traffic light is on green + * The traffic light is on orange + * The traffic light is on red + * The traffic light is on green + * The traffic light is on orange + * The traffic light is on red + */ From 6ae876f7c934a7fb68941e8b85104ff541f24768 Mon Sep 17 00:00:00 2001 From: Amani Alomaisi <139679770+amaniomaisy90@users.noreply.github.com> Date: Fri, 21 Feb 2025 15:39:12 +0100 Subject: [PATCH 3/4] solution for prep-ex --- .../prep-exercises/1-traffic-light/traffic-light.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/Week2/prep-exercises/1-traffic-light/traffic-light.js b/Week2/prep-exercises/1-traffic-light/traffic-light.js index f4a5c1a..04b6fc3 100644 --- a/Week2/prep-exercises/1-traffic-light/traffic-light.js +++ b/Week2/prep-exercises/1-traffic-light/traffic-light.js @@ -6,19 +6,15 @@ */ function getCurrentState(trafficLight) { - // TODO - // Should return the current state (i.e. colour) of the `trafficLight` - // object passed as a parameter. + return trafficLight.possibleStates[trafficLight.stateIndex]; } function getNextStateIndex(trafficLight) { - // TODO - // Return the index of the next state of the `trafficLight` such that: - // - if the color is green, it will turn to orange - // - if the color is orange, it will turn to red - // - if the color is red, it will turn to green + + return (trafficLight.stateIndex + 1) % trafficLight.possibleStates.length; } + // This function loops for the number of seconds specified by the `secs` // parameter and then returns. // IMPORTANT: This is not the recommended way to implement 'waiting' in From 3623efe357a4f7fd9b1256c52f10e74039ecf727 Mon Sep 17 00:00:00 2001 From: Amani Alomaisi <139679770+amaniomaisy90@users.noreply.github.com> Date: Fri, 21 Feb 2025 16:05:21 +0100 Subject: [PATCH 4/4] solution_index1 --- Week2/prep-exercises/2-experiments/index.js | 29 +++++++-------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/Week2/prep-exercises/2-experiments/index.js b/Week2/prep-exercises/2-experiments/index.js index 7e5aa92..8d5fd7c 100644 --- a/Week2/prep-exercises/2-experiments/index.js +++ b/Week2/prep-exercises/2-experiments/index.js @@ -3,27 +3,18 @@ function runExperiment(sampleSize) { const valueCounts = [0, 0, 0, 0, 0, 0]; - // TODO - // Write a for loop that iterates `sampleSize` times (sampleSize is a number). - // In each loop iteration: - // - // 1. Generate a random integer between 1 and 6 (as if throwing a six-sided die). - // 2. Add `1` to the element of the `valueCount` that corresponds to the random - // value from the previous step. Use the first element of `valueCounts` - // for keeping a count how many times the value 1 is thrown, the second - // element for value 2, etc. - + for (let i = 0; i < sampleSize; i++) { + const roll = Math.floor(Math.random() * 6); + + valueCounts[roll]++; + } const results = []; - // TODO - // Write a for..of loop for the `valueCounts` array created in the previous - // loop. In each loop iteration: - // 1. For each possible value of the die (1-6), compute the percentage of how - // many times that value was thrown. Remember that the first value of - // `valueCounts` represent the die value of 1, etc. - // 2. Convert the computed percentage to a number string with a precision of - // two decimals, e.g. '14.60'. - // 3. Then push that string onto the `results` array. + for (const count of valueCounts) { + const percentage = ((count / sampleSize) * 100).toFixed(2); + results.push(percentage); + } + return results; }