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

Skip to content

Commit 2e0ed79

Browse files
authored
Merge pull request #4 from SalmaIssa96/week2-js-prep-exercice
Week2 js prep exercice
2 parents 4f85cea + 8f04123 commit 2e0ed79

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"use strict";
1+
'use strict';
22
/**
33
* The `trafficLight` object is now no longer a global variable. Instead,
44
* it is defined in function `main()` and passed as a parameter to other
@@ -9,6 +9,8 @@ function getCurrentState(trafficLight) {
99
// TODO
1010
// Should return the current state (i.e. colour) of the `trafficLight`
1111
// object passed as a parameter.
12+
const { stateIndex, possibleStates } = trafficLight;
13+
return possibleStates[stateIndex];
1214
}
1315

1416
function getNextStateIndex(trafficLight) {
@@ -17,6 +19,9 @@ function getNextStateIndex(trafficLight) {
1719
// - if the color is green, it will turn to orange
1820
// - if the color is orange, it will turn to red
1921
// - if the color is red, it will turn to green
22+
let { stateIndex, possibleStates } = trafficLight;
23+
const possibleStatesLength = possibleStates.length;
24+
return stateIndex >= possibleStatesLength - 1 ? 0 : ++stateIndex;
2025
}
2126

2227
// This function loops for the number of seconds specified by the `secs`
@@ -33,13 +38,13 @@ function waitSync(secs) {
3338

3439
function main() {
3540
const trafficLight = {
36-
possibleStates: ["green", "orange", "red"],
41+
possibleStates: ['green', 'orange', 'red'],
3742
stateIndex: 0,
3843
};
3944

4045
for (let cycle = 0; cycle < 6; cycle++) {
4146
const currentState = getCurrentState(trafficLight);
42-
console.log(cycle, "The traffic light is now", currentState);
47+
console.log(cycle, 'The traffic light is now', currentState);
4348

4449
waitSync(1); // Wait a second before going to the next state
4550
trafficLight.stateIndex = getNextStateIndex(trafficLight);

Week2/prep-exercises/2-experiments/index.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1-
"use strict";
1+
'use strict';
22

3-
function runExperiment(sampleSize) {
3+
function runExperiment(sampleSize = 100) {
44
const valueCounts = [0, 0, 0, 0, 0, 0];
55

66
// TODO
77
// Write a for loop that iterates `sampleSize` times (sampleSize is a number).
88
// In each loop iteration:
99
//
1010
// 1. Generate a random integer between 1 and 6 (as if throwing a six-sided die).
11+
1112
// 2. Add `1` to the element of the `valueCount` that corresponds to the random
1213
// value from the previous step. Use the first element of `valueCounts`
1314
// for keeping a count how many times the value 1 is thrown, the second
1415
// element for value 2, etc.
15-
16+
for (let i = 1; i <= sampleSize; i++) {
17+
const randomNumber = Math.floor(Math.random() * 6);
18+
++valueCounts[randomNumber];
19+
}
1620
const results = [];
1721

1822
// TODO
@@ -25,12 +29,19 @@ function runExperiment(sampleSize) {
2529
// two decimals, e.g. '14.60'.
2630
// 3. Then push that string onto the `results` array.
2731

32+
for (const count of valueCounts) {
33+
results.push(((count / sampleSize) * 100).toFixed(2));
34+
}
2835
return results;
2936
}
3037

3138
function main() {
3239
const sampleSizes = [100, 1000, 1000000];
3340

41+
for (const size of sampleSizes) {
42+
const result = runExperiment(size);
43+
console.log(result, size);
44+
}
3445
// TODO
3546
// Write a for..of loop that calls the `runExperiment()` function for each
3647
// value of the `sampleSizes` array.

0 commit comments

Comments
 (0)