Skip to content

Samira exercises #59

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions Week1/practice-exercises/1-remove-the-comma.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

let myString = 'hello,this,is,a,difficult,to,read,sentence';

myString = myString.split(',').join(' ');

console.log(myString);



/* --- Code that will test your solution, do NOT change. Write above this line --- */
Expand Down
8 changes: 8 additions & 0 deletions Week1/practice-exercises/2-even-odd-reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@
* If it's even, log to the console The number [PUT_NUMBER_HERE] is even!.
*/

for (let i = 0; i <= 20; i++) {

if (i % 2 === 0) {
console.log(`The number ${i} is even!`);
} else {
console.log(`The number ${i} is odd!`);
}
}
9 changes: 9 additions & 0 deletions Week1/practice-exercises/3-recipe-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,12 @@
* Ingredients: 4 eggs, 2 strips of bacon, 1 tsp salt/pepper
*/

let recipe = {};

recipe.title = 'Omelette';
recipe.servings = 2;
recipe.ingredients = ['4 eggs', '2 strips of bacon', '1 tsp salt/pepper'];

for(let key in recipe) {
console.log(`${key}: ${recipe[key]}`);
}
16 changes: 16 additions & 0 deletions Week1/practice-exercises/4-reading-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,19 @@
* If you haven't read it log a string like You still need to read "The Lord of the Rings"
*/

let books = [
{ title: 'the Hobbit', author: 'J.R.R. Tolkien', alreadyRead: true },
{ title: 'the Lord of the Rings', author: 'J.R.R. Tolkien', alreadyRead: false },
{ title: 'Harry Potter and the Sorcerer\'s Stone', author: 'J.K. Rowling', alreadyRead: true }
];

for(let book of books) {

console.log(`${book.title} by ${book.author}`);

if(book.alreadyRead) {
console.log(`You already read "${book.title}".`);
} else {
console.log(`You still need to read "${book.title}".`);
}
}
11 changes: 10 additions & 1 deletion Week1/practice-exercises/5-who-wants-a-drink.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,13 @@
*/

// There are 3 different types of drinks:
const drinkTypes = ['cola', 'lemonade', 'water'];
const drinkTypes = ['cola', 'lemonade', 'water'];

let drinkTray = [];

for (let i = 0; i < 5; i++) {

drinkTray.push(drinkTypes[i % drinkTypes.length]);
}

console.log(`Hey guys, I brought a ${drinkTray.join(', ')}!`);
19 changes: 5 additions & 14 deletions Week1/prep-exercises/1-traffic-light/traffic-light-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,12 @@ let rotations = 0;
while (rotations < 2) {
const currentState = trafficLight.state;
console.log("The traffic light is on", currentState);

// TODO
// if the color is green, turn it orange
// if the color is orange, turn it red
// if the color is red, add 1 to rotations and turn it green
}

/**
* The output should be:
if (currentState === "green") { trafficLight.state = "orange";

The traffic light is on green
The traffic light is on orange
The traffic light is on red
The traffic light is on green
The traffic light is on orange
The traffic light is on red
} else if (currentState === "orange") { trafficLight.state = "red";

*/
} else if (currentState === "red") { trafficLight.state = "green";
rotations++;
}
32 changes: 11 additions & 21 deletions Week1/prep-exercises/1-traffic-light/traffic-light-2.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
"use strict";
/**
* The `possibleStates` property define the states (in this case: colours)
* in which the traffic light can be.
* The `stateIndex` property indicates which of the possible states is current.
*/

const trafficLight = {
possibleStates: ["green", "orange", "red"],
stateIndex: 0,
Expand All @@ -14,20 +10,14 @@ while (cycle < 2) {
const currentState = trafficLight.possibleStates[trafficLight.stateIndex];
console.log("The traffic light is on", currentState);

// TODO
// if the color is green, turn it orange
// if the color is orange, turn it red
// if the color is red, add 1 to cycles and turn it green
}

/**
* The output should be:

The traffic light is on green
The traffic light is on orange
The traffic light is on red
The traffic light is on green
The traffic light is on orange
The traffic light is on red
if (currentState === "green")
{ trafficLight.stateIndex = 1;
} else if (currentState === "orange")
{ trafficLight.stateIndex = 2 ;

*/
} else if (currentState === "red") {
trafficLight.stateIndex = 0;

cycle++;
}
}
9 changes: 7 additions & 2 deletions Week2/prep-exercises/1-traffic-light/traffic-light.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@
*/

function getCurrentState(trafficLight) {
// TODO

return trafficLight.possibleStates[trafficLight.stateIndex];
// Should return the current state (i.e. colour) of the `trafficLight`
// object passed as a parameter.
}

function getNextStateIndex(trafficLight) {
// TODO

if (trafficLight.stateIndex === trafficLight.possibleStates.length - 1) {
return 0;
}
return trafficLight.stateIndex + 1;
// Return the index of the next state of the `trafficLight` such that:
// - if the color is green, it will turn to orange
// - if the color is orange, it will turn to red
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
Expand Up @@ -3,7 +3,10 @@
function runExperiment(sampleSize) {
const valueCounts = [0, 0, 0, 0, 0, 0];

// TODO
for (let i = 0; i < sampleSize; i++) {
const randomValue = Math.floor(Math.random() * 6) + 1;
valueCounts[randomValue - 1]++;
}
// Write a for loop that iterates `sampleSize` times (sampleSize is a number).
// In each loop iteration:
//
Expand All @@ -15,7 +18,11 @@ function runExperiment(sampleSize) {

const results = [];

// TODO
for (let i = 0; i < valueCounts.length; i++) {
const percentage =((valueCounts[i] / sampleSize) * 100).toFixed(2);
results.push(`${percentage}`);
}

// Write a for..of loop for the `valueCounts` array created in the previous
// loop. In each loop iteration:
// 1. For each possible value of the die (1-6), compute the percentage of how
Expand All @@ -31,7 +38,11 @@ function runExperiment(sampleSize) {
function main() {
const sampleSizes = [100, 1000, 1000000];

// TODO
for (const sampleSize of sampleSizes) {
const results = runExperiment(sampleSize);
console.log(`${results.join(', ')}`, sampleSize);
}

// Write a for..of loop that calls the `runExperiment()` function for each
// value of the `sampleSizes` array.
// Log the results of each experiment as well as the experiment size to the
Expand Down