From 6e443d208e845e5029c925a082e37b32e3559404 Mon Sep 17 00:00:00 2001 From: Matteo Mortelliti Date: Mon, 3 Feb 2020 16:48:44 -0500 Subject: [PATCH] Module-2-1-exercises --- .DS_Store | Bin 0 -> 6148 bytes lecture-2-types.md | 36 ++++++----- lecture-5-control-flow.md | 40 ++++++++++++ workshop/2-types/2-questions.md | 73 +++++++++++++++------- workshop/5-control-flow/5-exercise-1.js | 4 +- workshop/5-control-flow/5-exercise-2.js | 4 +- workshop/5-control-flow/5-exercise-3.js | 7 +-- workshop/5-control-flow/5-exercise-4.1.js | 4 ++ workshop/5-control-flow/5-exercise-4.2.js | 4 ++ workshop/5-control-flow/5-exercise-4.3.js | 7 +++ workshop/5-control-flow/5-exercise-4.4.js | 7 +++ workshop/5-control-flow/5-exercise-4.5.js | 8 +++ workshop/5-control-flow/5-exercise-5.js | 8 +++ workshop/5-control-flow/5-exercise-6.js | 11 +++- workshop/5-control-flow/5-exercise-7.js | 12 +++- workshop/5-control-flow/5-exercise-8.js | 12 +++- workshop/5-control-flow/5-exercise-9.js | 14 ++++- 17 files changed, 201 insertions(+), 50 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..e29871e7d32af90c46eb7a5ddcbe8b3142be2dc4 GIT binary patch literal 6148 zcmeHK%}xR_5N-jrV2m702X{W z5F#(~?P6&%sUJ4VvUX6LmgS_;sFdY?tv;RR#qM79=(KbH_%wN*zPuWCIeds(wk^)! z4UDCQKDeWBpu$^-&FW_Pgv0kaL{Qh4oq8>3o46GCbJlAqtO<0n>TbCAxcdZ2NfTCbt nYH(fxhQ5j+7q8+Hs21=`GyolosX_37(2szoff{1qPZ{_G@f}mM literal 0 HcmV?d00001 diff --git a/lecture-2-types.md b/lecture-2-types.md index 10f9774..655aeb0 100644 --- a/lecture-2-types.md +++ b/lecture-2-types.md @@ -159,14 +159,14 @@ Of course comparison operators and logical operators are usually combined in an ```js // Look at these expressions below and determine whether they evaluate to true or false -1. true || false -2. false && false -3. 1 < 2 && 2 > 1 -4. 31 < 13 || 1 < 2 && 3 > 1 -5. 400 <= 400 && 399 < 400 && (30 > 31 || 400 > 31) -6. true && false && false || false && true -7. true && false || true || false -8. true && false && false || false && true ? true && false && false || false && true : 1 < 2 && 2 > 1 +1. true || false TRUE +2. false && false FALSE +3. 1 < 2 && 2 > 1 TRUE +4. 31 < 13 || 1 < 2 && 3 > 1 TRUE +5. 400 <= 400 && 399 < 400 && (30 > 31 || 400 > 31) TRUE +6. true && false && false || false && true FALSE +7. true && false || true || false TRUE +8. true && false && false || false && true ? true && false && false || false && true : 1 < 2 && 2 > 1 TRUE ``` --- @@ -293,9 +293,9 @@ Given this data structure: let data = [0, [], [], [1,2,3, [4]]] ``` -1. How would you access the value `0`? -2. How would you access the value `3`? -3. How would you access the value `4`? +1. How would you access the value `0`? data[0] +2. How would you access the value `3`? data[3][2] +3. How would you access the value `4`? data[3][3][0] --- @@ -341,6 +341,8 @@ Group the following items together according to their type: - List the number of properties for each object. - For each property, indicate its name and its value. - For each property value, indicate the type. +-- +- NOTE: **3 things: Property, key and value (Property is KEY AND VALUE COMBINED)** ```js { label: 'corn', price: 5.3 + '$' }; @@ -374,9 +376,9 @@ let person = {name: 'Bob', age: 23}; let name = 'John'; // What is the value of the following expressions? -1. person.name -2. person['name'] -3. person[name] +1. person.name BOB +2. person['name'] BOB +3. person[name] UNDEFINED ``` @@ -391,9 +393,9 @@ let person = {name: 'bob', age: 23}; let key = 'name'; // What is the value of the following expressions: -1. person.key -2. person['key'] -3. person[key] +1. person.key UNDEFINED +2. person['key'] UNDEFINED +3. person[key] BOB ``` diff --git a/lecture-5-control-flow.md b/lecture-5-control-flow.md index c300520..fbedba8 100644 --- a/lecture-5-control-flow.md +++ b/lecture-5-control-flow.md @@ -61,12 +61,24 @@ Turn the following sentences into valid JavaScript `if` statements. _Use console // 1. If it rains, I stay home. let currentWeather = 'rainy'; +if (currentWeather === "rainy) { + console.log("stay home"); +} + // 2. If I am hungry, I eat. let hunger = true; +if (hunger){ + console.log("eat!"); +} + // 3. If it's 10pm, I go to bed. If not, I write code. let currentHour = 22; +if (currentHour >= 22) { + console.log("go to bed") +} else console.log ("write code"); + ``` ## Loops @@ -181,6 +193,14 @@ Write a program that output all of the numbers from 0 to 25 ```js // code here +for(i=0; i<=25; i++) { + console.log(i); +} + + + + + ``` --- @@ -192,6 +212,18 @@ Write a program that output all of ODD the numbers from 0 to 25 ```js // code here +for(i=0; i<26; i++) { + if (i % 2 === 1) { + console.log(i); + } +} +OR + +for(let i=0; i<26; 2++) { + console.log(i) + } + + ``` --- @@ -203,6 +235,14 @@ Write a program that output all of the numbers from 0 to 25, but replaces all mu ```js // code here +for(let i=0; i<26; i++){ + if (i % 5 === 0 && i !== 0) { + console.log("five alive!"); + } else { + console.log(i); + } +} + ``` --- \ No newline at end of file diff --git a/workshop/2-types/2-questions.md b/workshop/2-types/2-questions.md index 2b279ed..9dc96ad 100644 --- a/workshop/2-types/2-questions.md +++ b/workshop/2-types/2-questions.md @@ -4,77 +4,108 @@ 1. ( ) "I am a "Horse"" 2. ( ) "I 'prefer' ducks' -3. ( ) 'Yes, duck is nice' -4. ( ) "Ah, but I\'m vegan!" +3. (X) 'Yes, duck is nice' +4. (X) "Ah, but I\'m vegan!" 5. ( ) 'You'll eat salad then' -6. ( ) 'Yes I\'ll eat salad' +6. (X) 'Yes I\'ll eat salad' 7. ( ) "I'm happy to hear that!" 8. ( ) "\"Happy to hear " + 'that" ' + "he says!" -9. ( ) “Hello world!” +9. (X) “Hello world!” ## Question 2: Rewrite below all of the strings that are not valid, and correct them to make them valid. +1. "I am a Horse" +2. "I prefer ducks" +5. 'You\'ll eat salad then' +7. "I\'m happy to hear that!" +8. "Happy to hear " + "that " + "he says!" ## Question 3: Which of the following expressions are true? Add an 'x' between the ( ) to indicate that it's true. 1. ( ) 7 == 2 -2. ( ) 7 == 7 -3. ( ) 7 == '7' -4. ( ) 7 != 0 -5. ( ) 7 !== '7' +2. (X) 7 == 7 +3. (X) 7 == '7' +4. (X) 7 != 0 +5. (X) 7 !== '7' 6. ( ) 7 != '7' 7. ( ) 7 != 7 ## Question 4: Which of the following expressions is/are truthy? -1. ( ) !0 +1. (X) !0 2. ( ) !1 3. ( ) -1 4. ( ) !"hello!" 5. ( ) null -6. ( ) !undefined -7. ( ) !NaN +6. (X) !undefined +7. (X) !NaN ## Question 5: Which of the following are valid objects? -1. ( ) {} +1. (X) {} 2. ( ) { 'hello' } -3. ( ) { name: 'I am fruit' } -4. ( ) {'brand-name': 'Dior' } +3. (X) { name: 'I am fruit' } +4. (X) {'brand-name': 'Dior' } 5. ( ) { brand-name: 'Channel' } -6. ( ) { cool_name: 'bob' + ' Dylan', age: 25 } +6. (X) { cool_name: 'bob' + ' Dylan', age: 25 } ## Question 6: For each array, specify the number of elements and the type of each element. 1. ['cat', 'dog', 'bird'] - - number of elements: + - number of elements: 3 - type of _each_ element: - - 'cat': - - 'dog': - - 'bird': + - 'cat': string + - 'dog': string + - 'bird': string + 2. [[], 24, 'hello', true] + - number: 4 + - type + []: array + 24: number + "hello": string + "bird": string; 3. [] + - empty array 4. [['romeo', 'juliet'], false] +- number of elements: 2 + - type of _each_ element: + - '['romeo', 'juliet']': array with two strings + - false: boolean + + + 5. [{name: 'bob', age: 23}, {name: 'paul', age: 33}] + + - number: 2 objects + ## Question 7: What is the type and value for each of the following variables? 1. let name = 'bob'; - - type: - - value: + - type: string + - value: 'bob' + 2. let age = 45; + - type: number + - value: 45 3. let isMarried = false; + - type: boolean + - value: false 4. let person = { name: name, age: age, isMarried: isMarried } + - type: object + 5. let kids = [{name: 'Morty', age: 3}, {name: 'Summer', age: 7}] + - type: array of objects \ No newline at end of file diff --git a/workshop/5-control-flow/5-exercise-1.js b/workshop/5-control-flow/5-exercise-1.js index 68ae873..99bb469 100644 --- a/workshop/5-control-flow/5-exercise-1.js +++ b/workshop/5-control-flow/5-exercise-1.js @@ -9,7 +9,7 @@ // It is supposed to print to the console the numbers 1, 2, 3, 4, 5. -for (let number = 2; number < 5; number ++) { - +for (let number = 1; number < 6; number++) { + console.log(number); } \ No newline at end of file diff --git a/workshop/5-control-flow/5-exercise-2.js b/workshop/5-control-flow/5-exercise-2.js index bfd825b..00b9585 100644 --- a/workshop/5-control-flow/5-exercise-2.js +++ b/workshop/5-control-flow/5-exercise-2.js @@ -8,7 +8,7 @@ // Fix this program // It should output the squares of all numbers between 0 and 12 -for (let number = 0; number < 12; number ++) { - +for (let number = 0; number < 13; number++) { + let square = number * number; console.log('the square of ', number, ' is ', square); } \ No newline at end of file diff --git a/workshop/5-control-flow/5-exercise-3.js b/workshop/5-control-flow/5-exercise-3.js index 00c6aaf..3169995 100644 --- a/workshop/5-control-flow/5-exercise-3.js +++ b/workshop/5-control-flow/5-exercise-3.js @@ -9,10 +9,9 @@ // It should output all of the odd numbers between 1 and 25 (including 1 and 25) -for (let number = 0; number < 25; number ++) { - - if (number % 2) { +for (let number = 1; number < 26; number++) { + + if (number % 2 === 1) { console.log(number); } - console.log(number); } \ No newline at end of file diff --git a/workshop/5-control-flow/5-exercise-4.1.js b/workshop/5-control-flow/5-exercise-4.1.js index 8974d37..9ff0dfc 100644 --- a/workshop/5-control-flow/5-exercise-4.1.js +++ b/workshop/5-control-flow/5-exercise-4.1.js @@ -6,3 +6,7 @@ // Q4.1 // Write a loop that will print to the console all of the integers from 0 to 100. + +for (i = 0; i < 101; i++) { + console.log(i); +} diff --git a/workshop/5-control-flow/5-exercise-4.2.js b/workshop/5-control-flow/5-exercise-4.2.js index d2172b2..3ab09f3 100644 --- a/workshop/5-control-flow/5-exercise-4.2.js +++ b/workshop/5-control-flow/5-exercise-4.2.js @@ -6,3 +6,7 @@ // Q4.2 // Write a loop that will print to the console all of the integers from 5 to 100. + +for (i = 5; i < 101; i++) { + console.log(i); +} \ No newline at end of file diff --git a/workshop/5-control-flow/5-exercise-4.3.js b/workshop/5-control-flow/5-exercise-4.3.js index dd5af4e..e1174b0 100644 --- a/workshop/5-control-flow/5-exercise-4.3.js +++ b/workshop/5-control-flow/5-exercise-4.3.js @@ -6,3 +6,10 @@ // Q4.3 // Write a loop that will print to the console all of the EVEN integers from 1 to 100. + +for (let number = 1; number < 101; number++) { + + if (number % 2 === 0) { + console.log(number); + } +} \ No newline at end of file diff --git a/workshop/5-control-flow/5-exercise-4.4.js b/workshop/5-control-flow/5-exercise-4.4.js index 61f589b..cea7576 100644 --- a/workshop/5-control-flow/5-exercise-4.4.js +++ b/workshop/5-control-flow/5-exercise-4.4.js @@ -6,3 +6,10 @@ // Q4.4 // Write a loop that will print to the console all of the EVEN integers from 100 to 0. + +for (let number = 100; number >= 0; number--) { + + if (number % 2 === 0) { + console.log(number); + } +} \ No newline at end of file diff --git a/workshop/5-control-flow/5-exercise-4.5.js b/workshop/5-control-flow/5-exercise-4.5.js index 4505d5a..ff31d99 100644 --- a/workshop/5-control-flow/5-exercise-4.5.js +++ b/workshop/5-control-flow/5-exercise-4.5.js @@ -14,6 +14,14 @@ // 2 is even // ... +for (i = 0; i < 16; i++) { + if (i % 2 === 0) { + console.log(`${i} is even`) + } else console.log(`${i} is odd`) +} + + + // many other possibilities here. \ No newline at end of file diff --git a/workshop/5-control-flow/5-exercise-5.js b/workshop/5-control-flow/5-exercise-5.js index 5111978..253aa0b 100644 --- a/workshop/5-control-flow/5-exercise-5.js +++ b/workshop/5-control-flow/5-exercise-5.js @@ -16,3 +16,11 @@ // It's 11h. Time to train! // It's 12h. Time to train! // It's 13h. Time to eat! + +for (i = 0; i < 24; i++) { + if (i > 21 || i <= 5) { + console.log(`It's ${i}h. Time to sleep!`) + } else if (i === 7 || i === 13 || i === 18) { + console.log(`It's ${i}h. Time to eat!`) + } else console.log(`It's ${i}h. Time to train!`) +} \ No newline at end of file diff --git a/workshop/5-control-flow/5-exercise-6.js b/workshop/5-control-flow/5-exercise-6.js index b762ca5..d32ae3e 100644 --- a/workshop/5-control-flow/5-exercise-6.js +++ b/workshop/5-control-flow/5-exercise-6.js @@ -8,4 +8,13 @@ // Write a program that will output the sum of all of the multiples of four // between 0 and 5000 // -// ANSWER: the number you should see in the console is 3127500 \ No newline at end of file +// ANSWER: the number you should see in the console is 3127500 + + +let sum = 0; + +for (i = 0; i < 5001; i = 4 + i) { + sum = sum + i; +} + +console.log(sum); diff --git a/workshop/5-control-flow/5-exercise-7.js b/workshop/5-control-flow/5-exercise-7.js index db6412e..af1179d 100644 --- a/workshop/5-control-flow/5-exercise-7.js +++ b/workshop/5-control-flow/5-exercise-7.js @@ -21,4 +21,14 @@ // 8 // Fizz // Buzz -// ... \ No newline at end of file +// ... + +for (i = 1; i < 101; i++) { + if (i % 3 === 0 && i % 5 === 0) { + console.log("Fizzbuzz") + } else if (i % 3 === 0) { + console.log("Fizz") + } else if (i % 5 === 0) { + console.log("Buzz") + } else console.log(i); +} \ No newline at end of file diff --git a/workshop/5-control-flow/5-exercise-8.js b/workshop/5-control-flow/5-exercise-8.js index 488111b..a1066af 100644 --- a/workshop/5-control-flow/5-exercise-8.js +++ b/workshop/5-control-flow/5-exercise-8.js @@ -12,4 +12,14 @@ // #### // ##### // ###### -// ####### \ No newline at end of file +// ####### + + +// let hash = ""; + +let hash = ""; +for (i = 0; i < 7; i++) { + hash = hash + "#"; + console.log(hash); +} + diff --git a/workshop/5-control-flow/5-exercise-9.js b/workshop/5-control-flow/5-exercise-9.js index 3a22258..05c455c 100644 --- a/workshop/5-control-flow/5-exercise-9.js +++ b/workshop/5-control-flow/5-exercise-9.js @@ -16,4 +16,16 @@ // #_#_#_#_ // _#_#_#_# // #_#_#_#_ -// _#_#_#_# \ No newline at end of file +// _#_#_#_# + +let odd = "#_#_#_#_"; +let even = "_#_#_#_#"; + +for (i = 1; i < 9; i++) { + + if (i % 2 === 0) { + console.log(even); + + } else console.log(odd); + +} \ No newline at end of file