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

Skip to content

Commit 0f0722c

Browse files
committed
week 3 prep-exercise completed
2 parents c5f06dd + 748731f commit 0f0722c

File tree

5 files changed

+102
-19
lines changed

5 files changed

+102
-19
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
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
55
* functions, as and when needed.
66
*/
77

88
function getCurrentState(trafficLight) {
9+
return trafficLight.possibleStates[trafficLight.stateIndex];
910
// TODO
1011
// Should return the current state (i.e. colour) of the `trafficLight`
1112
// object passed as a parameter.
1213
}
1314

1415
function getNextStateIndex(trafficLight) {
16+
return trafficLight.stateIndex == 2 ? 0 : ++trafficLight.stateIndex;
1517
// TODO
1618
// Return the index of the next state of the `trafficLight` such that:
1719
// - if the color is green, it will turn to orange
@@ -33,13 +35,13 @@ function waitSync(secs) {
3335

3436
function main() {
3537
const trafficLight = {
36-
possibleStates: ["green", "orange", "red"],
38+
possibleStates: ['green', 'orange', 'red'],
3739
stateIndex: 0,
3840
};
3941

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

4446
waitSync(1); // Wait a second before going to the next state
4547
trafficLight.stateIndex = getNextStateIndex(trafficLight);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Dice experimentation</title>
7+
</head>
8+
<body>
9+
<h1>Dice experimentation</h1>
10+
<h2>Results :</h2>
11+
12+
<p id="1"></p>
13+
<p id="2"></p>
14+
<p id="3"></p>
15+
<script src="./index.js"></script>
16+
</body>
17+
</html>

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

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1-
"use strict";
1+
'use strict';
2+
// new function is created for code reusable.
3+
// This function will give you random number from 1 to 6
4+
function rollDice(min, max) {
5+
return Math.floor(Math.random() * (max - min + 1)) + 1;
6+
}
7+
8+
function percent(diceRolled, totalDiceRoll) {
9+
return (diceRolled / totalDiceRoll) * 100;
10+
}
211

312
function runExperiment(sampleSize) {
413
const valueCounts = [0, 0, 0, 0, 0, 0];
5-
14+
for (let index = sampleSize; index > 0; --index) {
15+
let result = rollDice(1, 6);
16+
valueCounts[result - 1] += 1;
17+
}
618
// TODO
719
// Write a for loop that iterates `sampleSize` times (sampleSize is a number).
820
// In each loop iteration:
@@ -14,7 +26,14 @@ function runExperiment(sampleSize) {
1426
// element for value 2, etc.
1527

1628
const results = [];
17-
29+
valueCounts.forEach((rolledDiceCount) => {
30+
// console.log(percent(rolledDiceCount, sampleSize));
31+
results.push(
32+
` '${parseFloat(percent(rolledDiceCount, sampleSize))
33+
.toFixed(2)
34+
.toString()}'`
35+
);
36+
});
1837
// TODO
1938
// Write a for..of loop for the `valueCounts` array created in the previous
2039
// loop. In each loop iteration:
@@ -31,13 +50,24 @@ function runExperiment(sampleSize) {
3150
function main() {
3251
const sampleSizes = [100, 1000, 1000000];
3352

53+
for (let index = 0; index < sampleSizes.length; index++) {
54+
console.log(
55+
`[ ${runExperiment(sampleSizes[index])} ] ${sampleSizes[index]}`
56+
);
57+
58+
// This just extra to print the result on the webpage
59+
document.getElementById(
60+
(index + 1).toString()
61+
).innerHTML = `[ ${runExperiment(sampleSizes[index])} ] ${
62+
sampleSizes[index]
63+
}`;
64+
}
3465
// 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.
66+
// Write a for..of loop
67+
// that calls the `runExperiment()` function for each value of the `sampleSizes` array.
68+
// Log the results of each experiment as well as the experiment size to the console.
3969
// The expected output could look like this:
40-
//
70+
4171
// [ '26.00', '17.00', '10.00', '19.00', '16.00', '12.00' ] 100
4272
// [ '14.60', '17.10', '19.30', '15.50', '16.70', '16.80' ] 1000
4373
// [ '16.71', '16.68', '16.69', '16.66', '16.67', '16.59' ] 1000000
Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { modules, students, mentors, classes } from "./hyf.js";
1+
import { modules, students, mentors, classes } from './hyf.js';
22

33
/**
44
* Tjebbe would like help to get a list of possible mentors for a module.
@@ -8,10 +8,16 @@ import { modules, students, mentors, classes } from "./hyf.js";
88
* ['John', 'Mary']
99
*/
1010
const possibleMentorsForModule = (moduleName) => {
11-
// TODO complete this function
11+
const mentorList = [];
12+
mentors.forEach((mentor) => {
13+
if (mentor.canTeach.includes(moduleName)) {
14+
mentorList.push(mentor.name);
15+
}
16+
});
17+
return mentorList;
1218
};
1319
// You can uncomment out this line to try your function
14-
// console.log(possibleMentorsForModule('using-apis'));
20+
console.log(`${possibleMentorsForModule('using-apis')} can teach API module`);
1521

1622
/**
1723
* Tjebbe wants to make it even easier for himself.
@@ -20,7 +26,17 @@ const possibleMentorsForModule = (moduleName) => {
2026
* It should return a single name.
2127
*/
2228
const findMentorForModule = (moduleName) => {
23-
// TODO complete this function
29+
const mentorList = [];
30+
let randomNum;
31+
mentors.forEach((mentor) => {
32+
if (mentor.canTeach.includes(moduleName)) {
33+
mentorList.push(mentor.name);
34+
}
35+
});
36+
randomNum = Math.floor(Math.random() * (mentorList.length - 1 - 0 + 1)) + 0;
37+
return mentorList[randomNum];
2438
};
2539
// You can uncomment out this line to try your function
26-
// console.log(findMentorForModule('javascript'));
40+
console.log(
41+
`${findMentorForModule('javascript')} is going to teach Javascript`
42+
);

Week3/prep-exercises/1-hyf-program/2-class-list.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { modules, students, mentors, classes } from "./hyf.js";
1+
import { modules, students, mentors, classes } from './hyf.js';
22

33
/**
44
* We would like to have a list of everyone that is currently participating in a class.
@@ -12,10 +12,28 @@ import { modules, students, mentors, classes } from "./hyf.js";
1212
* [{ name: 'John', role: 'student' }, { name: 'Mary', role: 'mentor' }]
1313
*/
1414
const getPeopleOfClass = (className) => {
15-
// TODO complete this function
15+
let everyone = [];
16+
let currentModule;
17+
18+
classes.forEach((eachClass) => {
19+
if (eachClass.name == className) currentModule = eachClass.currentModule;
20+
});
21+
22+
students.forEach((stud) => {
23+
if (stud.class == className) {
24+
everyone.push({ name: stud.name, role: 'student' });
25+
}
26+
});
27+
28+
mentors.forEach((mentor) => {
29+
if (mentor.nowTeaching == currentModule)
30+
everyone.push({ name: mentor.name, role: 'mentor' });
31+
});
32+
33+
return everyone;
1634
};
1735
// You can uncomment out this line to try your function
18-
// console.log(getPeopleOfClass('class34'));
36+
console.log(getPeopleOfClass('class34'));
1937

2038
/**
2139
* We would like to have a complete overview of the current active classes.

0 commit comments

Comments
 (0)