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

Skip to content

Commit dcc7128

Browse files
committed
finished the practice exercises & prep exercises
1 parent 61e010f commit dcc7128

File tree

7 files changed

+85
-4
lines changed

7 files changed

+85
-4
lines changed

Week1/practice-exercises/1-remove-the-comma.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
*/
77

88
let myString = 'hello,this,is,a,difficult,to,read,sentence';
9-
9+
myString = myString.split(",").join(" ");
10+
console.log(myString);
1011

1112

1213
/* --- Code that will test your solution, do NOT change. Write above this line --- */
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
/**
2-
* Report whether or not a number is odd/even!
2+
* Report whether a number is odd/even!
33
*
44
* Create a for loop, that iterates from 0 to 20.
55
* Create a conditional statement that checks if the value of the counter variable is odd or even.
66
* If it's odd, log to the console The number [PUT_NUMBER_HERE] is odd!.
77
* If it's even, log to the console The number [PUT_NUMBER_HERE] is even!.
88
*/
99

10+
function oddCheck(counter) {
11+
let resultOfCheck = counter % 2 === 0? 'even' : 'odd';
12+
console.log(`The number ${counter} is ${resultOfCheck}`)
13+
}
14+
15+
16+
oddCheck(2);
17+
oddCheck(5);
18+
oddCheck(0);

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,27 @@
1212
* Ingredients: 4 eggs, 2 strips of bacon, 1 tsp salt/pepper
1313
*/
1414

15+
16+
const recipe = {};
17+
recipe.title = "Omelette";
18+
recipe.servings = 2;
19+
recipe.ingredients = ["4 eggs", "2 strips of bacon", "1 tsp salt/pepper"];
20+
21+
for (const key in recipe) {
22+
//switch for more descriptive logging
23+
switch(key) {
24+
case 'title':
25+
console.log(`Meal name: ${recipe[key]}`);
26+
break;
27+
case 'servings':
28+
console.log(`Serves: ${recipe[key]}`);
29+
break;
30+
case 'ingredients':
31+
console.log(`Ingredients: ${recipe[key].join(', ')}`);
32+
break;
33+
default:
34+
console.log(`${key}: ${recipe[key]}`);
35+
break;
36+
}
37+
}
38+

Week1/practice-exercises/4-reading-list.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,13 @@
99
* If you haven't read it log a string like You still need to read "The Lord of the Rings"
1010
*/
1111

12+
const books= [
13+
{ title: "The Hobbit", author: "J.R.R. Tolkien", alreadyRead: false },
14+
{ title: "The Comedy Bible", author: "J. Carter", alreadyRead: true },
15+
{ title: "The Enchiridion", author: "Epictetus", alreadyRead: false }
16+
];
17+
18+
for (book of books) {
19+
const status = book.alreadyRead === true? 'You have already read':'You still need to read';
20+
console.log(`${status} ${book.title} by ${book.author}.`);
21+
}

Week1/practice-exercises/5-who-wants-a-drink.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,25 @@
88
*/
99

1010
// There are 3 different types of drinks:
11-
const drinkTypes = ['cola', 'lemonade', 'water'];
11+
const drinkTypes = ['cola', 'lemonade', 'water'];
12+
13+
const drinkTray = [];
14+
const drinkLimits = { cola: 0, lemonade: 0, water: 0 };
15+
const maxPerDrink = 2;
16+
17+
for (let i = 0; i < 5; i++) {
18+
let drinkAdded = false;
19+
while (!drinkAdded) {
20+
// Randomly select a drink from the drinkTypes array
21+
const drink = drinkTypes[Math.floor(Math.random() * drinkTypes.length)];
22+
23+
// Check if we can add this drink to the tray
24+
if (drinkLimits[drink] < maxPerDrink) {
25+
drinkTray.push(drink);
26+
drinkLimits[drink] += 1;
27+
drinkAdded = true;
28+
}
29+
}
30+
}
31+
32+
console.log(`Hey guys, I brought a ${drinkTray.join(', ')}!`);

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ while (rotations < 2) {
1616
// if the color is green, turn it orange
1717
// if the color is orange, turn it red
1818
// if the color is red, add 1 to rotations and turn it green
19+
if (currentState === "green") {
20+
trafficLight.state = "orange";
21+
} else if (currentState === "orange") {
22+
trafficLight.state = "red";
23+
} else if (currentState === "red") {
24+
rotations ++;
25+
trafficLight.state = "green"
26+
}
1927
}
2028

2129
/**

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22
/**
3-
* The `possibleStates` property define the states (in this case: colours)
3+
* The `possibleStates` property defines the states (in this case: colours)
44
* in which the traffic light can be.
55
* The `stateIndex` property indicates which of the possible states is current.
66
*/
@@ -18,6 +18,14 @@ while (cycle < 2) {
1818
// if the color is green, turn it orange
1919
// if the color is orange, turn it red
2020
// if the color is red, add 1 to cycles and turn it green
21+
if (currentState === "green") {
22+
trafficLight.stateIndex ++;
23+
} else if (currentState === "orange") {
24+
trafficLight.stateIndex ++;
25+
} else if (currentState === "red") {
26+
cycle ++;
27+
trafficLight.stateIndex = 0;
28+
}
2129
}
2230

2331
/**

0 commit comments

Comments
 (0)