From 0ccac04935e76f10371d8e24e6ecfeb68e8d1117 Mon Sep 17 00:00:00 2001 From: iakancay Date: Fri, 20 May 2022 18:46:13 +0200 Subject: [PATCH 1/5] TODO is done --- .../prep-exercises/1-traffic-light/traffic-light-1.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Week2/prep-exercises/1-traffic-light/traffic-light-1.js b/Week2/prep-exercises/1-traffic-light/traffic-light-1.js index f1d9169..d86b2a8 100644 --- a/Week2/prep-exercises/1-traffic-light/traffic-light-1.js +++ b/Week2/prep-exercises/1-traffic-light/traffic-light-1.js @@ -11,7 +11,16 @@ let rotations = 0; while (rotations < 2) { const currentState = trafficLight.state; console.log("The traffic light is on", currentState); - + if(currentState==="green"){ + trafficLight.state="orange"; + } + else if(currentState==="orange"){ + trafficLight.state="red"; + } + else if(currentState==="red"){ + trafficLight.state="green"; + rotations++; + } // TODO // if the color is green, turn it orange // if the color is orange, turn it red From bc82942b8aa0f2bf4a199044779c62b5e508c088 Mon Sep 17 00:00:00 2001 From: iakancay Date: Fri, 20 May 2022 18:55:47 +0200 Subject: [PATCH 2/5] TODO is done --- .../prep-exercises/1-traffic-light/traffic-light-2.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Week2/prep-exercises/1-traffic-light/traffic-light-2.js b/Week2/prep-exercises/1-traffic-light/traffic-light-2.js index 8c6ba95..95e82cb 100644 --- a/Week2/prep-exercises/1-traffic-light/traffic-light-2.js +++ b/Week2/prep-exercises/1-traffic-light/traffic-light-2.js @@ -13,6 +13,16 @@ let cycle = 0; while (cycle < 2) { const currentState = trafficLight.possibleStates[trafficLight.stateIndex]; console.log("The traffic light is on", currentState); + if(currentState==="green"){ + trafficLight.stateIndex=1; + } + else if(currentState==="orange"){ + trafficLight.stateIndex=2; + }else if(currentState==="red"){ + trafficLight.stateIndex=0; + cycle++ + } + // TODO // if the color is green, turn it orange From 6b692955e409475ffe77ab42aa6866cd410cbdab Mon Sep 17 00:00:00 2001 From: iakancay Date: Fri, 20 May 2022 23:02:26 +0200 Subject: [PATCH 3/5] added for loop as alternative --- .../1-traffic-light/traffic-light-2.js | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/Week2/prep-exercises/1-traffic-light/traffic-light-2.js b/Week2/prep-exercises/1-traffic-light/traffic-light-2.js index 95e82cb..cd6689e 100644 --- a/Week2/prep-exercises/1-traffic-light/traffic-light-2.js +++ b/Week2/prep-exercises/1-traffic-light/traffic-light-2.js @@ -9,6 +9,8 @@ const trafficLight = { stateIndex: 0, }; + +/* let cycle = 0; while (cycle < 2) { const currentState = trafficLight.possibleStates[trafficLight.stateIndex]; @@ -29,15 +31,11 @@ while (cycle < 2) { // 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 - */ + +for(let cycle = 0; cycle<2; cycle++){ + for(let i=0;i<3;i++){ + const currentState = trafficLight.possibleStates[i]; + console.log("The traffic light is on", currentState); + } +} From 90f2ee017cda9ee5108618494839a8c9bdff54b5 Mon Sep 17 00:00:00 2001 From: iakancay Date: Sun, 22 May 2022 02:07:10 +0200 Subject: [PATCH 4/5] all exercises are done --- Week2/practice-exercises/1-remove-the-comma.js | 3 +++ Week2/practice-exercises/2-even-odd-reporter.js | 4 +++- Week2/practice-exercises/3-recipe-card.js | 16 ++++++++++++++++ Week2/practice-exercises/4-reading-list.js | 9 +++++++++ Week2/practice-exercises/5-who-wants-a-drink.js | 7 ++++++- Week2/prep-exercises/1-traffic-light/sezgin.js | 3 +++ 6 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 Week2/prep-exercises/1-traffic-light/sezgin.js diff --git a/Week2/practice-exercises/1-remove-the-comma.js b/Week2/practice-exercises/1-remove-the-comma.js index b71cffd..08c6360 100644 --- a/Week2/practice-exercises/1-remove-the-comma.js +++ b/Week2/practice-exercises/1-remove-the-comma.js @@ -6,6 +6,9 @@ */ let myString = 'hello,this,is,a,difficult,to,read,sentence'; +// split() method divides a String into an ordered list of substrings, puts these substrings into an array, and returns the array. +// join() method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string +myString=myString.split(",").join(" "); diff --git a/Week2/practice-exercises/2-even-odd-reporter.js b/Week2/practice-exercises/2-even-odd-reporter.js index 6edf23e..2a1bd55 100644 --- a/Week2/practice-exercises/2-even-odd-reporter.js +++ b/Week2/practice-exercises/2-even-odd-reporter.js @@ -6,4 +6,6 @@ * If it's odd, log to the console The number [PUT_NUMBER_HERE] is odd!. * If it's even, log to the console The number [PUT_NUMBER_HERE] is even!. */ - +for(i=0;i<21;i++){ + console.log(i%2==0 ? `The number ${i} is even!` : `The number ${i} is odd!`); +} diff --git a/Week2/practice-exercises/3-recipe-card.js b/Week2/practice-exercises/3-recipe-card.js index 24bcb54..8cc54f8 100644 --- a/Week2/practice-exercises/3-recipe-card.js +++ b/Week2/practice-exercises/3-recipe-card.js @@ -11,4 +11,20 @@ * Serves: 2 * Ingredients: 4 eggs, 2 strips of bacon, 1 tsp salt/pepper */ +const recipe={ + title:"Omelette", + servings:2, + ingredients:["4 eggs","2 strips of bacon","1 tsp salt/pepper"] +}; +for(let property in recipe){ + if(property=='title'){ + console.log(`Meal name:${recipe.title}`); + } + if(property=='servings'){ + console.log(`Serves:${recipe.servings}`); + } + if(property=='ingredients'){ + console.log(`Ingredients:${recipe.ingredients}`); + } +} diff --git a/Week2/practice-exercises/4-reading-list.js b/Week2/practice-exercises/4-reading-list.js index f535657..0f7222d 100644 --- a/Week2/practice-exercises/4-reading-list.js +++ b/Week2/practice-exercises/4-reading-list.js @@ -9,3 +9,12 @@ * If you haven't read it log a string like You still need to read "The Lord of the Rings" */ +let books=[ + {title:"Little Prince",author:"Antoine de Saint-Exupéry",alreadyRead:true}, + {title:"Harry Potter",author:"J. K. Rowling",alreadyRead:false}, + {title:"The Hobbit",author:"J.R.R. Tolkien",alreadyRead:false} +]; +for(let book of books){ + console.log(`"${book.title} by ${book.author}"`); + console.log(book.alreadyRead===true ? `You already read "${book.title}"`: `You still need to read "${book.title}"`); +} diff --git a/Week2/practice-exercises/5-who-wants-a-drink.js b/Week2/practice-exercises/5-who-wants-a-drink.js index f37f02b..436e328 100644 --- a/Week2/practice-exercises/5-who-wants-a-drink.js +++ b/Week2/practice-exercises/5-who-wants-a-drink.js @@ -8,4 +8,9 @@ */ // 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(i=0;i<5;i++){ + drinkTray.push(drinkTypes[i%3]); +} +console.log(`"Hey guys, I brought a ${drinkTray.join(", ")}!"`); \ No newline at end of file diff --git a/Week2/prep-exercises/1-traffic-light/sezgin.js b/Week2/prep-exercises/1-traffic-light/sezgin.js new file mode 100644 index 0000000..6f39440 --- /dev/null +++ b/Week2/prep-exercises/1-traffic-light/sezgin.js @@ -0,0 +1,3 @@ +let array=[['a',' b', 3 , 5, 11, 45],[true, 7, 8.5],[6, 8,'text',2],['a',3,8,true]]; +let arrayNew=array.map(element1=>element1.filter(element2=>typeof(element2)=='number')); +console.log(arrayNew); \ No newline at end of file From c29a6918311068b0f3913bf49a5ec39c479ba9ab Mon Sep 17 00:00:00 2001 From: iakancay Date: Thu, 26 May 2022 16:05:44 +0200 Subject: [PATCH 5/5] week3 exercises done --- Week2/practice-exercises/3-recipe-card.js | 9 +++++---- .../1-traffic-light/traffic-light.js | 14 +++++++++----- Week3/prep-exercises/2-experiments/index.js | 17 ++++++++++++++++- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/Week2/practice-exercises/3-recipe-card.js b/Week2/practice-exercises/3-recipe-card.js index 8cc54f8..954e625 100644 --- a/Week2/practice-exercises/3-recipe-card.js +++ b/Week2/practice-exercises/3-recipe-card.js @@ -17,14 +17,15 @@ const recipe={ ingredients:["4 eggs","2 strips of bacon","1 tsp salt/pepper"] }; for(let property in recipe){ - if(property=='title'){ + if(property==='title'){ console.log(`Meal name:${recipe.title}`); } - if(property=='servings'){ + if(property==='servings'){ console.log(`Serves:${recipe.servings}`); } - if(property=='ingredients'){ - console.log(`Ingredients:${recipe.ingredients}`); + if(property==='ingredients'){ + console.log(`Ingredients:${recipe.ingredients} `); + console.log(recipe.ingredients); } } diff --git a/Week3/prep-exercises/1-traffic-light/traffic-light.js b/Week3/prep-exercises/1-traffic-light/traffic-light.js index f4a5c1a..91ef146 100644 --- a/Week3/prep-exercises/1-traffic-light/traffic-light.js +++ b/Week3/prep-exercises/1-traffic-light/traffic-light.js @@ -6,12 +6,20 @@ */ function getCurrentState(trafficLight) { + return trafficLight.possibleStates[trafficLight.stateIndex]; // TODO // Should return the current state (i.e. colour) of the `trafficLight` // object passed as a parameter. } function getNextStateIndex(trafficLight) { + if(trafficLight.stateIndex===0){ + trafficLight.stateIndex=1; + }else if(trafficLight.stateIndex===1){ + trafficLight.stateIndex=2; + }else + {trafficLight.stateIndex=0;} + return trafficLight.stateIndex; // TODO // Return the index of the next state of the `trafficLight` such that: // - if the color is green, it will turn to orange @@ -19,11 +27,7 @@ function getNextStateIndex(trafficLight) { // - if the color is red, it will turn to green } -// This function loops for the number of seconds specified by the `secs` -// parameter and then returns. -// IMPORTANT: This is not the recommended way to implement 'waiting' in -// JavaScript. You will learn better ways of doing this when you learn about -// asynchronous code. + function waitSync(secs) { const start = Date.now(); while (Date.now() - start < secs * 1000) { diff --git a/Week3/prep-exercises/2-experiments/index.js b/Week3/prep-exercises/2-experiments/index.js index 7e5aa92..d3d98da 100644 --- a/Week3/prep-exercises/2-experiments/index.js +++ b/Week3/prep-exercises/2-experiments/index.js @@ -2,6 +2,13 @@ function runExperiment(sampleSize) { const valueCounts = [0, 0, 0, 0, 0, 0]; + + for(let i=0; ielement===x)]++; + + } // TODO // Write a for loop that iterates `sampleSize` times (sampleSize is a number). @@ -14,6 +21,9 @@ function runExperiment(sampleSize) { // element for value 2, etc. const results = []; + for(let valueCount of valueCounts){ + results.push(((valueCount/sampleSize)*100).toFixed(2)); + } // TODO // Write a for..of loop for the `valueCounts` array created in the previous @@ -24,12 +34,17 @@ function runExperiment(sampleSize) { // 2. Convert the computed percentage to a number string with a precision of // two decimals, e.g. '14.60'. // 3. Then push that string onto the `results` array. - + console.log(results); return results; + } function main() { const sampleSizes = [100, 1000, 1000000]; +for(let sampleSize of sampleSizes){ + runExperiment(sampleSize); + +} // TODO // Write a for..of loop that calls the `runExperiment()` function for each