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

Skip to content

Commit c29a691

Browse files
committed
week3 exercises done
1 parent 90f2ee0 commit c29a691

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

Week2/practice-exercises/3-recipe-card.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ const recipe={
1717
ingredients:["4 eggs","2 strips of bacon","1 tsp salt/pepper"]
1818
};
1919
for(let property in recipe){
20-
if(property=='title'){
20+
if(property==='title'){
2121
console.log(`Meal name:${recipe.title}`);
2222
}
23-
if(property=='servings'){
23+
if(property==='servings'){
2424
console.log(`Serves:${recipe.servings}`);
2525
}
26-
if(property=='ingredients'){
27-
console.log(`Ingredients:${recipe.ingredients}`);
26+
if(property==='ingredients'){
27+
console.log(`Ingredients:${recipe.ingredients} `);
28+
console.log(recipe.ingredients);
2829
}
2930
}
3031

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,28 @@
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+
if(trafficLight.stateIndex===0){
17+
trafficLight.stateIndex=1;
18+
}else if(trafficLight.stateIndex===1){
19+
trafficLight.stateIndex=2;
20+
}else
21+
{trafficLight.stateIndex=0;}
22+
return trafficLight.stateIndex;
1523
// TODO
1624
// Return the index of the next state of the `trafficLight` such that:
1725
// - if the color is green, it will turn to orange
1826
// - if the color is orange, it will turn to red
1927
// - if the color is red, it will turn to green
2028
}
2129

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.
30+
2731
function waitSync(secs) {
2832
const start = Date.now();
2933
while (Date.now() - start < secs * 1000) {

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
function runExperiment(sampleSize) {
44
const valueCounts = [0, 0, 0, 0, 0, 0];
5+
6+
for(let i=0; i<sampleSize; i++){
7+
let x=Math.floor(Math.random()*6+1);
8+
let numberArray=[1,2,3,4,5,6];
9+
valueCounts[numberArray.findIndex(element=>element===x)]++;
10+
11+
}
512

613
// TODO
714
// Write a for loop that iterates `sampleSize` times (sampleSize is a number).
@@ -14,6 +21,9 @@ function runExperiment(sampleSize) {
1421
// element for value 2, etc.
1522

1623
const results = [];
24+
for(let valueCount of valueCounts){
25+
results.push(((valueCount/sampleSize)*100).toFixed(2));
26+
}
1727

1828
// TODO
1929
// Write a for..of loop for the `valueCounts` array created in the previous
@@ -24,12 +34,17 @@ function runExperiment(sampleSize) {
2434
// 2. Convert the computed percentage to a number string with a precision of
2535
// two decimals, e.g. '14.60'.
2636
// 3. Then push that string onto the `results` array.
27-
37+
console.log(results);
2838
return results;
39+
2940
}
3041

3142
function main() {
3243
const sampleSizes = [100, 1000, 1000000];
44+
for(let sampleSize of sampleSizes){
45+
runExperiment(sampleSize);
46+
47+
}
3348

3449
// TODO
3550
// Write a for..of loop that calls the `runExperiment()` function for each

0 commit comments

Comments
 (0)