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

Skip to content

Commit f319fb8

Browse files
committed
new_commit
1 parent b0f8b3d commit f319fb8

File tree

4 files changed

+163
-81
lines changed

4 files changed

+163
-81
lines changed

Week1/new.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Exercise 1: Calculate the Sum of Array Elements:
3+
Write a function calculateSum that takes an array of numbers as input and returns the sum of all the elements in the array.
4+
*/
5+
6+
const totalNumbers = [6, 7, 8, 6, 6, 6, 9];
7+
8+
/*
9+
Exercise 2: Calculate the Average of Array Elements:
10+
Write a function calculateAverage that takes an array of numbers as input and returns the average of all the elements in the array.
11+
*/
12+
function calAvg(averageNumbers) {
13+
let sum = 0;
14+
for (let i = 0; i < averageNumbers.length; i++) {
15+
sum += averageNumbers[i];
16+
}
17+
return sum / averageNumbers.length;
18+
}
19+
20+
const averageNumbers = [6, 3, 48, 4, 12, 8, 16];
21+
const average = calAvg(averageNumbers);
22+
console.log("Average:", average);
23+
24+
25+
26+
27+
/*
28+
Exercise 3: Find the Largest Number in an Array:
29+
Write a function findLargestNumber that takes an array of numbers as input and returns the largest number in the array.
30+
*/
31+
32+
const largestNumbers = [3, 7, 9, 48, 6, 6, 18];

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

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
"use strict";
2-
/**
3-
* The `state` property says what the traffic light's state (i.e. colour) is at
4-
* that moment.
5-
*/
2+
3+
64
const trafficLight = {
75
state: "green",
86
};
@@ -12,25 +10,7 @@ while (rotations < 2) {
1210
const currentState = trafficLight.state;
1311
console.log("The traffic light is on", currentState);
1412

15-
// TODO
16-
// if the color is green, turn it orange
17-
// if the color is orange, turn it red
18-
// if the color is red, add 1 to rotations and turn it green
19-
}
20-
21-
/**
22-
* The output should be:
23-
24-
The traffic light is on green
25-
The traffic light is on orange
26-
The traffic light is on red
27-
The traffic light is on green
28-
The traffic light is on orange
29-
The traffic light is on red
30-
31-
*/
32-
33-
// We use a switch statement to handle different states of the traffic light
13+
// Handling the traffic light state transitions
3414
switch (currentState) {
3515
case "green":
3616
trafficLight.state = "orange";

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

Lines changed: 66 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,80 @@
1+
// "use strict";
2+
// /**
3+
// * The `trafficLight` object is now no longer a global variable. Instead,
4+
// * it is defined in function `main()` and passed as a parameter to other
5+
// * functions, as and when needed.
6+
// */
7+
8+
// function getCurrentState(trafficLight) {
9+
// // TODO
10+
// // Should return the current state (i.e. colour) of the `trafficLight`
11+
// // object passed as a parameter.
12+
// }
13+
14+
// function getNextStateIndex(trafficLight) {
15+
// // TODO
16+
// // Return the index of the next state of the `trafficLight` such that:
17+
// // - if the color is green, it will turn to orange
18+
// // - if the color is orange, it will turn to red
19+
// // - if the color is red, it will turn to green
20+
// }
21+
22+
// // This function loops for the number of seconds specified by the `secs`
23+
// // parameter and then returns.
24+
// // IMPORTANT: This is not the recommended way to implement 'waiting' in
25+
// // JavaScript. You will learn better ways of doing this when you learn about
26+
// // asynchronous code.
27+
// function waitSync(secs) {
28+
// const start = Date.now();
29+
// while (Date.now() - start < secs * 1000) {
30+
// // nothing do to here
31+
// }
32+
// }
33+
34+
// function main() {
35+
// const trafficLight = {
36+
// possibleStates: ["green", "orange", "red"],
37+
// stateIndex: 0,
38+
// };
39+
40+
// for (let cycle = 0; cycle < 6; cycle++) {
41+
// const currentState = getCurrentState(trafficLight);
42+
// console.log(cycle, "The traffic light is now", currentState);
43+
44+
// waitSync(1); // Wait a second before going to the next state
45+
// trafficLight.stateIndex = getNextStateIndex(trafficLight);
46+
// }
47+
// }
48+
49+
// main();
50+
// /**
51+
// * The output should be:
52+
53+
// 0 The traffic light is now green
54+
// 1 The traffic light is now orange
55+
// 2 The traffic light is now red
56+
// 3 The traffic light is now green
57+
// 4 The traffic light is now orange
58+
// 5 The traffic light is now red
59+
60+
// */
61+
162
"use strict";
2-
/**
3-
* The `trafficLight` object is now no longer a global variable. Instead,
4-
* it is defined in function `main()` and passed as a parameter to other
5-
* functions, as and when needed.
6-
*/
763

864
function getCurrentState(trafficLight) {
9-
// TODO
10-
// Should return the current state (i.e. colour) of the `trafficLight`
11-
// object passed as a parameter.
65+
return trafficLight.possibleStates[trafficLight.stateIndex];
1266
}
1367

1468
function getNextStateIndex(trafficLight) {
15-
// TODO
16-
// Return the index of the next state of the `trafficLight` such that:
17-
// - if the color is green, it will turn to orange
18-
// - if the color is orange, it will turn to red
19-
// - if the color is red, it will turn to green
69+
// Increment the state index, wrapping around to 0 if it exceeds the length
70+
// of the possibleStates array.
71+
return (trafficLight.stateIndex + 1) % trafficLight.possibleStates.length;
2072
}
2173

22-
// This function loops for the number of seconds specified by the `secs`
23-
// parameter and then returns.
24-
// IMPORTANT: This is not the recommended way to implement 'waiting' in
25-
// JavaScript. You will learn better ways of doing this when you learn about
26-
// asynchronous code.
2774
function waitSync(secs) {
2875
const start = Date.now();
2976
while (Date.now() - start < secs * 1000) {
30-
// nothing do to here
77+
// Do nothing here
3178
}
3279
}
3380

@@ -47,14 +94,3 @@ function main() {
4794
}
4895

4996
main();
50-
/**
51-
* The output should be:
52-
53-
0 The traffic light is now green
54-
1 The traffic light is now orange
55-
2 The traffic light is now red
56-
3 The traffic light is now green
57-
4 The traffic light is now orange
58-
5 The traffic light is now red
59-
60-
*/
Lines changed: 62 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,80 @@
1+
// "use strict";
2+
3+
// function runExperiment(sampleSize) {
4+
// const valueCounts = [0, 0, 0, 0, 0, 0];
5+
6+
// // TODO
7+
// // Write a for loop that iterates `sampleSize` times (sampleSize is a number).
8+
// // In each loop iteration:
9+
// //
10+
// // 1. Generate a random integer between 1 and 6 (as if throwing a six-sided die).
11+
// // 2. Add `1` to the element of the `valueCount` that corresponds to the random
12+
// // value from the previous step. Use the first element of `valueCounts`
13+
// // for keeping a count how many times the value 1 is thrown, the second
14+
// // element for value 2, etc.
15+
16+
// const results = [];
17+
18+
// // TODO
19+
// // Write a for..of loop for the `valueCounts` array created in the previous
20+
// // loop. In each loop iteration:
21+
// // 1. For each possible value of the die (1-6), compute the percentage of how
22+
// // many times that value was thrown. Remember that the first value of
23+
// // `valueCounts` represent the die value of 1, etc.
24+
// // 2. Convert the computed percentage to a number string with a precision of
25+
// // two decimals, e.g. '14.60'.
26+
// // 3. Then push that string onto the `results` array.
27+
28+
// return results;
29+
// }
30+
31+
// function main() {
32+
// const sampleSizes = [100, 1000, 1000000];
33+
34+
// // TODO
35+
// // Write a for..of loop that calls the `runExperiment()` function for each
36+
// // value of the `sampleSizes` array.
37+
// // Log the results of each experiment as well as the experiment size to the
38+
// // console.
39+
// // The expected output could look like this:
40+
// //
41+
// // [ '26.00', '17.00', '10.00', '19.00', '16.00', '12.00' ] 100
42+
// // [ '14.60', '17.10', '19.30', '15.50', '16.70', '16.80' ] 1000
43+
// // [ '16.71', '16.68', '16.69', '16.66', '16.67', '16.59' ] 1000000
44+
// }
45+
46+
// main();
47+
148
"use strict";
249

350
function runExperiment(sampleSize) {
451
const valueCounts = [0, 0, 0, 0, 0, 0];
552

6-
// TODO
7-
// Write a for loop that iterates `sampleSize` times (sampleSize is a number).
8-
// In each loop iteration:
9-
//
10-
// 1. Generate a random integer between 1 and 6 (as if throwing a six-sided die).
11-
// 2. Add `1` to the element of the `valueCount` that corresponds to the random
12-
// value from the previous step. Use the first element of `valueCounts`
13-
// for keeping a count how many times the value 1 is thrown, the second
14-
// element for value 2, etc.
53+
// Step 1: Generate random dice rolls and count occurrences
54+
for (let i = 0; i < sampleSize; i++) {
55+
const roll = Math.floor(Math.random() * 6) + 1; // Generate a random number between 1 and 6
56+
valueCounts[roll - 1]++; // Increment the corresponding count in valueCounts
57+
}
1558

1659
const results = [];
1760

18-
// TODO
19-
// Write a for..of loop for the `valueCounts` array created in the previous
20-
// loop. In each loop iteration:
21-
// 1. For each possible value of the die (1-6), compute the percentage of how
22-
// many times that value was thrown. Remember that the first value of
23-
// `valueCounts` represent the die value of 1, etc.
24-
// 2. Convert the computed percentage to a number string with a precision of
25-
// two decimals, e.g. '14.60'.
26-
// 3. Then push that string onto the `results` array.
61+
// Step 2: Calculate the percentage of each value and format the results
62+
for (let count of valueCounts) {
63+
const percentage = ((count / sampleSize) * 100).toFixed(2); // Calculate the percentage and fix to 2 decimal places
64+
results.push(percentage); // Push the formatted percentage to the results array
65+
}
2766

2867
return results;
2968
}
3069

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

34-
// TODO
35-
// Write a for..of loop that calls the `runExperiment()` function for each
36-
// value of the `sampleSizes` array.
37-
// Log the results of each experiment as well as the experiment size to the
38-
// console.
39-
// The expected output could look like this:
40-
//
41-
// [ '26.00', '17.00', '10.00', '19.00', '16.00', '12.00' ] 100
42-
// [ '14.60', '17.10', '19.30', '15.50', '16.70', '16.80' ] 1000
43-
// [ '16.71', '16.68', '16.69', '16.66', '16.67', '16.59' ] 1000000
73+
// Run experiments for different sample sizes and log the results
74+
for (let sampleSize of sampleSizes) {
75+
const percentages = runExperiment(sampleSize);
76+
console.log(percentages, sampleSize);
77+
}
4478
}
4579

4680
main();

0 commit comments

Comments
 (0)