Skip to content

Commit df72884

Browse files
committed
ex1,2
1 parent 30024ba commit df72884

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@
66
*/
77

88
function getCurrentState(trafficLight) {
9-
// TODO
109
// Should return the current state (i.e. colour) of the `trafficLight`
1110
// object passed as a parameter.
11+
return trafficLight.possibleStates[trafficLight.stateIndex]; //return array[index]
1212
}
1313

1414
function getNextStateIndex(trafficLight) {
15-
// TODO
1615
// Return the index of the next state of the `trafficLight` such that:
1716
// - if the color is green, it will turn to orange
1817
// - if the color is orange, it will turn to red
1918
// - if the color is red, it will turn to green
19+
let nextIndex = trafficLight.stateIndex +1; //declare nextIndex = index+1
20+
if (nextIndex>=trafficLight.possibleStates.length){ //if indexNum is greater than array's length
21+
nextIndex=0; //assign it to 0
22+
}
23+
return nextIndex;
2024
}
2125

2226
// This function loops for the number of seconds specified by the `secs`

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
function runExperiment(sampleSize) {
44
const valueCounts = [0, 0, 0, 0, 0, 0];
55

6-
// TODO
6+
for (let i=0; i<sampleSize; i++){
7+
const random = Math.floor((Math.random()*6)+1);
8+
valueCounts[random-1]+=1; //valueCount[random-1]+=1;
9+
}
710
// Write a for loop that iterates `sampleSize` times (sampleSize is a number).
811
// In each loop iteration:
912
//
@@ -12,10 +15,12 @@ function runExperiment(sampleSize) {
1215
// value from the previous step. Use the first element of `valueCounts`
1316
// for keeping a count how many times the value 1 is thrown, the second
1417
// element for value 2, etc.
15-
18+
19+
1620
const results = [];
17-
18-
// TODO
21+
for (let i of valueCounts){
22+
results.push((i/sampleSize*100).toFixed(2)); // percentage= (i/sampleSize*100).toFixed(2)
23+
}
1924
// Write a for..of loop for the `valueCounts` array created in the previous
2025
// loop. In each loop iteration:
2126
// 1. For each possible value of the die (1-6), compute the percentage of how
@@ -30,12 +35,16 @@ function runExperiment(sampleSize) {
3035

3136
function main() {
3237
const sampleSizes = [100, 1000, 1000000];
33-
34-
// TODO
38+
39+
for (let i of sampleSizes){
40+
console.log(runExperiment(i));
41+
}
3542
// Write a for..of loop that calls the `runExperiment()` function for each
3643
// value of the `sampleSizes` array.
3744
// Log the results of each experiment as well as the experiment size to the
3845
// console.
46+
47+
3948
// The expected output could look like this:
4049
//
4150
// [ '26.00', '17.00', '10.00', '19.00', '16.00', '12.00' ] 100

0 commit comments

Comments
 (0)