From c06454682f6efe1c35ceacc27b9ebaadbfe49a9e Mon Sep 17 00:00:00 2001 From: Samira Date: Mon, 25 Nov 2024 10:07:03 +0100 Subject: [PATCH 1/2] update change --- .../1-traffic-light/traffic-light-1.js | 19 +++-------- .../1-traffic-light/traffic-light-2.js | 32 +++++++------------ 2 files changed, 16 insertions(+), 35 deletions(-) diff --git a/Week1/prep-exercises/1-traffic-light/traffic-light-1.js b/Week1/prep-exercises/1-traffic-light/traffic-light-1.js index f1d9169..982af8a 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-1.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-1.js @@ -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++; + } \ No newline at end of file diff --git a/Week1/prep-exercises/1-traffic-light/traffic-light-2.js b/Week1/prep-exercises/1-traffic-light/traffic-light-2.js index 8c6ba95..5429fbf 100644 --- a/Week1/prep-exercises/1-traffic-light/traffic-light-2.js +++ b/Week1/prep-exercises/1-traffic-light/traffic-light-2.js @@ -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, @@ -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++; + } + } \ No newline at end of file From 01ca21a8ef905892c6a472fc68ef7b9be519aa57 Mon Sep 17 00:00:00 2001 From: Samira Date: Fri, 29 Nov 2024 13:24:31 +0100 Subject: [PATCH 2/2] Updated all practice exercises --- Week1/practice-exercises/1-remove-the-comma.js | 4 ++++ Week1/practice-exercises/2-even-odd-reporter.js | 8 ++++++++ Week1/practice-exercises/3-recipe-card.js | 9 +++++++++ Week1/practice-exercises/4-reading-list.js | 16 ++++++++++++++++ Week1/practice-exercises/5-who-wants-a-drink.js | 11 ++++++++++- .../1-traffic-light/traffic-light.js | 9 +++++++-- Week2/prep-exercises/2-experiments/index.js | 17 ++++++++++++++--- 7 files changed, 68 insertions(+), 6 deletions(-) diff --git a/Week1/practice-exercises/1-remove-the-comma.js b/Week1/practice-exercises/1-remove-the-comma.js index b71cffd..36a4b9c 100644 --- a/Week1/practice-exercises/1-remove-the-comma.js +++ b/Week1/practice-exercises/1-remove-the-comma.js @@ -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 --- */ diff --git a/Week1/practice-exercises/2-even-odd-reporter.js b/Week1/practice-exercises/2-even-odd-reporter.js index 6edf23e..5bee5bf 100644 --- a/Week1/practice-exercises/2-even-odd-reporter.js +++ b/Week1/practice-exercises/2-even-odd-reporter.js @@ -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!`); + } +} \ No newline at end of file diff --git a/Week1/practice-exercises/3-recipe-card.js b/Week1/practice-exercises/3-recipe-card.js index 24bcb54..6644cc4 100644 --- a/Week1/practice-exercises/3-recipe-card.js +++ b/Week1/practice-exercises/3-recipe-card.js @@ -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]}`); +} \ No newline at end of file diff --git a/Week1/practice-exercises/4-reading-list.js b/Week1/practice-exercises/4-reading-list.js index f535657..0d77644 100644 --- a/Week1/practice-exercises/4-reading-list.js +++ b/Week1/practice-exercises/4-reading-list.js @@ -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}".`); + } +} \ No newline at end of file diff --git a/Week1/practice-exercises/5-who-wants-a-drink.js b/Week1/practice-exercises/5-who-wants-a-drink.js index f37f02b..198d8c5 100644 --- a/Week1/practice-exercises/5-who-wants-a-drink.js +++ b/Week1/practice-exercises/5-who-wants-a-drink.js @@ -8,4 +8,13 @@ */ // There are 3 different types of drinks: -const drinkTypes = ['cola', 'lemonade', 'water']; \ No newline at end of file +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(', ')}!`); diff --git a/Week2/prep-exercises/1-traffic-light/traffic-light.js b/Week2/prep-exercises/1-traffic-light/traffic-light.js index f4a5c1a..f7a6292 100644 --- a/Week2/prep-exercises/1-traffic-light/traffic-light.js +++ b/Week2/prep-exercises/1-traffic-light/traffic-light.js @@ -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 diff --git a/Week2/prep-exercises/2-experiments/index.js b/Week2/prep-exercises/2-experiments/index.js index 7e5aa92..3e0fb39 100644 --- a/Week2/prep-exercises/2-experiments/index.js +++ b/Week2/prep-exercises/2-experiments/index.js @@ -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: // @@ -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 @@ -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