From a3c2b5c239d59f8eb85a67636632847ea49aea8c Mon Sep 17 00:00:00 2001 From: Omar <96267545+Omarsd007@users.noreply.github.com> Date: Sun, 10 Apr 2022 11:44:47 +0200 Subject: [PATCH 1/2] Update traffic-light.js --- .../1-traffic-light/traffic-light.js | 50 +++++++------------ 1 file changed, 18 insertions(+), 32 deletions(-) diff --git a/Week3/prep-exercises/1-traffic-light/traffic-light.js b/Week3/prep-exercises/1-traffic-light/traffic-light.js index f4a5c1a..a729edb 100644 --- a/Week3/prep-exercises/1-traffic-light/traffic-light.js +++ b/Week3/prep-exercises/1-traffic-light/traffic-light.js @@ -1,32 +1,20 @@ -"use strict"; -/** - * The `trafficLight` object is now no longer a global variable. Instead, - * it is defined in function `main()` and passed as a parameter to other - * functions, as and when needed. - */ - 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 + + if (trafficLight.stateIndex==2){ + trafficLight.stateIndex=0; + } + else { + trafficLight.stateIndex++; + } +return trafficLight.stateIndex; } - -// 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 -// JavaScript. You will learn better ways of doing this when you learn about -// asynchronous code. function waitSync(secs) { const start = Date.now(); - while (Date.now() - start < secs * 1000) { + while (Date.now - start < secs * 1000) { // nothing do to here } } @@ -47,14 +35,12 @@ function main() { } main(); -/** - * The output should be: -0 The traffic light is now green -1 The traffic light is now orange -2 The traffic light is now red -3 The traffic light is now green -4 The traffic light is now orange -5 The traffic light is now red +output: -*/ +0The traffic light is nowgreen +1The traffic light is noworange +2The traffic light is nowred +3The traffic light is nowgreen +4The traffic light is noworange +5The traffic light is nowred From 79c66027927c2ab46ef79f4636e91c35e458e3c1 Mon Sep 17 00:00:00 2001 From: Omar <96267545+Omarsd007@users.noreply.github.com> Date: Sun, 10 Apr 2022 12:30:11 +0200 Subject: [PATCH 2/2] Update traffic-light.js --- .../1-traffic-light/traffic-light.js | 52 ++++++++++++------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/Week3/prep-exercises/1-traffic-light/traffic-light.js b/Week3/prep-exercises/1-traffic-light/traffic-light.js index a729edb..fa75572 100644 --- a/Week3/prep-exercises/1-traffic-light/traffic-light.js +++ b/Week3/prep-exercises/1-traffic-light/traffic-light.js @@ -1,20 +1,32 @@ -function getCurrentState(trafficLight) { - return trafficLight.possibleStates[trafficLight.stateIndex]; +"use strict"; +/** + * The `trafficLight` object is now no longer a global variable. Instead, + * it is defined in function `main()` and passed as a parameter to other + * functions, as and when needed. + */ +function getCurrentState(trafficLight) { + // TODO + // Should return the current state (i.e. colour) of the `trafficLight` + // object passed as a parameter. } + function getNextStateIndex(trafficLight) { - - if (trafficLight.stateIndex==2){ - trafficLight.stateIndex=0; - } - else { - trafficLight.stateIndex++; - } -return trafficLight.stateIndex; + // 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 } + +// 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 +// JavaScript. You will learn better ways of doing this when you learn about +// asynchronous code. function waitSync(secs) { const start = Date.now(); - while (Date.now - start < secs * 1000) { + while (Date.now() - start < secs * 1000) { // nothing do to here } } @@ -35,12 +47,12 @@ function main() { } main(); - -output: - -0The traffic light is nowgreen -1The traffic light is noworange -2The traffic light is nowred -3The traffic light is nowgreen -4The traffic light is noworange -5The traffic light is nowred +/** + * The output should be: +0 The traffic light is now green +1 The traffic light is now orange +2 The traffic light is now red +3 The traffic light is now green +4 The traffic light is now orange +5 The traffic light is now red +*/