Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 36703f6

Browse files
committed
prep-exercises
1 parent 2e74f47 commit 36703f6

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

Week1/prep-exercises/1-traffic-light/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,10 @@ Let's have a deeper look at the working of traffic lights this week so that we c
55
## Things to think about
66

77
- Which way of representing the traffic light did you find better? Why?
8+
i found the second one better because it is more effective in case I want to expand the exercise. I can just enter new values into the array without editing each statement. This is helpful if I have a lot of values to check, as I won't need to write a separate condition for each one
9+
-
810
- What happens if you change the loop to a `do-while` loop instead of a `while` loop? Why?
11+
If i use a do-while loop the code inside the loop will execute at least once before checking the condition. So even if cycle is already 2, the loop will still run one more time before the condition is checked.
912
- We could have also used a `for` loop to make the traffic light do 2 full rotations. Do you think that would be better? Why or why not?
13+
No because using for will execute only two times, but with while, as long as the condition is true, the code will execute until the condition is false. That is what the program is looking for.
14+

Week1/prep-exercises/1-traffic-light/traffic-light-1.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ let rotations = 0;
1111
while (rotations < 2) {
1212
const currentState = trafficLight.state;
1313
console.log("The traffic light is on", currentState);
14+
if (trafficLight.state === "green") {
15+
trafficLight.state = "orange";
16+
}
17+
else if (trafficLight.state === "orange") {
18+
trafficLight.state = "red";
19+
}
20+
else {
21+
rotations++;
22+
trafficLight.state = "green";
23+
}
24+
1425

1526
// TODO
1627
// if the color is green, turn it orange

Week1/prep-exercises/1-traffic-light/traffic-light-2.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ let cycle = 0;
1313
while (cycle < 2) {
1414
const currentState = trafficLight.possibleStates[trafficLight.stateIndex];
1515
console.log("The traffic light is on", currentState);
16+
if (trafficLight.possibleStates[trafficLight.stateIndex] === "green") {
17+
trafficLight.stateIndex = trafficLight.possibleStates.indexOf("orange");
18+
}
19+
else if (trafficLight.possibleStates[trafficLight.stateIndex] === "orange") {
20+
trafficLight.stateIndex = trafficLight.possibleStates.indexOf("red");
21+
}
22+
else {
23+
cycle++
24+
trafficLight.stateIndex = trafficLight.possibleStates.indexOf("green");
25+
}
1626

1727
// TODO
1828
// if the color is green, turn it orange

0 commit comments

Comments
 (0)