From 7bb7eb127b49382f0d97ba3644356c030b696827 Mon Sep 17 00:00:00 2001 From: Vishwak Date: Sun, 16 Jan 2022 23:13:33 +0530 Subject: [PATCH 1/2] Day_16 --- ...are-scopes-of-the-var-and-let-keywords.txt | 9 ++++++++ ...lete-a-promise-with-resolve-and-reject.txt | 10 +++++++++ .../create-a-javascript-promise.txt | 3 +++ .../create-a-module-script.txt | 8 +++++++ ...an-export-fallback-with-export-default.txt | 3 +++ ...create-strings-using-template-literals.txt | 17 +++++++++++++++ .../handle-a-fulfilled-promise-with-then.txt | 14 +++++++++++++ .../handle-a-rejected-promise-with-catch.txt | 18 ++++++++++++++++ .../import-a-default-export.txt | 4 ++++ .../mutate-an-array-declared-with-const.txt | 12 +++++++++++ .../prevent-object-mutation.txt | 16 ++++++++++++++ .../reuse-javascript-code-using-import.txt | 6 ++++++ ...-default-parameters-for-your-functions.txt | 3 +++ .../use--to-import-everything-from-a-file.txt | 5 +++++ ...s-to-write-concise-anonymous-functions.txt | 1 + ...yntax-to-define-a-constructor-function.txt | 11 ++++++++++ ...gnment-to-assign-variables-from-arrays.txt | 3 +++ ...o-assign-variables-from-nested-objects.txt | 11 ++++++++++ ...nment-to-assign-variables-from-objects.txt | 11 ++++++++++ ...ignment-to-extract-values-from-objects.txt | 11 ++++++++++ ...ss-an-object-as-a-functions-parameters.txt | 12 +++++++++++ ...t-parameter-to-reassign-array-elements.txt | 8 +++++++ .../use-export-to-share-a-code-block.txt | 9 ++++++++ ...setters-to-control-access-to-an-object.txt | 21 +++++++++++++++++++ ...est-parameter-with-function-parameters.txt | 1 + ...d-operator-to-evaluate-arrays-in-place.txt | 6 ++++++ .../write-arrow-functions-with-parameters.txt | 4 ++++ ...concise-declarative-functions-with-es6.txt | 8 +++++++ ...ations-using-object-property-shorthand.txt | 9 ++++++++ 29 files changed, 254 insertions(+) create mode 100644 js/functional_programming/compare-scopes-of-the-var-and-let-keywords.txt create mode 100644 js/functional_programming/complete-a-promise-with-resolve-and-reject.txt create mode 100644 js/functional_programming/create-a-javascript-promise.txt create mode 100644 js/functional_programming/create-a-module-script.txt create mode 100644 js/functional_programming/create-an-export-fallback-with-export-default.txt create mode 100644 js/functional_programming/create-strings-using-template-literals.txt create mode 100644 js/functional_programming/handle-a-fulfilled-promise-with-then.txt create mode 100644 js/functional_programming/handle-a-rejected-promise-with-catch.txt create mode 100644 js/functional_programming/import-a-default-export.txt create mode 100644 js/functional_programming/mutate-an-array-declared-with-const.txt create mode 100644 js/functional_programming/prevent-object-mutation.txt create mode 100644 js/functional_programming/reuse-javascript-code-using-import.txt create mode 100644 js/functional_programming/set-default-parameters-for-your-functions.txt create mode 100644 js/functional_programming/use--to-import-everything-from-a-file.txt create mode 100644 js/functional_programming/use-arrow-functions-to-write-concise-anonymous-functions.txt create mode 100644 js/functional_programming/use-class-syntax-to-define-a-constructor-function.txt create mode 100644 js/functional_programming/use-destructuring-assignment-to-assign-variables-from-arrays.txt create mode 100644 js/functional_programming/use-destructuring-assignment-to-assign-variables-from-nested-objects.txt create mode 100644 js/functional_programming/use-destructuring-assignment-to-assign-variables-from-objects.txt create mode 100644 js/functional_programming/use-destructuring-assignment-to-extract-values-from-objects.txt create mode 100644 js/functional_programming/use-destructuring-assignment-to-pass-an-object-as-a-functions-parameters.txt create mode 100644 js/functional_programming/use-destructuring-assignment-with-the-rest-parameter-to-reassign-array-elements.txt create mode 100644 js/functional_programming/use-export-to-share-a-code-block.txt create mode 100644 js/functional_programming/use-getters-and-setters-to-control-access-to-an-object.txt create mode 100644 js/functional_programming/use-the-rest-parameter-with-function-parameters.txt create mode 100644 js/functional_programming/use-the-spread-operator-to-evaluate-arrays-in-place.txt create mode 100644 js/functional_programming/write-arrow-functions-with-parameters.txt create mode 100644 js/functional_programming/write-concise-declarative-functions-with-es6.txt create mode 100644 js/functional_programming/write-concise-object-literal-declarations-using-object-property-shorthand.txt diff --git a/js/functional_programming/compare-scopes-of-the-var-and-let-keywords.txt b/js/functional_programming/compare-scopes-of-the-var-and-let-keywords.txt new file mode 100644 index 0000000..e7f3567 --- /dev/null +++ b/js/functional_programming/compare-scopes-of-the-var-and-let-keywords.txt @@ -0,0 +1,9 @@ +function checkScope() { + let i = 'function scope'; + if (true) { + let i = 'block scope'; + console.log('Block scope i is: ', i); + } + console.log('Function scope i is: ', i); + return i; +} \ No newline at end of file diff --git a/js/functional_programming/complete-a-promise-with-resolve-and-reject.txt b/js/functional_programming/complete-a-promise-with-resolve-and-reject.txt new file mode 100644 index 0000000..4c932e6 --- /dev/null +++ b/js/functional_programming/complete-a-promise-with-resolve-and-reject.txt @@ -0,0 +1,10 @@ +const makeServerRequest = new Promise((resolve, reject) => { + // responseFromServer represents a response from a server + let responseFromServer; + + if(responseFromServer) { + resolve('We got the data'); + } else { + reject('Data not received'); + } +}); \ No newline at end of file diff --git a/js/functional_programming/create-a-javascript-promise.txt b/js/functional_programming/create-a-javascript-promise.txt new file mode 100644 index 0000000..857eafa --- /dev/null +++ b/js/functional_programming/create-a-javascript-promise.txt @@ -0,0 +1,3 @@ +const makeServerRequest = new Promise((resolve, reject) => { + +}) \ No newline at end of file diff --git a/js/functional_programming/create-a-module-script.txt b/js/functional_programming/create-a-module-script.txt new file mode 100644 index 0000000..7b08481 --- /dev/null +++ b/js/functional_programming/create-a-module-script.txt @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/js/functional_programming/create-an-export-fallback-with-export-default.txt b/js/functional_programming/create-an-export-fallback-with-export-default.txt new file mode 100644 index 0000000..d2213c6 --- /dev/null +++ b/js/functional_programming/create-an-export-fallback-with-export-default.txt @@ -0,0 +1,3 @@ +export default function subtract(x, y) { + return x - y; +} \ No newline at end of file diff --git a/js/functional_programming/create-strings-using-template-literals.txt b/js/functional_programming/create-strings-using-template-literals.txt new file mode 100644 index 0000000..20bff1b --- /dev/null +++ b/js/functional_programming/create-strings-using-template-literals.txt @@ -0,0 +1,17 @@ +const result = { + success: ["max-length", "no-amd", "prefer-arrow-functions"], + failure: ["no-var", "var-on-top", "linebreak"], + skipped: ["no-extra-semi", "no-dup-keys"] +}; +function makeList(arr) { + // Only change code below this line + const failureItems = []; + arr.forEach(item => { + failureItems.push(`
  • ${item}
  • `); + }); + // Only change code above this line + + return failureItems; +} + +const failuresList = makeList(result.failure); \ No newline at end of file diff --git a/js/functional_programming/handle-a-fulfilled-promise-with-then.txt b/js/functional_programming/handle-a-fulfilled-promise-with-then.txt new file mode 100644 index 0000000..6add24c --- /dev/null +++ b/js/functional_programming/handle-a-fulfilled-promise-with-then.txt @@ -0,0 +1,14 @@ +const makeServerRequest = new Promise((resolve, reject) => { + // responseFromServer is set to true to represent a successful response from a server + let responseFromServer = true; + + if(responseFromServer) { + resolve("We got the data"); + } else { + reject("Data not received"); + } +}); + +makeServerRequest.then(result => { + console.log(result); +}); \ No newline at end of file diff --git a/js/functional_programming/handle-a-rejected-promise-with-catch.txt b/js/functional_programming/handle-a-rejected-promise-with-catch.txt new file mode 100644 index 0000000..2a1149f --- /dev/null +++ b/js/functional_programming/handle-a-rejected-promise-with-catch.txt @@ -0,0 +1,18 @@ +const makeServerRequest = new Promise((resolve, reject) => { + // responseFromServer is set to false to represent an unsuccessful response from a server + let responseFromServer = false; + + if(responseFromServer) { + resolve("We got the data"); + } else { + reject("Data not received"); + } +}); + +makeServerRequest.then(result => { + console.log(result); +}); + +makeServerRequest.catch(error => { + console.log(error); +}); \ No newline at end of file diff --git a/js/functional_programming/import-a-default-export.txt b/js/functional_programming/import-a-default-export.txt new file mode 100644 index 0000000..5b67f05 --- /dev/null +++ b/js/functional_programming/import-a-default-export.txt @@ -0,0 +1,4 @@ +import subtract from './math_functions.js'; +// Only change code above this line + +subtract(7,4); \ No newline at end of file diff --git a/js/functional_programming/mutate-an-array-declared-with-const.txt b/js/functional_programming/mutate-an-array-declared-with-const.txt new file mode 100644 index 0000000..df07663 --- /dev/null +++ b/js/functional_programming/mutate-an-array-declared-with-const.txt @@ -0,0 +1,12 @@ +const s = [5, 7, 2]; +function editInPlace() { + // Only change code below this line +s[0] = 2; +s[1] = 5; +s[2] = 7; +return s; + // Using s = [2, 5, 7] would be invalid + + // Only change code above this line +} +editInPlace(); \ No newline at end of file diff --git a/js/functional_programming/prevent-object-mutation.txt b/js/functional_programming/prevent-object-mutation.txt new file mode 100644 index 0000000..1a1e53e --- /dev/null +++ b/js/functional_programming/prevent-object-mutation.txt @@ -0,0 +1,16 @@ +function freezeObj() { + const MATH_CONSTANTS = { + PI: 3.14 + }; + // Only change code below this line +Object.freeze(MATH_CONSTANTS); + + // Only change code above this line + try { + MATH_CONSTANTS.PI = 99; + } catch(ex) { + console.log(ex); + } + return MATH_CONSTANTS.PI; +} +const PI = freezeObj(); \ No newline at end of file diff --git a/js/functional_programming/reuse-javascript-code-using-import.txt b/js/functional_programming/reuse-javascript-code-using-import.txt new file mode 100644 index 0000000..b7c97b6 --- /dev/null +++ b/js/functional_programming/reuse-javascript-code-using-import.txt @@ -0,0 +1,6 @@ + +import {uppercaseString, lowercaseString } from './string_functions.js'; +// Only change code above this line + +uppercaseString("hello"); +lowercaseString("WORLD!"); \ No newline at end of file diff --git a/js/functional_programming/set-default-parameters-for-your-functions.txt b/js/functional_programming/set-default-parameters-for-your-functions.txt new file mode 100644 index 0000000..8943f34 --- /dev/null +++ b/js/functional_programming/set-default-parameters-for-your-functions.txt @@ -0,0 +1,3 @@ +// Only change code below this line +const increment = (number, value = 1) => number + value; +// Only change code above this line \ No newline at end of file diff --git a/js/functional_programming/use--to-import-everything-from-a-file.txt b/js/functional_programming/use--to-import-everything-from-a-file.txt new file mode 100644 index 0000000..5231a36 --- /dev/null +++ b/js/functional_programming/use--to-import-everything-from-a-file.txt @@ -0,0 +1,5 @@ +import * as stringFunctions from './string_functions.js'; +// Only change code above this line + +stringFunctions.uppercaseString("hello"); +stringFunctions.lowercaseString("WORLD!"); \ No newline at end of file diff --git a/js/functional_programming/use-arrow-functions-to-write-concise-anonymous-functions.txt b/js/functional_programming/use-arrow-functions-to-write-concise-anonymous-functions.txt new file mode 100644 index 0000000..e67a364 --- /dev/null +++ b/js/functional_programming/use-arrow-functions-to-write-concise-anonymous-functions.txt @@ -0,0 +1 @@ +const magic = () => new Date(); \ No newline at end of file diff --git a/js/functional_programming/use-class-syntax-to-define-a-constructor-function.txt b/js/functional_programming/use-class-syntax-to-define-a-constructor-function.txt new file mode 100644 index 0000000..6a79ae6 --- /dev/null +++ b/js/functional_programming/use-class-syntax-to-define-a-constructor-function.txt @@ -0,0 +1,11 @@ +// Only change code below this line +class Vegetable { + name; + constructor(test) { + this.name = test; + } +} +// Only change code above this line + +const carrot = new Vegetable('carrot'); +console.log(carrot.name); // Should display 'carrot' \ No newline at end of file diff --git a/js/functional_programming/use-destructuring-assignment-to-assign-variables-from-arrays.txt b/js/functional_programming/use-destructuring-assignment-to-assign-variables-from-arrays.txt new file mode 100644 index 0000000..3081cc9 --- /dev/null +++ b/js/functional_programming/use-destructuring-assignment-to-assign-variables-from-arrays.txt @@ -0,0 +1,3 @@ +let a = 8, b = 6; +// Only change code below this line +[a,b] = [b,a]; \ No newline at end of file diff --git a/js/functional_programming/use-destructuring-assignment-to-assign-variables-from-nested-objects.txt b/js/functional_programming/use-destructuring-assignment-to-assign-variables-from-nested-objects.txt new file mode 100644 index 0000000..48061f2 --- /dev/null +++ b/js/functional_programming/use-destructuring-assignment-to-assign-variables-from-nested-objects.txt @@ -0,0 +1,11 @@ +const LOCAL_FORECAST = { + yesterday: { low: 61, high: 75 }, + today: { low: 64, high: 77 }, + tomorrow: { low: 68, high: 80 } +}; + +// Only change code below this line + +const {today: {low: lowToday, high: highToday }} = LOCAL_FORECAST; + +// Only change code above this line \ No newline at end of file diff --git a/js/functional_programming/use-destructuring-assignment-to-assign-variables-from-objects.txt b/js/functional_programming/use-destructuring-assignment-to-assign-variables-from-objects.txt new file mode 100644 index 0000000..727966d --- /dev/null +++ b/js/functional_programming/use-destructuring-assignment-to-assign-variables-from-objects.txt @@ -0,0 +1,11 @@ +const HIGH_TEMPERATURES = { + yesterday: 75, + today: 77, + tomorrow: 80 +}; + +// Only change code below this line + +const {today: highToday, tomorrow: highTomorrow } = HIGH_TEMPERATURES; + +// Only change code above this line \ No newline at end of file diff --git a/js/functional_programming/use-destructuring-assignment-to-extract-values-from-objects.txt b/js/functional_programming/use-destructuring-assignment-to-extract-values-from-objects.txt new file mode 100644 index 0000000..a4c3b5a --- /dev/null +++ b/js/functional_programming/use-destructuring-assignment-to-extract-values-from-objects.txt @@ -0,0 +1,11 @@ +const HIGH_TEMPERATURES = { + yesterday: 75, + today: 77, + tomorrow: 80 +}; + +// Only change code below this line + +const {today, tomorrow} = HIGH_TEMPERATURES; + +// Only change code above this line \ No newline at end of file diff --git a/js/functional_programming/use-destructuring-assignment-to-pass-an-object-as-a-functions-parameters.txt b/js/functional_programming/use-destructuring-assignment-to-pass-an-object-as-a-functions-parameters.txt new file mode 100644 index 0000000..7ed1326 --- /dev/null +++ b/js/functional_programming/use-destructuring-assignment-to-pass-an-object-as-a-functions-parameters.txt @@ -0,0 +1,12 @@ +const stats = { + max: 56.78, + standard_deviation: 4.34, + median: 34.54, + mode: 23.87, + min: -0.75, + average: 35.85 +}; + +// Only change code below this line +const half = ({max, min}) => (max + min) / 2.0; +// Only change code above this line \ No newline at end of file diff --git a/js/functional_programming/use-destructuring-assignment-with-the-rest-parameter-to-reassign-array-elements.txt b/js/functional_programming/use-destructuring-assignment-with-the-rest-parameter-to-reassign-array-elements.txt new file mode 100644 index 0000000..7e4d010 --- /dev/null +++ b/js/functional_programming/use-destructuring-assignment-with-the-rest-parameter-to-reassign-array-elements.txt @@ -0,0 +1,8 @@ +const source = [1,2,3,4,5,6,7,8,9,10]; +function removeFirstTwo(list) { + // Only change code below this line + const [a,b,...arr] = list; // Change this line + // Only change code above this line + return arr; +} +const arr = removeFirstTwo(source); \ No newline at end of file diff --git a/js/functional_programming/use-export-to-share-a-code-block.txt b/js/functional_programming/use-export-to-share-a-code-block.txt new file mode 100644 index 0000000..06ee046 --- /dev/null +++ b/js/functional_programming/use-export-to-share-a-code-block.txt @@ -0,0 +1,9 @@ +const uppercaseString = (string) => { + return string.toUpperCase(); +} + +const lowercaseString = (string) => { + return string.toLowerCase() +} + +export {uppercaseString, lowercaseString}; \ No newline at end of file diff --git a/js/functional_programming/use-getters-and-setters-to-control-access-to-an-object.txt b/js/functional_programming/use-getters-and-setters-to-control-access-to-an-object.txt new file mode 100644 index 0000000..01b197e --- /dev/null +++ b/js/functional_programming/use-getters-and-setters-to-control-access-to-an-object.txt @@ -0,0 +1,21 @@ +// Only change code below this line + + class Thermostat { + constructor(fahrenheit) { + this.fahrenheit = fahrenheit; + } + + get temperature() { + return (5 / 9) * (this.fahrenheit - 32); + } + + set temperature(celsius) { + this.fahrenheit = (celsius * 9.0) / 5 + 32; + } +} +// Only change code above this line + +const thermos = new Thermostat(76); // Setting in Fahrenheit scale +let temp = thermos.temperature; // 24.44 in Celsius +thermos.temperature = 26; +temp = thermos.temperature; // 26 in Celsius \ No newline at end of file diff --git a/js/functional_programming/use-the-rest-parameter-with-function-parameters.txt b/js/functional_programming/use-the-rest-parameter-with-function-parameters.txt new file mode 100644 index 0000000..f795a78 --- /dev/null +++ b/js/functional_programming/use-the-rest-parameter-with-function-parameters.txt @@ -0,0 +1 @@ +const sum = (...args) => args.reduce((a, b) => a + b, 0); \ No newline at end of file diff --git a/js/functional_programming/use-the-spread-operator-to-evaluate-arrays-in-place.txt b/js/functional_programming/use-the-spread-operator-to-evaluate-arrays-in-place.txt new file mode 100644 index 0000000..b1dff21 --- /dev/null +++ b/js/functional_programming/use-the-spread-operator-to-evaluate-arrays-in-place.txt @@ -0,0 +1,6 @@ +const arr1 = ['JAN', 'FEB', 'MAR', 'APR', 'MAY']; +let arr2; + +arr2 = [...arr1]; // Change this line + +console.log(arr2); \ No newline at end of file diff --git a/js/functional_programming/write-arrow-functions-with-parameters.txt b/js/functional_programming/write-arrow-functions-with-parameters.txt new file mode 100644 index 0000000..3e9b031 --- /dev/null +++ b/js/functional_programming/write-arrow-functions-with-parameters.txt @@ -0,0 +1,4 @@ +const myConcat = (arr1, arr2) => arr1.concat(arr2); + + +console.log(myConcat([1, 2], [3, 4, 5])); \ No newline at end of file diff --git a/js/functional_programming/write-concise-declarative-functions-with-es6.txt b/js/functional_programming/write-concise-declarative-functions-with-es6.txt new file mode 100644 index 0000000..eac6c3b --- /dev/null +++ b/js/functional_programming/write-concise-declarative-functions-with-es6.txt @@ -0,0 +1,8 @@ +// Only change code below this line +const bicycle = { + gear: 2, + setGear(newGear) { this.gear = newGear} +}; +// Only change code above this line +bicycle.setGear(3); +console.log(bicycle.gear); \ No newline at end of file diff --git a/js/functional_programming/write-concise-object-literal-declarations-using-object-property-shorthand.txt b/js/functional_programming/write-concise-object-literal-declarations-using-object-property-shorthand.txt new file mode 100644 index 0000000..0562f88 --- /dev/null +++ b/js/functional_programming/write-concise-object-literal-declarations-using-object-property-shorthand.txt @@ -0,0 +1,9 @@ +const createPerson = (name, age, gender) => { + // Only change code below this line + return { + name, + age, + gender + }; + // Only change code above this line +}; \ No newline at end of file From 6e355bad57281483729b32ca7afa50c378330c11 Mon Sep 17 00:00:00 2001 From: K-Vishwak Date: Fri, 14 Jul 2023 23:39:12 +0530 Subject: [PATCH 2/2] [1] all set --- .DS_Store | Bin 6148 -> 6148 bytes js/.DS_Store | Bin 6148 -> 6148 bytes js/functional_programming/.DS_Store | Bin 12292 -> 12292 bytes .../learn-about-functional-programming.txt | 21 ------------------ 4 files changed, 21 deletions(-) delete mode 100644 js/functional_programming/learn-about-functional-programming.txt diff --git a/.DS_Store b/.DS_Store index d03fb7943cbd76e5f52058ab047d02353541ab1c..e0911bb986d2debdf4b3a9e4b7dc7960316192a0 100644 GIT binary patch delta 125 zcmZoMXfc@J&&akhU^gQp+hiW5xr`?#uVQM5lBljWvar-qFg7%;)lsOnG&0apFflf( zt>xqpS2eWtOvtUQs;;T6o5=tMjEoSPfgei4s98V;6GIk5aY}J=PEvk;4&&w`=4O`7 I>>Pjj0gwC~H2?qr delta 110 zcmZoMXfc@J&&awlU^gQp>tr6Lxr`?!uVQNO5U;K_HZ|8#u(YhzQK+^wGSE>lF*d8M v<>U}oHMI3i$gQlZuBok?!2kw~j1ZcEA4 diff --git a/js/.DS_Store b/js/.DS_Store index 182c16a45c2656496f0717984aa408e6bb616e98..1c5b6a9ed8ab41221d95693f31ddc950bbf5db01 100644 GIT binary patch delta 43 zcmZoMXfc@J&&akhU^g=(+vJ6;2AgeIrZRG-6es5-<>%)xZeGZ$%e0xD<1aq|EuRg! delta 36 scmZoMXfc@J&&awlU^g=(>*R&32AgeIrZR5kV*ATDvB7dPJI7ys0M{4`-T(jq diff --git a/js/functional_programming/.DS_Store b/js/functional_programming/.DS_Store index f3a25fa2911c71624dd06552a3671c81249ac88e..ed0f956ad015bb14559bd5eceb1afeaef93b1723 100644 GIT binary patch delta 32 ocmZokXi1ph<KfU^hRb(Pkcjr(Bz3rPr}cY{=Qnt?-i{0LF9+`Tzg` delta 75 zcmZokXi1ph<8bU^hRb@n#-@r(EJ{3^@#`42cXy40#N?Kst#bpP`hYWby)O{mI=5 bV!Z1a7#Q^ag8>7>=66yXSvGSk{Nx7!Xha!a diff --git a/js/functional_programming/learn-about-functional-programming.txt b/js/functional_programming/learn-about-functional-programming.txt deleted file mode 100644 index 297e450..0000000 --- a/js/functional_programming/learn-about-functional-programming.txt +++ /dev/null @@ -1,21 +0,0 @@ -// Function that returns a string representing a cup of green tea -const prepareTea = () => 'greenTea'; - -/* -Given a function (representing the tea type) and number of cups needed, the -following function returns an array of strings (each representing a cup of -a specific type of tea). -*/ -const getTea = (numOfCups) => { - const teaCups = []; - - for(let cups = 1; cups <= numOfCups; cups += 1) { - const teaCup = prepareTea(); - teaCups.push(teaCup); - } - return teaCups; -}; - -// Only change code below this line -const tea4TeamFCC = getTea(40); -// Only change code above this line \ No newline at end of file