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

Skip to content

Week2 js prep exercice #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions Week2/prep-exercises/1-traffic-light/traffic-light.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"use strict";
'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
Expand All @@ -9,6 +9,8 @@ function getCurrentState(trafficLight) {
// TODO
// Should return the current state (i.e. colour) of the `trafficLight`
// object passed as a parameter.
const { stateIndex, possibleStates } = trafficLight;
return possibleStates[stateIndex];
}

function getNextStateIndex(trafficLight) {
Expand All @@ -17,6 +19,9 @@ function getNextStateIndex(trafficLight) {
// - 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
let { stateIndex, possibleStates } = trafficLight;
const possibleStatesLength = possibleStates.length;
return stateIndex >= possibleStatesLength - 1 ? 0 : ++stateIndex;
}

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

function main() {
const trafficLight = {
possibleStates: ["green", "orange", "red"],
possibleStates: ['green', 'orange', 'red'],
stateIndex: 0,
};

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

waitSync(1); // Wait a second before going to the next state
trafficLight.stateIndex = getNextStateIndex(trafficLight);
Expand Down
17 changes: 14 additions & 3 deletions Week2/prep-exercises/2-experiments/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
"use strict";
'use strict';

function runExperiment(sampleSize) {
function runExperiment(sampleSize = 100) {
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 = 1; i <= sampleSize; i++) {
const randomNumber = Math.floor(Math.random() * 6);
++valueCounts[randomNumber];
}
const results = [];

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

for (const count of valueCounts) {
results.push(((count / sampleSize) * 100).toFixed(2));
}
return results;
}

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

for (const size of sampleSizes) {
const result = runExperiment(size);
console.log(result, size);
}
// TODO
// Write a for..of loop that calls the `runExperiment()` function for each
// value of the `sampleSizes` array.
Expand Down