From df4498c9f647e597d1ac27f5040ca1c02acb561a Mon Sep 17 00:00:00 2001 From: ejsadiarin Date: Sun, 8 Jan 2023 23:16:38 +0800 Subject: [PATCH 01/31] Added hey.txt --- hey.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 hey.txt diff --git a/hey.txt b/hey.txt new file mode 100644 index 0000000..e69de29 From 0ffa42f6ed32d96208d7ac81bfd8c76142f7443e Mon Sep 17 00:00:00 2001 From: ejsadiarin Date: Mon, 9 Jan 2023 00:14:12 +0800 Subject: [PATCH 02/31] added TODO --- hey.txt | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/hey.txt b/hey.txt index e69de29..bea72b7 100644 --- a/hey.txt +++ b/hey.txt @@ -0,0 +1,9 @@ +TODO: +** All before classes starts (January 16) + +- Finish this in 7 days!!! +- Customize distro +- Finish the projects in this course (All) +- Learn how to create and consume an API in JavaScript/TypeScript +- Learn TypeScript +- Learn a little bit about React for now \ No newline at end of file From 24dd02ec4067c28a613cd69114de4ac302babb6e Mon Sep 17 00:00:00 2001 From: ejsadiarin Date: Mon, 9 Jan 2023 16:36:30 +0800 Subject: [PATCH 03/31] added Documentation Best Practices format in JavaScript --- yourPlayground.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/yourPlayground.js b/yourPlayground.js index e69de29..bff6193 100644 --- a/yourPlayground.js +++ b/yourPlayground.js @@ -0,0 +1,38 @@ +console.log('EJ SADIARIN IN THE UNVERSE!!!'); +// const food = 200; +// const pricePercent = 0.2; +// const tip = food * pricePercent; +// console.log(tip); + +// let input = prompt(); +// const typeInput = typeof(input); + +// console.log(`input type is: ${typeInput}`); + +// let inputInt = Number(input); +// console.log(typeof(inputInt)); +// console.log(inputInt); + +// // Date +// let now = new Date().toDateString(); +// console.log(`The type of date: ${typeof(now)}`); +// console.log(now); +function floorRandom() { + let random = Math.random() * 3; + let randomFloor = Math.floor(random); + return randomFloor; + // or console.log(randomFloor); + // and just call floorRandom(); +} +// floorRandom(); +console.log(floorRandom()); + +// THE REST IS IN VSCODE // + +// Documentation Best Practices: + +/* In this format +* @param: {number} num1 first number to add +* @param: {number} num2 second number to add +* @return {number} returns result sum of adding num1 and num2 +*/ \ No newline at end of file From 4699ab35594fed87a94b0922f3f129ba0f9b5648 Mon Sep 17 00:00:00 2001 From: ejsadiarin Date: Mon, 9 Jan 2023 17:25:15 +0800 Subject: [PATCH 04/31] DONE exercise on ES6 Arrow functions and calling it in another function --- functions/sum/exercise/script.js | 42 ++++++++++++++++++++++---------- functions/sum/solution/script.js | 2 +- yourPlayground.js | 28 ++++++++++----------- 3 files changed, 44 insertions(+), 28 deletions(-) diff --git a/functions/sum/exercise/script.js b/functions/sum/exercise/script.js index 69d6b48..6750a91 100644 --- a/functions/sum/exercise/script.js +++ b/functions/sum/exercise/script.js @@ -8,23 +8,39 @@ ES6 Syntax (Arrow function): const add = () => {} */ -function add(){ - //Add function here -} +// ES5 Syntax: +// function add(){ +// //Add function here +// } -function sub(){ - //Subtract function here -} +// function sub(){ +// //Subtract function here +// } -function div(){ - //Divide function here -} +// function div(){ +// //Divide function here +// } -function mul(){ - //Multiply function here -} +// function mul(){ +// //Multiply function here +// } + +// ES6 Syntax: +const add = (num1, num2) => num1 + num2; +const subtract = (num1, num2) => num1 - num2; +const div = (num1, num2) => num1 / num2; +const mul = (num1, num2) => num1 * num2; console.log('hello from the SUM exercise') /* TODO: create a function that console logs the result of any of the above operations. -*/ \ No newline at end of file +*/ +/* +* @param {num1, num2} takes in number as argument to perform a function with +* @returns {result} result after performing function +*/ +let result = (num1, num2) => { + const perform = add(num1, num2); + return perform; +} +console.log(result(1, 1)); \ No newline at end of file diff --git a/functions/sum/solution/script.js b/functions/sum/solution/script.js index 6a567a7..9f9e955 100644 --- a/functions/sum/solution/script.js +++ b/functions/sum/solution/script.js @@ -1 +1 @@ -console.log("Sum solution")) \ No newline at end of file +console.log("Sum solution"); \ No newline at end of file diff --git a/yourPlayground.js b/yourPlayground.js index bff6193..16793ef 100644 --- a/yourPlayground.js +++ b/yourPlayground.js @@ -1,22 +1,22 @@ console.log('EJ SADIARIN IN THE UNVERSE!!!'); -// const food = 200; -// const pricePercent = 0.2; -// const tip = food * pricePercent; -// console.log(tip); +const food = 200; +const pricePercent = 0.2; +const tip = food * pricePercent; +console.log(tip); -// let input = prompt(); -// const typeInput = typeof(input); +let input = "sentence"; +const typeInput = typeof(input); -// console.log(`input type is: ${typeInput}`); +console.log(`input type is: ${typeInput}`); -// let inputInt = Number(input); -// console.log(typeof(inputInt)); -// console.log(inputInt); +let inputInt = Number(input); +console.log(typeof(inputInt)); +console.log(inputInt); -// // Date -// let now = new Date().toDateString(); -// console.log(`The type of date: ${typeof(now)}`); -// console.log(now); +// Date +let now = new Date().toDateString(); +console.log(`The type of date: ${typeof(now)}`); +console.log(now); function floorRandom() { let random = Math.random() * 3; let randomFloor = Math.floor(random); From 2a99be6ec797c04f28a9da603a4c8b3346cee6a8 Mon Sep 17 00:00:00 2001 From: ejsadiarin Date: Mon, 9 Jan 2023 17:29:38 +0800 Subject: [PATCH 05/31] DONE ES6 Syntax Arrow functions exercise --- functions/sum/exercise/script.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/functions/sum/exercise/script.js b/functions/sum/exercise/script.js index 6750a91..eaa5cbe 100644 --- a/functions/sum/exercise/script.js +++ b/functions/sum/exercise/script.js @@ -43,4 +43,5 @@ let result = (num1, num2) => { const perform = add(num1, num2); return perform; } -console.log(result(1, 1)); \ No newline at end of file +console.log(result(1, 1)); +console.log("LESGOWW"); \ No newline at end of file From 03104d0bc1c9b8265c77070fdfdf571caebb6caf Mon Sep 17 00:00:00 2001 From: ejsadiarin Date: Mon, 9 Jan 2023 18:00:15 +0800 Subject: [PATCH 06/31] added Array Methods with proper documentation --- yourPlayground.js | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/yourPlayground.js b/yourPlayground.js index 16793ef..c1c9b6d 100644 --- a/yourPlayground.js +++ b/yourPlayground.js @@ -31,8 +31,48 @@ console.log(floorRandom()); // Documentation Best Practices: -/* In this format +/* In this format (for functions) * @param: {number} num1 first number to add * @param: {number} num2 second number to add * @return {number} returns result sum of adding num1 and num2 -*/ \ No newline at end of file +*/ + +// Array +let letters = ['a', 'b', 'c', 'd', 'e']; +console.log(letters); +console.log(letters[0]); + +// Array methods + +/* ********************************** +1: push --> adds new value to the end of the array +*/ + +letters.push("f"); +console.log(letters); + +/* ********************************** +2: slice --> gets value from array +*/ + +console.log(letters.slice(0, 3)); +/* +* @param {indexStart} - index of the value in the array to start from +* @param {indexEnd} - x (index of value you want to get) < indexEnd +* +* Basically: indexStart < x < indexEnd +*/ + +// Get d, e, f from letters array: +console.log(letters.slice(3, 6)); + +/* ********************************** +3: indexOf --> gets index of the value selected from the specific array +*/ +console.log(letters.indexOf('e')); + +/* ********************************** +4: length --> gets the total index length of an array +*/ +console.log(letters.length); + From 02b7211ab566d4d2321d10d4d2be8584fb8b03ab Mon Sep 17 00:00:00 2001 From: ejsadiarin Date: Tue, 10 Jan 2023 03:27:48 +0800 Subject: [PATCH 07/31] given a formula exercise done --- yourPlayground.js | 49 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/yourPlayground.js b/yourPlayground.js index c1c9b6d..f1ab5a1 100644 --- a/yourPlayground.js +++ b/yourPlayground.js @@ -42,7 +42,7 @@ let letters = ['a', 'b', 'c', 'd', 'e']; console.log(letters); console.log(letters[0]); -// Array methods +// ======= Array methods ======= // /* ********************************** 1: push --> adds new value to the end of the array @@ -76,3 +76,50 @@ console.log(letters.indexOf('e')); */ console.log(letters.length); +// ======= OBJECTS {} ======= // +// objects --> enclosed by curly braces {}, are generic --> can hold different value types or data types (string, number, etc.) +// key: value +// key is also called as property +const person = { + name: 'Ej', // '' or "" are both fine + face: 'handsome', + shirt: 'green', + age: 18 +} +// Access object property (key) +// dot notation: +console.log(person.name); +console.log(person.face); +console.log(person.shirt); +console.log(person.age); + +// bracket notation: +console.log(person['name']); +console.log(person['age']); +console.log(person['shirt']); +console.log(person['face']); +console.log(typeof(person.age)); +// add new property to person object +person.hobbies = 'programming and workout'; +person['birth month'] = 'January'; +console.log(person); + +// ======= EXTRA: don't mind ======= // + +/* Instructions: +* - Given the formula, iterate two arrays e and z using a loop +* g(z) = 1 / (1 + e**-z) +* g(z) should be in list form / object form {} that holds generic data types (just store results in another new array) +*/ +const e = [1.718, 2.718, 3.718, 4.718, 5.718, 6.718, 7.718, 8.718, 9.718, 10.718]; +const z = [2, 4, 6 ,8 ,10, 12, 14, 16, 18, 20] +const result = (e, z) => { + let resultsArr = []; + for (let i = 0; i < e.length; i++) { + let formula = 1 / (1 + Math.pow(e[i], -z[i])); + resultsArr.push(formula); + } + console.log(resultsArr); +} + +result(e, z); \ No newline at end of file From d2e3d76550b67b2030e98da06eae7f079771fe8c Mon Sep 17 00:00:00 2001 From: ejsadiarin Date: Tue, 10 Jan 2023 15:54:41 +0800 Subject: [PATCH 08/31] added arrow function with objects inside and returns a template literal --- yourPlayground.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/yourPlayground.js b/yourPlayground.js index f1ab5a1..e4fa142 100644 --- a/yourPlayground.js +++ b/yourPlayground.js @@ -122,4 +122,17 @@ const result = (e, z) => { console.log(resultsArr); } -result(e, z); \ No newline at end of file +result(e, z); + +// ======= Create a function that uses ES6 arrow function with 2 arguments, object, and template literals ======= +const createPerson = (theName, shirt) => { + const personNew = { + name: theName, + shirt: shirt, + face: 'handsome' + } + + return `${personNew.name} is the name and shirt is ${personNew.shirt}, and face is ${personNew.face}`; +} + +console.log(createPerson('edwin', 'yellow')); \ No newline at end of file From 93f35592c02f364b1622b070041d27d452e34085 Mon Sep 17 00:00:00 2001 From: ejsadiarin Date: Tue, 10 Jan 2023 16:54:42 +0800 Subject: [PATCH 09/31] added proper documentation TRUE this time --- yourPlayground.js | 59 +++++++++++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/yourPlayground.js b/yourPlayground.js index e4fa142..b3bbca2 100644 --- a/yourPlayground.js +++ b/yourPlayground.js @@ -31,11 +31,11 @@ console.log(floorRandom()); // Documentation Best Practices: -/* In this format (for functions) -* @param: {number} num1 first number to add -* @param: {number} num2 second number to add -* @return {number} returns result sum of adding num1 and num2 -*/ +/** In this format (for functions) + * @param {number} num1 first number to add + * @param {number} num2 second number to add + * @return {number} returns result sum of adding num1 and num2 + */ // Array let letters = ['a', 'b', 'c', 'd', 'e']; @@ -56,12 +56,12 @@ console.log(letters); */ console.log(letters.slice(0, 3)); -/* -* @param {indexStart} - index of the value in the array to start from -* @param {indexEnd} - x (index of value you want to get) < indexEnd -* -* Basically: indexStart < x < indexEnd -*/ +/** + * @param {indexStart} - index of the value in the array to start from + * @param {indexEnd} - x (index of value you want to get) < indexEnd + * + * Basically: indexStart < x < indexEnd + */ // Get d, e, f from letters array: console.log(letters.slice(3, 6)); @@ -106,11 +106,11 @@ console.log(person); // ======= EXTRA: don't mind ======= // -/* Instructions: -* - Given the formula, iterate two arrays e and z using a loop -* g(z) = 1 / (1 + e**-z) -* g(z) should be in list form / object form {} that holds generic data types (just store results in another new array) -*/ +/** Instructions: + * - Given the formula, iterate two arrays e and z using a loop + * g(z) = 1 / (1 + e**-z) + * g(z) should be in list form / object form {} that holds generic data types (just store results in another new array) + */ const e = [1.718, 2.718, 3.718, 4.718, 5.718, 6.718, 7.718, 8.718, 9.718, 10.718]; const z = [2, 4, 6 ,8 ,10, 12, 14, 16, 18, 20] const result = (e, z) => { @@ -124,15 +124,34 @@ const result = (e, z) => { result(e, z); -// ======= Create a function that uses ES6 arrow function with 2 arguments, object, and template literals ======= +// ======= Create a function that uses ES6 arrow function with 2 arguments, object, and template literals ======= // +// then store a function in the object, use 'this' keyword +/** createPerson function expression + * @param {theName} takes in name + * @param {shirt} takes in shirt + * @object property {networth} is a function that refers to assets subtracted by liabilities + * 'this' keyword refers to the personNew object that houses the properties + * @returns a template literal by personNew.{property key} + * when calling or returning a function in an object add parenthesis () for ex. personNew.networth() + */ const createPerson = (theName, shirt) => { const personNew = { name: theName, shirt: shirt, - face: 'handsome' + face: 'handsome', + assets: 1000000000, + liabilities: 700000, + networth: function () { + return this.assets - this.liabilities; + } } - return `${personNew.name} is the name and shirt is ${personNew.shirt}, and face is ${personNew.face}`; + return `${personNew.name} is the name and shirt is ${personNew.shirt}, and face is ${personNew.face}. The networth is ${personNew.networth()}`; } -console.log(createPerson('edwin', 'yellow')); \ No newline at end of file +console.log(createPerson('Ej', 'green')); + +// ======= LOOPS using 'of' and 'in' keyword ======= // +/** + * @param {} + */ From 4f7ff3558af2e9d0a513de20b3099344729fb587 Mon Sep 17 00:00:00 2001 From: ejsadiarin Date: Tue, 10 Jan 2023 18:09:35 +0800 Subject: [PATCH 10/31] added relevant array exercises --- yourPlayground.js | 84 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/yourPlayground.js b/yourPlayground.js index b3bbca2..7f8bab2 100644 --- a/yourPlayground.js +++ b/yourPlayground.js @@ -151,7 +151,89 @@ const createPerson = (theName, shirt) => { console.log(createPerson('Ej', 'green')); +// ======= LOOPS for loop ======= + +const fruits = ['mango', 'banana', 'strawberry', 'kiwi', 'melon']; + +// beautiful normal loop using i iteration +for (let i = 0; i < fruits.length; i++) { + console.log(fruits[i]); +} +// beautiful shorter form of a for loop using 'of' keyword +for (const fruit of fruits) { + console.log(`\nThe fruits are: ${fruit}`); +} + +// use 'in' keyword for loops and use .length property for total amount of characters +const countLetters = () => { + let phrase = 'hey how are you yes good yes yes ok wow'; + return phrase.length; +} +console.log(`The total letters are: ${countLetters()}`); + // ======= LOOPS using 'of' and 'in' keyword ======= // /** - * @param {} + * of --> when looped, returns string or characters + * in --> when looped, returns numbers instead + * both can be used when populating a new array + */ +const countLettersWithInKeyword = () => { + let phrase = 'hey hey hey hye yesyesyeysys ok wow'; + + for (letter in phrase) { + console.log(letter); + } +} +countLettersWithInKeyword(); // prints 0 1 2 3 4 5 6 7 ... + +const countLettersWithOfKeyword = () => { + let phrase = 'hey hey hey hye yesyesyeysys ok wow'; + + for (letter of phrase) { + console.log(letter); + } +} +countLettersWithOfKeyword(); // prints h e y h e y h e y h y e y e s y e s y ... + +const phraseResult = 'hey hey ywo hey hey hey good wow ok wow'; +let counter = 0; + +for (const index in phraseResult) { + console.log(parseInt(index)+ 1); + counter = index +} +console.log({ counter }); // returns result as an object { counter: '38' } + +// ======= Sum of the numbers in an array ======= // +const arrayNum = [1, 2, 3, 4, 5, 8, 12, 11, 7, 77, 70, 777]; + +/** sumOfArray function --> product goooooood + * @param {arr} is an array + * @returns the mutated result variable by adding the current result value + values in the array */ +const sumOfArray = (arr) => { + let result = 0; + for (const num of arr) { + console.log(num); + result = result + num; + } + return { result }; // returns object + // can also do return result --> to return as a number +} +console.log(sumOfArray(arrayNum)); + +const max = (array) => { + let result = 0; + for (let i = 0; i < array.length; i++) { + let currentNum = array[i]; + let nextNum = array[i + 1]; + if (currentNum < nextNum) { + result = nextNum; + } + if (currentNum > nextNum) { + result = currentNum; + } + } + return `The max number in the array is: ${result}`; +} +console.log(max(arrayNum)); \ No newline at end of file From 39f46d2f42a10ec501be512a1c4e2a34c871f6a9 Mon Sep 17 00:00:00 2001 From: ejsadiarin Date: Tue, 10 Jan 2023 18:10:09 +0800 Subject: [PATCH 11/31] added relevant array exercises --- yourPlayground.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yourPlayground.js b/yourPlayground.js index 7f8bab2..afc9253 100644 --- a/yourPlayground.js +++ b/yourPlayground.js @@ -230,7 +230,7 @@ const max = (array) => { if (currentNum < nextNum) { result = nextNum; } - if (currentNum > nextNum) { + if (currentNum > nextNum) {result result = currentNum; } } From 1ebbd1ab707ec16aa509b1455de3520198d361b0 Mon Sep 17 00:00:00 2001 From: ejsadiarin Date: Tue, 10 Jan 2023 23:11:20 +0800 Subject: [PATCH 12/31] added relevant functions intermediate difficulty && fixed documentation --- yourPlayground.js | 119 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 106 insertions(+), 13 deletions(-) diff --git a/yourPlayground.js b/yourPlayground.js index afc9253..eabbc49 100644 --- a/yourPlayground.js +++ b/yourPlayground.js @@ -31,10 +31,13 @@ console.log(floorRandom()); // Documentation Best Practices: -/** In this format (for functions) - * @param {number} num1 first number to add - * @param {number} num2 second number to add - * @return {number} returns result sum of adding num1 and num2 +/** + * In this format (for functions) + * + * @param {number} num1 - first number to add + * @param {number} num2 - second number to add + * + * @return {number} - returns result sum of adding num1 and num2 */ // Array @@ -126,12 +129,17 @@ result(e, z); // ======= Create a function that uses ES6 arrow function with 2 arguments, object, and template literals ======= // // then store a function in the object, use 'this' keyword -/** createPerson function expression - * @param {theName} takes in name - * @param {shirt} takes in shirt + +/** + * createPerson function expression + * + * @param {string} theName - takes in name + * @param {string} shirt - takes in shirt + * * @object property {networth} is a function that refers to assets subtracted by liabilities * 'this' keyword refers to the personNew object that houses the properties - * @returns a template literal by personNew.{property key} + * + * @returns {string} a template literal by personNew.{property key} * when calling or returning a function in an object add parenthesis () for ex. personNew.networth() */ const createPerson = (theName, shirt) => { @@ -207,9 +215,12 @@ console.log({ counter }); // returns result as an object { counter: '38' } // ======= Sum of the numbers in an array ======= // const arrayNum = [1, 2, 3, 4, 5, 8, 12, 11, 7, 77, 70, 777]; -/** sumOfArray function --> product goooooood - * @param {arr} is an array - * @returns the mutated result variable by adding the current result value + values in the array +/** + * sumOfArray function --> product goooooood + * + * @param {number} arr - given array + * + * @returns {number} - the sum of array by mutating the result variable - adding the current result value + values in the array */ const sumOfArray = (arr) => { let result = 0; @@ -222,6 +233,13 @@ const sumOfArray = (arr) => { } console.log(sumOfArray(arrayNum)); +/** + * Max Function + * + * @param {number} array - given array + * + * @returns {number} - the max number in an array + */ const max = (array) => { let result = 0; for (let i = 0; i < array.length; i++) { @@ -230,10 +248,85 @@ const max = (array) => { if (currentNum < nextNum) { result = nextNum; } - if (currentNum > nextNum) {result + if (currentNum > nextNum) { result = currentNum; } } return `The max number in the array is: ${result}`; } -console.log(max(arrayNum)); \ No newline at end of file +console.log(max(arrayNum)); + +// another way of doing the max function +const anotherMax = (numbers) => { + let result = numbers[0]; + for (const number of numbers) { + if (number > result) { + result = number; + } + } + + // return result; --> can be used if want to return pure number + return { result }; +} +// console.log(`anotherMax function result: ${anotherMax(arrayNum)}`); --> use if want to use return result above +console.log(anotherMax(arrayNum)); // outputs object in console + +/** + * @function letterFrequency(phrase) + * + * @param {string} phrase - any phrase or even sentence + * + * @returns {object} - counts how many certain specific letters are in a phrase string + * + * @example { + * h: 1, + * o: 14, + * e: 7, + * t: 2, + * w: 3 + * } + */ +const letterFrequency = (phrase) => { + let frequency = {}; + for (const letter of phrase) { + // check if letter exists + if (letter in frequency) { + // if yes then increment++ + frequency[letter] += 1; + } else { + // otherwise set value to 1 + frequency[letter] = 1; + } + } + + return frequency; +} +console.log(letterFrequency("Wow Ej Sadiarin you so good handsome everything code goooooood")); + +/** + * @function wordFrequency() + * + * @param {string} phrase - a phrase or sentence + * + * @method split() - seperates words, letters, etc. given an argument + * @argument characters - in split() method + * + * @returns {object} - counts the certain specific words in a given phrase or sentence + */ +const examplePhrase = 'hey hey wow good yes'; +const wordFrequency = (phrase) => { + let frequency = {}; + // use split() method to seperate words + const splitPhrase = phrase.split(' '); + for (const word of splitPhrase) { + // check if word exist + if (word in frequency) { + frequency[word] += 1; + } else { + // if new word then set value to 1 + frequency[word] = 1; + } + } + return frequency; +} +console.log(wordFrequency(examplePhrase)); \ No newline at end of file From 8a1a8b86d7192c10e42a30a1b1c7febedf4c4ac7 Mon Sep 17 00:00:00 2001 From: ejsadiarin Date: Tue, 10 Jan 2023 23:38:16 +0800 Subject: [PATCH 13/31] TODO: Array methods (map, filter, reduce) and formatted document --- yourPlayground.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/yourPlayground.js b/yourPlayground.js index eabbc49..13a5235 100644 --- a/yourPlayground.js +++ b/yourPlayground.js @@ -233,8 +233,11 @@ const sumOfArray = (arr) => { } console.log(sumOfArray(arrayNum)); + + + /** - * Max Function + * @function max(array) * * @param {number} array - given array * @@ -271,6 +274,9 @@ const anotherMax = (numbers) => { // console.log(`anotherMax function result: ${anotherMax(arrayNum)}`); --> use if want to use return result above console.log(anotherMax(arrayNum)); // outputs object in console + + + /** * @function letterFrequency(phrase) * @@ -303,8 +309,12 @@ const letterFrequency = (phrase) => { } console.log(letterFrequency("Wow Ej Sadiarin you so good handsome everything code goooooood")); + + +// REAL WORLD EXAMPLE: GOOGLE SEARCH ENGINE +// --> detecting the frequency of searched words (popular) /** - * @function wordFrequency() + * @function wordFrequency(phrase) * * @param {string} phrase - a phrase or sentence * From 673dfb49cf7c483c61259ad172590bebac0eba64 Mon Sep 17 00:00:00 2001 From: ejsadiarin Date: Wed, 11 Jan 2023 14:57:12 +0800 Subject: [PATCH 14/31] prettier format --- yourPlayground.js | 324 +++++++++++++++++++++++++--------------------- 1 file changed, 178 insertions(+), 146 deletions(-) diff --git a/yourPlayground.js b/yourPlayground.js index 13a5235..06426c2 100644 --- a/yourPlayground.js +++ b/yourPlayground.js @@ -1,21 +1,21 @@ -console.log('EJ SADIARIN IN THE UNVERSE!!!'); +console.log("EJ SADIARIN IN THE UNVERSE!!!"); const food = 200; const pricePercent = 0.2; const tip = food * pricePercent; console.log(tip); let input = "sentence"; -const typeInput = typeof(input); +const typeInput = typeof input; console.log(`input type is: ${typeInput}`); let inputInt = Number(input); -console.log(typeof(inputInt)); +console.log(typeof inputInt); console.log(inputInt); // Date let now = new Date().toDateString(); -console.log(`The type of date: ${typeof(now)}`); +console.log(`The type of date: ${typeof now}`); console.log(now); function floorRandom() { let random = Math.random() * 3; @@ -31,17 +31,17 @@ console.log(floorRandom()); // Documentation Best Practices: -/** +/** * In this format (for functions) - * + * * @param {number} num1 - first number to add * @param {number} num2 - second number to add - * + * * @return {number} - returns result sum of adding num1 and num2 */ // Array -let letters = ['a', 'b', 'c', 'd', 'e']; +let letters = ["a", "b", "c", "d", "e"]; console.log(letters); console.log(letters[0]); @@ -51,15 +51,15 @@ console.log(letters[0]); 1: push --> adds new value to the end of the array */ -letters.push("f"); +letters.push("f"); console.log(letters); /* ********************************** 2: slice --> gets value from array */ -console.log(letters.slice(0, 3)); -/** +console.log(letters.slice(0, 3)); +/** * @param {indexStart} - index of the value in the array to start from * @param {indexEnd} - x (index of value you want to get) < indexEnd * @@ -72,7 +72,7 @@ console.log(letters.slice(3, 6)); /* ********************************** 3: indexOf --> gets index of the value selected from the specific array */ -console.log(letters.indexOf('e')); +console.log(letters.indexOf("e")); /* ********************************** 4: length --> gets the total index length of an array @@ -84,11 +84,11 @@ console.log(letters.length); // key: value // key is also called as property const person = { - name: 'Ej', // '' or "" are both fine - face: 'handsome', - shirt: 'green', - age: 18 -} + name: "Ej", // '' or "" are both fine + face: "handsome", + shirt: "green", + age: 18, +}; // Access object property (key) // dot notation: console.log(person.name); @@ -97,14 +97,14 @@ console.log(person.shirt); console.log(person.age); // bracket notation: -console.log(person['name']); -console.log(person['age']); -console.log(person['shirt']); -console.log(person['face']); -console.log(typeof(person.age)); +console.log(person["name"]); +console.log(person["age"]); +console.log(person["shirt"]); +console.log(person["face"]); +console.log(typeof person.age); // add new property to person object -person.hobbies = 'programming and workout'; -person['birth month'] = 'January'; +person.hobbies = "programming and workout"; +person["birth month"] = "January"; console.log(person); // ======= EXTRA: don't mind ======= // @@ -114,163 +114,170 @@ console.log(person); * g(z) = 1 / (1 + e**-z) * g(z) should be in list form / object form {} that holds generic data types (just store results in another new array) */ -const e = [1.718, 2.718, 3.718, 4.718, 5.718, 6.718, 7.718, 8.718, 9.718, 10.718]; -const z = [2, 4, 6 ,8 ,10, 12, 14, 16, 18, 20] +const e = [ + 1.718, 2.718, 3.718, 4.718, 5.718, 6.718, 7.718, 8.718, 9.718, 10.718, +]; +const z = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]; const result = (e, z) => { - let resultsArr = []; - for (let i = 0; i < e.length; i++) { - let formula = 1 / (1 + Math.pow(e[i], -z[i])); - resultsArr.push(formula); - } - console.log(resultsArr); -} + let resultsArr = []; + for (let i = 0; i < e.length; i++) { + let formula = 1 / (1 + Math.pow(e[i], -z[i])); + resultsArr.push(formula); + } + console.log(resultsArr); +}; result(e, z); // ======= Create a function that uses ES6 arrow function with 2 arguments, object, and template literals ======= // // then store a function in the object, use 'this' keyword -/** +/** * createPerson function expression - * + * * @param {string} theName - takes in name * @param {string} shirt - takes in shirt - * + * * @object property {networth} is a function that refers to assets subtracted by liabilities * 'this' keyword refers to the personNew object that houses the properties - * + * * @returns {string} a template literal by personNew.{property key} - * when calling or returning a function in an object add parenthesis () for ex. personNew.networth() + * when calling or returning a function in an object add parenthesis () for ex. personNew.networth() */ const createPerson = (theName, shirt) => { - const personNew = { - name: theName, - shirt: shirt, - face: 'handsome', - assets: 1000000000, - liabilities: 700000, - networth: function () { - return this.assets - this.liabilities; - } - } - - return `${personNew.name} is the name and shirt is ${personNew.shirt}, and face is ${personNew.face}. The networth is ${personNew.networth()}`; -} - -console.log(createPerson('Ej', 'green')); + const personNew = { + name: theName, + shirt: shirt, + face: "handsome", + assets: 1000000000, + liabilities: 700000, + networth: function () { + return this.assets - this.liabilities; + }, + }; + + return `${personNew.name} is the name and shirt is ${ + personNew.shirt + }, and face is ${personNew.face}. The networth is ${personNew.networth()}`; +}; + +console.log(createPerson("Ej", "green")); // ======= LOOPS for loop ======= -const fruits = ['mango', 'banana', 'strawberry', 'kiwi', 'melon']; +const fruits = ["mango", "banana", "strawberry", "kiwi", "melon"]; -// beautiful normal loop using i iteration +// beautiful normal loop using i iteration for (let i = 0; i < fruits.length; i++) { - console.log(fruits[i]); + console.log(fruits[i]); } // beautiful shorter form of a for loop using 'of' keyword for (const fruit of fruits) { - console.log(`\nThe fruits are: ${fruit}`); + console.log(`\nThe fruits are: ${fruit}`); } // use 'in' keyword for loops and use .length property for total amount of characters const countLetters = () => { - let phrase = 'hey how are you yes good yes yes ok wow'; - return phrase.length; -} + let phrase = "hey how are you yes good yes yes ok wow"; + return phrase.length; +}; console.log(`The total letters are: ${countLetters()}`); // ======= LOOPS using 'of' and 'in' keyword ======= // /** * of --> when looped, returns string or characters * in --> when looped, returns numbers instead - * both can be used when populating a new array + * both can be used when populating a new array */ const countLettersWithInKeyword = () => { - let phrase = 'hey hey hey hye yesyesyeysys ok wow'; + let phrase = "hey hey hey hye yesyesyeysys ok wow"; - for (letter in phrase) { - console.log(letter); - } -} + for (letter in phrase) { + console.log(letter); + } +}; countLettersWithInKeyword(); // prints 0 1 2 3 4 5 6 7 ... const countLettersWithOfKeyword = () => { - let phrase = 'hey hey hey hye yesyesyeysys ok wow'; + let phrase = "hey hey hey hye yesyesyeysys ok wow"; - for (letter of phrase) { - console.log(letter); - } -} + for (letter of phrase) { + console.log(letter); + } +}; countLettersWithOfKeyword(); // prints h e y h e y h e y h y e y e s y e s y ... -const phraseResult = 'hey hey ywo hey hey hey good wow ok wow'; +const phraseResult = "hey hey ywo hey hey hey good wow ok wow"; let counter = 0; for (const index in phraseResult) { - console.log(parseInt(index)+ 1); - counter = index + console.log(parseInt(index) + 1); + counter = index; } console.log({ counter }); // returns result as an object { counter: '38' } // ======= Sum of the numbers in an array ======= // const arrayNum = [1, 2, 3, 4, 5, 8, 12, 11, 7, 77, 70, 777]; -/** + + + +/** * sumOfArray function --> product goooooood - * + * * @param {number} arr - given array - * - * @returns {number} - the sum of array by mutating the result variable - adding the current result value + values in the array + * + * @returns {number} - the sum of array by mutating the result variable - adding the current result value + values in the array */ const sumOfArray = (arr) => { - let result = 0; - for (const num of arr) { - console.log(num); - result = result + num; - } - return { result }; // returns object - // can also do return result --> to return as a number -} + let result = 0; + for (const num of arr) { + console.log(num); + result = result + num; + } + return { result }; // returns object + // can also do return result --> to return as a number +}; console.log(sumOfArray(arrayNum)); - +// ======= Max Function ======= // /** * @function max(array) - * - * @param {number} array - given array - * + * + * @param {number} array - given array + * * @returns {number} - the max number in an array */ const max = (array) => { - let result = 0; - for (let i = 0; i < array.length; i++) { - let currentNum = array[i]; - let nextNum = array[i + 1]; - if (currentNum < nextNum) { - result = nextNum; - } - if (currentNum > nextNum) { - result = currentNum; - } + let result = 0; + for (let i = 0; i < array.length; i++) { + let currentNum = array[i]; + let nextNum = array[i + 1]; + if (currentNum < nextNum) { + result = nextNum; } - return `The max number in the array is: ${result}`; -} + if (currentNum > nextNum) { + result = currentNum; + } + } + return `The max number in the array is: ${result}`; +}; console.log(max(arrayNum)); // another way of doing the max function const anotherMax = (numbers) => { - let result = numbers[0]; - for (const number of numbers) { - if (number > result) { - result = number; - } + let result = numbers[0]; + for (const number of numbers) { + if (number > result) { + result = number; } + } - // return result; --> can be used if want to return pure number - return { result }; -} + // return result; --> can be used if want to return pure number + return { result }; +}; // console.log(`anotherMax function result: ${anotherMax(arrayNum)}`); --> use if want to use return result above console.log(anotherMax(arrayNum)); // outputs object in console @@ -279,13 +286,13 @@ console.log(anotherMax(arrayNum)); // outputs object in console /** * @function letterFrequency(phrase) - * - * @param {string} phrase - any phrase or even sentence - * + * + * @param {string} phrase - any phrase or even sentence + * * @returns {object} - counts how many certain specific letters are in a phrase string - * + * * @example { - * h: 1, + * h: 1, * o: 14, * e: 7, * t: 2, @@ -293,21 +300,26 @@ console.log(anotherMax(arrayNum)); // outputs object in console * } */ const letterFrequency = (phrase) => { - let frequency = {}; - for (const letter of phrase) { - // check if letter exists - if (letter in frequency) { - // if yes then increment++ - frequency[letter] += 1; - } else { - // otherwise set value to 1 - frequency[letter] = 1; - } + let frequency = {}; + for (const letter of phrase) { + // check if letter exists + if (letter in frequency) { + // if yes then increment++ + frequency[letter] += 1; + } else { + // otherwise set value to 1 + frequency[letter] = 1; } + } + + return frequency; +}; +console.log( + letterFrequency( + "Wow Ej Sadiarin you so good handsome everything code goooooood" + ) +); - return frequency; -} -console.log(letterFrequency("Wow Ej Sadiarin you so good handsome everything code goooooood")); @@ -315,28 +327,48 @@ console.log(letterFrequency("Wow Ej Sadiarin you so good handsome everything cod // --> detecting the frequency of searched words (popular) /** * @function wordFrequency(phrase) - * + * * @param {string} phrase - a phrase or sentence - * + * * @method split() - seperates words, letters, etc. given an argument * @argument characters - in split() method - * + * * @returns {object} - counts the certain specific words in a given phrase or sentence */ -const examplePhrase = 'hey hey wow good yes'; +const examplePhrase = "hey hey wow good yes"; const wordFrequency = (phrase) => { - let frequency = {}; - // use split() method to seperate words - const splitPhrase = phrase.split(' '); - for (const word of splitPhrase) { - // check if word exist - if (word in frequency) { - frequency[word] += 1; - } else { - // if new word then set value to 1 - frequency[word] = 1; - } + let frequency = {}; + // use split() method to seperate words + const splitPhrase = phrase.split(" "); + for (const word of splitPhrase) { + // check if word exist + if (word in frequency) { + frequency[word] += 1; + } else { + // if new word then set value to 1 + frequency[word] = 1; } - return frequency; -} -console.log(wordFrequency(examplePhrase)); \ No newline at end of file + } + return frequency; +}; +console.log(wordFrequency(examplePhrase)); + + + +// ======= MAP, FILTER, REDUCE Array Methods ======= // + +// MAP + +// FILTER +const filter = (numbers, greaterThan) => { + let result = []; + for (const number of numbers) { + if (number > greaterThan) { + result.push(number); + } + } + return result; +}; +console.log(filter([1, 2, 3, 7, 4, 5], 3)); + +// REDUCE From ade2f8dbe6e10284bc3684385d2d4fc4bc4f24a9 Mon Sep 17 00:00:00 2001 From: ejsadiarin Date: Wed, 11 Jan 2023 21:12:51 +0800 Subject: [PATCH 15/31] findMax exercise DONE --- exercises/findMax.js | 11 +++++-- yourPlayground.js | 78 +++++++++++++++++++++++++++++++++++++------- 2 files changed, 76 insertions(+), 13 deletions(-) diff --git a/exercises/findMax.js b/exercises/findMax.js index 78c0cbc..f5e90a4 100644 --- a/exercises/findMax.js +++ b/exercises/findMax.js @@ -2,9 +2,16 @@ // 👉 // Write a function that takes in an array of numbers and returns the largest number +const numbersArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 7, 18]; function findMax (array) { - + let result = 0; + for (const num of array) { + if (num > result) { + result = num; + } + } + return result; } - +console.log(findMax(numbersArr)); //Topics: loops, arrays, conditions, \ No newline at end of file diff --git a/yourPlayground.js b/yourPlayground.js index 06426c2..4efa21c 100644 --- a/yourPlayground.js +++ b/yourPlayground.js @@ -219,9 +219,6 @@ console.log({ counter }); // returns result as an object { counter: '38' } // ======= Sum of the numbers in an array ======= // const arrayNum = [1, 2, 3, 4, 5, 8, 12, 11, 7, 77, 70, 777]; - - - /** * sumOfArray function --> product goooooood * @@ -314,11 +311,7 @@ const letterFrequency = (phrase) => { return frequency; }; -console.log( - letterFrequency( - "Wow Ej Sadiarin you so good handsome everything code goooooood" - ) -); +console.log(letterFrequency("Wow Ej Sadiarin you so good handsome everything code goooooood")); @@ -354,12 +347,39 @@ const wordFrequency = (phrase) => { console.log(wordFrequency(examplePhrase)); +/* ======= HIGHER ORDER FUNCTIONS ======= + --> functions that takes in another function as argument and/or returns a function +*/ + +// MAP, FILTER, REDUCE Array Methods +const nums = [1, 2, 3, 4, 7, 9, 5, 8]; + +/** + * MAP @method map() + * - selects all values and + * - changes the value of the whole given array + * - works like loops but returns new array + * + * @param callbackfn + * + * @returns {array} - new array + */ +const mapFunc = (numArray) => { + return numArray.map(number => number * 2); +} +console.log(mapFunc(arrayNum)); + + +// MAP simple ver +console.log(nums.map(num => num * 2)); + -// ======= MAP, FILTER, REDUCE Array Methods ======= // -// MAP +// FILTER - filters out values from an array given a condition +// - works like loops but returns new array +console.log(nums.filter(num => num > 5 || num === 5)); -// FILTER +// works the same as this: const filter = (numbers, greaterThan) => { let result = []; for (const number of numbers) { @@ -372,3 +392,39 @@ const filter = (numbers, greaterThan) => { console.log(filter([1, 2, 3, 7, 4, 5], 3)); // REDUCE +/** + * used in SUM + * @returns one number + */ + +// +console.log(nums.reduce(num => num * 2)); +// given an array called nums, use reduce() array method to return the product of the whole array +console.log(nums.reduce((a, b) => a * b)); + +// return sum of array using sum() function +const sum = (a, b) => { + return a + b; +} +const resultNumsArray = nums.reduce(sum); +console.log(resultNumsArray); +console.log(sumOfArray(nums)); + + +const exampleObj = [ + {name: "personOne", money: 1000000}, + {name: "personTwo", money: 7080328}, + {name: "personThree", money: 891302777} +] +let moneyp = exampleObj[1]; +console.log(moneyp); + +// given the array of objects above, get the sum of the money {number} +const moneySum = (objectPerson) => { + let result = objectPerson.reduce(person => person.money, ); + // for (const person of objectPerson) { + // result = result + person.money; + // } + return result; +} +console.log(moneySum(exampleObj)); \ No newline at end of file From c5628c181ee1070fb967ea504951b7990761b3e2 Mon Sep 17 00:00:00 2001 From: ejsadiarin Date: Wed, 11 Jan 2023 21:43:02 +0800 Subject: [PATCH 16/31] TODO: optimize moneySum using reduce() --- yourPlayground.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/yourPlayground.js b/yourPlayground.js index 4efa21c..5183c64 100644 --- a/yourPlayground.js +++ b/yourPlayground.js @@ -393,8 +393,9 @@ console.log(filter([1, 2, 3, 7, 4, 5], 3)); // REDUCE /** + * reduces array to a single value * used in SUM - * @returns one number + * @returns one number / single value */ // @@ -419,12 +420,15 @@ const exampleObj = [ let moneyp = exampleObj[1]; console.log(moneyp); -// given the array of objects above, get the sum of the money {number} +// ======= given the array of objects above, get the sum of the money {number} ======= // const moneySum = (objectPerson) => { - let result = objectPerson.reduce(person => person.money, ); - // for (const person of objectPerson) { - // result = result + person.money; - // } + let result = 0; + for (const person of objectPerson) { + result = result + person.money; + } return result; } -console.log(moneySum(exampleObj)); \ No newline at end of file +console.log(moneySum(exampleObj)); + +// optimize above solution (use reduce()) + From c0b6c287ed155893517b989beb41de91569eea4a Mon Sep 17 00:00:00 2001 From: ejsadiarin Date: Wed, 11 Jan 2023 22:43:56 +0800 Subject: [PATCH 17/31] TODO: reduce optimize and API --- api/superHero/script.js | 86 +++++++++++++++++++------------------ exercises/findMax.js | 1 + exercises/gradeconverter.js | 11 +++++ yourPlayground.js | 12 +++++- 4 files changed, 67 insertions(+), 43 deletions(-) create mode 100644 exercises/gradeconverter.js diff --git a/api/superHero/script.js b/api/superHero/script.js index 357f320..9f0c723 100644 --- a/api/superHero/script.js +++ b/api/superHero/script.js @@ -1,15 +1,15 @@ // https://superheroapi.com/api/access-token/character-id -const SUPERHERO_TOKEN = '10223569763528853' -const BASE_URL = `https://superheroapi.com/api.php/${SUPERHERO_TOKEN}` +const SUPERHERO_TOKEN = "10223569763528853"; +const BASE_URL = `https://superheroapi.com/api.php/${SUPERHERO_TOKEN}`; -const newHeroButton = document.getElementById('newHeroButton') +const newHeroButton = document.getElementById("newHeroButton"); -const heroImageDiv = document.getElementById('heroImage') +const heroImageDiv = document.getElementById("heroImage"); -const searchButton = document.getElementById('searchButton') +const searchButton = document.getElementById("searchButton"); -const searchInput = document.getElementById('searchInput') +const searchInput = document.getElementById("searchInput"); const getSuperHero = (id, name) => { // name 👉 base_url/search/batman @@ -17,52 +17,56 @@ const getSuperHero = (id, name) => { // id: 👉 base_url/id // json.image.url fetch(`${BASE_URL}/${id}`) - .then(response => response.json()) - .then(json => { - console.log(json.powerstats) - const superHero = json - showHeroInfo(superHero) - }) -} + .then((response) => response.json()) + .then((json) => { + console.log(json.powerstats); + const superHero = json; + showHeroInfo(superHero); + }); +}; const statToEmoji = { - intelligence: '🧠', - strength: '💪', - speed: '⚡', - durability: '🏋️‍♂️', - power: '📊', - combat: '⚔️', -} + intelligence: "🧠", + strength: "💪", + speed: "⚡", + durability: "🏋️‍♂️", + power: "📊", + combat: "⚔️", +}; const showHeroInfo = (character) => { - const name = `

${character.name}

` + const name = `

${character.name}

`; + + const img = ``; - const img = `` - - const stats = Object.keys(character.powerstats).map(stat => { - return `

${statToEmoji[stat]} ${stat.toUpperCase()}: ${character.powerstats[stat]}

` - }).join('') - - heroImageDiv.innerHTML = `${name}${img}${stats}` -} + const stats = Object.keys(character.powerstats) + .map((stat) => { + return `

${statToEmoji[stat]} ${stat.toUpperCase()}: ${ + character.powerstats[stat] + }

`; + }) + .join(""); + + heroImageDiv.innerHTML = `${name}${img}${stats}`; +}; //

💪 Strength: ${json.powerstats.strength}

🧠 Intelligence: ${json.powerstats.intelligence}

🧠 Combat: ${json.powerstats.intelligence}

🧠 Speed: ${json.powerstats.intelligence}

🧠 Durability: ${json.powerstats.intelligence}

const getSearchSuperHero = (name) => { - console.log(searchInput.value) + console.log(searchInput.value); fetch(`${BASE_URL}/search/${name}`) - .then(response => response.json()) - .then(json => { - const hero = json.results[0] - showHeroInfo(hero) - }) -} + .then((response) => response.json()) + .then((json) => { + const hero = json.results[0]; + showHeroInfo(hero); + }); +}; const randomHero = () => { - const numberOfHeroes = 731 - return Math.floor(Math.random() * numberOfHeroes) + 1 -} + const numberOfHeroes = 731; + return Math.floor(Math.random() * numberOfHeroes) + 1; +}; -newHeroButton.onclick = () => getSuperHero(randomHero()) +newHeroButton.onclick = () => getSuperHero(randomHero()); -searchButton.onclick = () => getSearchSuperHero(searchInput.value) +searchButton.onclick = () => getSearchSuperHero(searchInput.value); diff --git a/exercises/findMax.js b/exercises/findMax.js index f5e90a4..c0249c7 100644 --- a/exercises/findMax.js +++ b/exercises/findMax.js @@ -2,6 +2,7 @@ // 👉 // Write a function that takes in an array of numbers and returns the largest number +// DONE const numbersArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 7, 18]; function findMax (array) { diff --git a/exercises/gradeconverter.js b/exercises/gradeconverter.js new file mode 100644 index 0000000..4d7aa68 --- /dev/null +++ b/exercises/gradeconverter.js @@ -0,0 +1,11 @@ +/** + * A - greater than 90 + * B - 80 to 89 + * C - 79 to 70 + * D - 69 to 65 + * F - 65 or lower + * + */ +const gradeConverter = () => { + +} \ No newline at end of file diff --git a/yourPlayground.js b/yourPlayground.js index 5183c64..a71cd42 100644 --- a/yourPlayground.js +++ b/yourPlayground.js @@ -399,7 +399,7 @@ console.log(filter([1, 2, 3, 7, 4, 5], 3)); */ // -console.log(nums.reduce(num => num * 2)); +console.log(nums.reduce(num => num * 2, 9)); // given an array called nums, use reduce() array method to return the product of the whole array console.log(nums.reduce((a, b) => a * b)); @@ -430,5 +430,13 @@ const moneySum = (objectPerson) => { } console.log(moneySum(exampleObj)); -// optimize above solution (use reduce()) +// TODO: optimize above solution (use reduce()) +const reducef = nums.reduce(num => num.reduce()) +const moneySumReduce = (arr) => { + let result = 0; + for (const num in arr) { + + } +} + From 1429005c418269f38980dd688a50dd1e3db57450 Mon Sep 17 00:00:00 2001 From: ejsadiarin Date: Wed, 11 Jan 2023 22:49:15 +0800 Subject: [PATCH 18/31] TODO grade converter --- exercises/gradeconverter.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/exercises/gradeconverter.js b/exercises/gradeconverter.js index 4d7aa68..8211e43 100644 --- a/exercises/gradeconverter.js +++ b/exercises/gradeconverter.js @@ -6,6 +6,13 @@ * F - 65 or lower * */ -const gradeConverter = () => { - +const gradeConverter = (grade) => { + if (grade > 90) { + + } + if (89 <= grade && grade > 80) + + else { + + } } \ No newline at end of file From 218910122393380718b688079b183b23bcb5bcce Mon Sep 17 00:00:00 2001 From: ejsadiarin Date: Thu, 12 Jan 2023 16:22:58 +0800 Subject: [PATCH 19/31] added extensions in vscode --- yourPlayground.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/yourPlayground.js b/yourPlayground.js index a71cd42..53a1e09 100644 --- a/yourPlayground.js +++ b/yourPlayground.js @@ -431,12 +431,22 @@ const moneySum = (objectPerson) => { console.log(moneySum(exampleObj)); // TODO: optimize above solution (use reduce()) -const reducef = nums.reduce(num => num.reduce()) -const moneySumReduce = (arr) => { - let result = 0; - for (const num in arr) { +// const reducef = nums.reduce(num => num.reduce()) +// const moneySumReduce = (arr) => { +// let result = 0; +// for (const num in arr) { +// } +// } +const func = () => { + let result = 0; + for (const num of nums) { + result = result + num; } } +const functionA = (arr) => { + arr.reduce(fnp => fnp * 2); + return result +} From 0ae789b099fee10aecbee4dbcade41babf08b23b Mon Sep 17 00:00:00 2001 From: ejsadiarin Date: Thu, 12 Jan 2023 18:25:45 +0800 Subject: [PATCH 20/31] added simple API response --- jsconfig.json | 14 ++++++++++++++ yourPlayground.js | 34 +++++++++++++++++++++++----------- 2 files changed, 37 insertions(+), 11 deletions(-) create mode 100644 jsconfig.json diff --git a/jsconfig.json b/jsconfig.json new file mode 100644 index 0000000..458cf9b --- /dev/null +++ b/jsconfig.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + "module": "ESNext", + "moduleResolution": "Node", + "target": "ES2020", + "jsx": "react", + "strictNullChecks": true, + "strictFunctionTypes": true + }, + "exclude": [ + "node_modules", + "**/node_modules/*" + ] +} \ No newline at end of file diff --git a/yourPlayground.js b/yourPlayground.js index 53a1e09..d8246cf 100644 --- a/yourPlayground.js +++ b/yourPlayground.js @@ -438,15 +438,27 @@ console.log(moneySum(exampleObj)); // } // } -const func = () => { - let result = 0; - for (const num of nums) { - result = result + num; - } -} - -const functionA = (arr) => { - arr.reduce(fnp => fnp * 2); - return result -} +// const func = () => { +// let result = 0; +// for (const num of nums) { +// result = result + num; +// } +// } +// ======= API ======= // +/** + * + */ +const dogImage = document.getElementById('dogImage'); +const dogButton = document.getElementById('dogButton'); + +// example: 'run 2nd' will run last since it is async +console.log('run 1st') +fetch('https://dog.ceo/api/breeds/image/random') + .then(response => response.json) + .then(json => { + console.log(json.message) + dogImage.innerHTML = `` + }) + + console.log('run 3rd'); \ No newline at end of file From 14478bb565b9d7f86425a583e8e6c42e7fe2ff11 Mon Sep 17 00:00:00 2001 From: ejsadiarin Date: Fri, 13 Jan 2023 18:25:28 +0800 Subject: [PATCH 21/31] DONE added random number exercise with minimum and maximum value argument --- exercises/randomArgs.js | 15 +++++++++++++++ yourPlayground.js | 16 +++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 exercises/randomArgs.js diff --git a/exercises/randomArgs.js b/exercises/randomArgs.js new file mode 100644 index 0000000..ef0b79b --- /dev/null +++ b/exercises/randomArgs.js @@ -0,0 +1,15 @@ +// Get a random number and set minimum value to 1 and max to 734 + +const minMax = (number) => { + let result = 0; + // reroll + if (number > 735 && number < 1) { + minMax(number); + } else { + result = number; + return result; + } + +} +// @argument {number} 734 - defines the maximum value +console.log(minMax(Math.floor(Math.random() * 734))); \ No newline at end of file diff --git a/yourPlayground.js b/yourPlayground.js index d8246cf..b0cff59 100644 --- a/yourPlayground.js +++ b/yourPlayground.js @@ -461,4 +461,18 @@ fetch('https://dog.ceo/api/breeds/image/random') dogImage.innerHTML = `` }) - console.log('run 3rd'); \ No newline at end of file + console.log('run 3rd'); + +// Get a random number and set minimum value to 1 and max to 734 + const minMax = (number) => { + + // reroll + if (number > 735 && number < 1) { + minMax(number); + } else { + return; + } + + } + +console.log(minMax(Math.random())); \ No newline at end of file From c1e67119f6fb61f2c024420d7885b30f4f66c551 Mon Sep 17 00:00:00 2001 From: ejsadiarin Date: Fri, 13 Jan 2023 18:28:13 +0800 Subject: [PATCH 22/31] DONE added Math.random() argument exercise with min and max value restriction --- exercises/randomArgs.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/randomArgs.js b/exercises/randomArgs.js index ef0b79b..1f30a2c 100644 --- a/exercises/randomArgs.js +++ b/exercises/randomArgs.js @@ -12,4 +12,4 @@ const minMax = (number) => { } // @argument {number} 734 - defines the maximum value -console.log(minMax(Math.floor(Math.random() * 734))); \ No newline at end of file +console.log(minMax(Math.floor(Math.random() * 735))); \ No newline at end of file From a351f0fa98cdfccac50cde28bb6c7558fda1e2f9 Mon Sep 17 00:00:00 2001 From: ejsadiarin Date: Fri, 13 Jan 2023 21:52:44 +0800 Subject: [PATCH 23/31] added other solution if data (number) is coming from an API --- exercises/randomArgs.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/exercises/randomArgs.js b/exercises/randomArgs.js index 1f30a2c..21fba34 100644 --- a/exercises/randomArgs.js +++ b/exercises/randomArgs.js @@ -1,5 +1,5 @@ // Get a random number and set minimum value to 1 and max to 734 - +// Own solution const minMax = (number) => { let result = 0; // reroll @@ -12,4 +12,13 @@ const minMax = (number) => { } // @argument {number} 734 - defines the maximum value -console.log(minMax(Math.floor(Math.random() * 735))); \ No newline at end of file +console.log(minMax(Math.floor(Math.random() * 735))); + +// =========================================================================== // +// Other solution (superhero) +const otherMinMax = () => { + const numberOfHeroes = 731; + return Math.floor(Math.random() * numberOfHeroes) + 1; +} + +console.log(otherMinMax); // doesn't print any because the data is from an API (see /api/superHero/script.js) \ No newline at end of file From ba777fc62bd00749ced1026f906968ecc810af79 Mon Sep 17 00:00:00 2001 From: ejsadiarin Date: Fri, 13 Jan 2023 23:21:54 +0800 Subject: [PATCH 24/31] Consuming an API -- learning --- yourPlayground.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/yourPlayground.js b/yourPlayground.js index b0cff59..79e19d9 100644 --- a/yourPlayground.js +++ b/yourPlayground.js @@ -464,15 +464,16 @@ fetch('https://dog.ceo/api/breeds/image/random') console.log('run 3rd'); // Get a random number and set minimum value to 1 and max to 734 - const minMax = (number) => { - - // reroll - if (number > 735 && number < 1) { +const minMax = (number) => { + let result = 0; + // reroll + if (number > 735 && number < 1) { minMax(number); - } else { - return; - } - + } else { + result = number; + return result; } -console.log(minMax(Math.random())); \ No newline at end of file +} +// @argument {number} 734 - defines the maximum value +console.log(minMax(Math.floor(Math.random() * 735))); \ No newline at end of file From 01be95638cd614c0d40adeafe36f12ee4eb7b512 Mon Sep 17 00:00:00 2001 From: ejsadiarin Date: Sat, 14 Jan 2023 21:30:36 +0800 Subject: [PATCH 25/31] added randomNumber generator function --- yourPlayground.js | 940 ++++++++++++++++++++++++---------------------- 1 file changed, 483 insertions(+), 457 deletions(-) diff --git a/yourPlayground.js b/yourPlayground.js index 79e19d9..1885759 100644 --- a/yourPlayground.js +++ b/yourPlayground.js @@ -1,479 +1,505 @@ -console.log("EJ SADIARIN IN THE UNVERSE!!!"); -const food = 200; -const pricePercent = 0.2; -const tip = food * pricePercent; -console.log(tip); - -let input = "sentence"; -const typeInput = typeof input; - -console.log(`input type is: ${typeInput}`); - -let inputInt = Number(input); -console.log(typeof inputInt); -console.log(inputInt); - -// Date -let now = new Date().toDateString(); -console.log(`The type of date: ${typeof now}`); -console.log(now); -function floorRandom() { - let random = Math.random() * 3; - let randomFloor = Math.floor(random); - return randomFloor; - // or console.log(randomFloor); - // and just call floorRandom(); -} -// floorRandom(); -console.log(floorRandom()); - -// THE REST IS IN VSCODE // - -// Documentation Best Practices: - -/** - * In this format (for functions) - * - * @param {number} num1 - first number to add - * @param {number} num2 - second number to add - * - * @return {number} - returns result sum of adding num1 and num2 - */ - -// Array -let letters = ["a", "b", "c", "d", "e"]; -console.log(letters); -console.log(letters[0]); - -// ======= Array methods ======= // - -/* ********************************** -1: push --> adds new value to the end of the array -*/ - -letters.push("f"); -console.log(letters); - -/* ********************************** -2: slice --> gets value from array -*/ - -console.log(letters.slice(0, 3)); -/** - * @param {indexStart} - index of the value in the array to start from - * @param {indexEnd} - x (index of value you want to get) < indexEnd - * - * Basically: indexStart < x < indexEnd - */ - -// Get d, e, f from letters array: -console.log(letters.slice(3, 6)); - -/* ********************************** -3: indexOf --> gets index of the value selected from the specific array -*/ -console.log(letters.indexOf("e")); - -/* ********************************** -4: length --> gets the total index length of an array -*/ -console.log(letters.length); - -// ======= OBJECTS {} ======= // -// objects --> enclosed by curly braces {}, are generic --> can hold different value types or data types (string, number, etc.) -// key: value -// key is also called as property -const person = { - name: "Ej", // '' or "" are both fine - face: "handsome", - shirt: "green", - age: 18, -}; -// Access object property (key) -// dot notation: -console.log(person.name); -console.log(person.face); -console.log(person.shirt); -console.log(person.age); - -// bracket notation: -console.log(person["name"]); -console.log(person["age"]); -console.log(person["shirt"]); -console.log(person["face"]); -console.log(typeof person.age); -// add new property to person object -person.hobbies = "programming and workout"; -person["birth month"] = "January"; -console.log(person); - -// ======= EXTRA: don't mind ======= // - -/** Instructions: - * - Given the formula, iterate two arrays e and z using a loop - * g(z) = 1 / (1 + e**-z) - * g(z) should be in list form / object form {} that holds generic data types (just store results in another new array) - */ -const e = [ - 1.718, 2.718, 3.718, 4.718, 5.718, 6.718, 7.718, 8.718, 9.718, 10.718, -]; -const z = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]; -const result = (e, z) => { - let resultsArr = []; - for (let i = 0; i < e.length; i++) { - let formula = 1 / (1 + Math.pow(e[i], -z[i])); - resultsArr.push(formula); - } - console.log(resultsArr); -}; - -result(e, z); - -// ======= Create a function that uses ES6 arrow function with 2 arguments, object, and template literals ======= // -// then store a function in the object, use 'this' keyword - -/** - * createPerson function expression - * - * @param {string} theName - takes in name - * @param {string} shirt - takes in shirt - * - * @object property {networth} is a function that refers to assets subtracted by liabilities - * 'this' keyword refers to the personNew object that houses the properties - * - * @returns {string} a template literal by personNew.{property key} - * when calling or returning a function in an object add parenthesis () for ex. personNew.networth() - */ -const createPerson = (theName, shirt) => { - const personNew = { - name: theName, - shirt: shirt, - face: "handsome", - assets: 1000000000, - liabilities: 700000, - networth: function () { - return this.assets - this.liabilities; - }, - }; - - return `${personNew.name} is the name and shirt is ${ - personNew.shirt - }, and face is ${personNew.face}. The networth is ${personNew.networth()}`; -}; - -console.log(createPerson("Ej", "green")); - -// ======= LOOPS for loop ======= - -const fruits = ["mango", "banana", "strawberry", "kiwi", "melon"]; - -// beautiful normal loop using i iteration -for (let i = 0; i < fruits.length; i++) { - console.log(fruits[i]); -} -// beautiful shorter form of a for loop using 'of' keyword -for (const fruit of fruits) { - console.log(`\nThe fruits are: ${fruit}`); -} +// console.log("EJ SADIARIN IN THE UNVERSE!!!"); +// const food = 200; +// const pricePercent = 0.2; +// const tip = food * pricePercent; +// console.log(tip); + +// let input = "sentence"; +// const typeInput = typeof input; + +// console.log(`input type is: ${typeInput}`); + +// let inputInt = Number(input); +// console.log(typeof inputInt); +// console.log(inputInt); + +// // Date +// let now = new Date().toDateString(); +// console.log(`The type of date: ${typeof now}`); +// console.log(now); +// function floorRandom() { +// let random = Math.random() * 3; +// let randomFloor = Math.floor(random); +// return randomFloor; +// // or console.log(randomFloor); +// // and just call floorRandom(); +// } +// // floorRandom(); +// console.log(floorRandom()); + +// // THE REST IS IN VSCODE // + +// // Documentation Best Practices: + +// /** +// * In this format (for functions) +// * +// * @param {number} num1 - first number to add +// * @param {number} num2 - second number to add +// * +// * @return {number} - returns result sum of adding num1 and num2 +// */ + +// // Array +// let letters = ["a", "b", "c", "d", "e"]; +// console.log(letters); +// console.log(letters[0]); + +// // ======= Array methods ======= // + +// /* ********************************** +// 1: push --> adds new value to the end of the array +// */ + +// letters.push("f"); +// console.log(letters); + +// /* ********************************** +// 2: slice --> gets value from array +// */ + +// console.log(letters.slice(0, 3)); +// /** +// * @param {indexStart} - index of the value in the array to start from +// * @param {indexEnd} - x (index of value you want to get) < indexEnd +// * +// * Basically: indexStart < x < indexEnd +// */ + +// // Get d, e, f from letters array: +// console.log(letters.slice(3, 6)); + +// /* ********************************** +// 3: indexOf --> gets index of the value selected from the specific array +// */ +// console.log(letters.indexOf("e")); + +// /* ********************************** +// 4: length --> gets the total index length of an array +// */ +// console.log(letters.length); + +// // ======= OBJECTS {} ======= // +// // objects --> enclosed by curly braces {}, are generic --> can hold different value types or data types (string, number, etc.) +// // key: value +// // key is also called as property +// const person = { +// name: "Ej", // '' or "" are both fine +// face: "handsome", +// shirt: "green", +// age: 18, +// }; +// // Access object property (key) +// // dot notation: +// console.log(person.name); +// console.log(person.face); +// console.log(person.shirt); +// console.log(person.age); + +// // bracket notation: +// console.log(person["name"]); +// console.log(person["age"]); +// console.log(person["shirt"]); +// console.log(person["face"]); +// console.log(typeof person.age); +// // add new property to person object +// person.hobbies = "programming and workout"; +// person["birth month"] = "January"; +// console.log(person); + +// // ======= EXTRA: don't mind ======= // + +// /** Instructions: +// * - Given the formula, iterate two arrays e and z using a loop +// * g(z) = 1 / (1 + e**-z) +// * g(z) should be in list form / object form {} that holds generic data types (just store results in another new array) +// */ +// const e = [ +// 1.718, 2.718, 3.718, 4.718, 5.718, 6.718, 7.718, 8.718, 9.718, 10.718, +// ]; +// const z = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]; +// const result = (e, z) => { +// let resultsArr = []; +// for (let i = 0; i < e.length; i++) { +// let formula = 1 / (1 + Math.pow(e[i], -z[i])); +// resultsArr.push(formula); +// } +// console.log(resultsArr); +// }; + +// result(e, z); + +// // ======= Create a function that uses ES6 arrow function with 2 arguments, object, and template literals ======= // +// // then store a function in the object, use 'this' keyword + +// /** +// * createPerson function expression +// * +// * @param {string} theName - takes in name +// * @param {string} shirt - takes in shirt +// * +// * @object property {networth} is a function that refers to assets subtracted by liabilities +// * 'this' keyword refers to the personNew object that houses the properties +// * +// * @returns {string} a template literal by personNew.{property key} +// * when calling or returning a function in an object add parenthesis () for ex. personNew.networth() +// */ +// const createPerson = (theName, shirt) => { +// const personNew = { +// name: theName, +// shirt: shirt, +// face: "handsome", +// assets: 1000000000, +// liabilities: 700000, +// networth: function () { +// return this.assets - this.liabilities; +// }, +// }; + +// return `${personNew.name} is the name and shirt is ${ +// personNew.shirt +// }, and face is ${personNew.face}. The networth is ${personNew.networth()}`; +// }; + +// console.log(createPerson("Ej", "green")); + +// // ======= LOOPS for loop ======= + +// const fruits = ["mango", "banana", "strawberry", "kiwi", "melon"]; + +// // beautiful normal loop using i iteration +// for (let i = 0; i < fruits.length; i++) { +// console.log(fruits[i]); +// } +// // beautiful shorter form of a for loop using 'of' keyword +// for (const fruit of fruits) { +// console.log(`\nThe fruits are: ${fruit}`); +// } -// use 'in' keyword for loops and use .length property for total amount of characters -const countLetters = () => { - let phrase = "hey how are you yes good yes yes ok wow"; - return phrase.length; -}; -console.log(`The total letters are: ${countLetters()}`); - -// ======= LOOPS using 'of' and 'in' keyword ======= // -/** - * of --> when looped, returns string or characters - * in --> when looped, returns numbers instead - * both can be used when populating a new array - */ -const countLettersWithInKeyword = () => { - let phrase = "hey hey hey hye yesyesyeysys ok wow"; - - for (letter in phrase) { - console.log(letter); - } -}; -countLettersWithInKeyword(); // prints 0 1 2 3 4 5 6 7 ... - -const countLettersWithOfKeyword = () => { - let phrase = "hey hey hey hye yesyesyeysys ok wow"; - - for (letter of phrase) { - console.log(letter); - } -}; -countLettersWithOfKeyword(); // prints h e y h e y h e y h y e y e s y e s y ... - -const phraseResult = "hey hey ywo hey hey hey good wow ok wow"; -let counter = 0; - -for (const index in phraseResult) { - console.log(parseInt(index) + 1); - counter = index; -} -console.log({ counter }); // returns result as an object { counter: '38' } - -// ======= Sum of the numbers in an array ======= // -const arrayNum = [1, 2, 3, 4, 5, 8, 12, 11, 7, 77, 70, 777]; - -/** - * sumOfArray function --> product goooooood - * - * @param {number} arr - given array - * - * @returns {number} - the sum of array by mutating the result variable - adding the current result value + values in the array - */ -const sumOfArray = (arr) => { - let result = 0; - for (const num of arr) { - console.log(num); - result = result + num; - } - return { result }; // returns object - // can also do return result --> to return as a number -}; -console.log(sumOfArray(arrayNum)); - - - -// ======= Max Function ======= // -/** - * @function max(array) - * - * @param {number} array - given array - * - * @returns {number} - the max number in an array - */ -const max = (array) => { - let result = 0; - for (let i = 0; i < array.length; i++) { - let currentNum = array[i]; - let nextNum = array[i + 1]; - if (currentNum < nextNum) { - result = nextNum; - } - if (currentNum > nextNum) { - result = currentNum; - } - } - return `The max number in the array is: ${result}`; -}; -console.log(max(arrayNum)); - -// another way of doing the max function -const anotherMax = (numbers) => { - let result = numbers[0]; - for (const number of numbers) { - if (number > result) { - result = number; - } - } - - // return result; --> can be used if want to return pure number - return { result }; -}; -// console.log(`anotherMax function result: ${anotherMax(arrayNum)}`); --> use if want to use return result above -console.log(anotherMax(arrayNum)); // outputs object in console - - - - -/** - * @function letterFrequency(phrase) - * - * @param {string} phrase - any phrase or even sentence - * - * @returns {object} - counts how many certain specific letters are in a phrase string - * - * @example { - * h: 1, - * o: 14, - * e: 7, - * t: 2, - * w: 3 - * } - */ -const letterFrequency = (phrase) => { - let frequency = {}; - for (const letter of phrase) { - // check if letter exists - if (letter in frequency) { - // if yes then increment++ - frequency[letter] += 1; - } else { - // otherwise set value to 1 - frequency[letter] = 1; - } - } - - return frequency; -}; -console.log(letterFrequency("Wow Ej Sadiarin you so good handsome everything code goooooood")); - - - - -// REAL WORLD EXAMPLE: GOOGLE SEARCH ENGINE -// --> detecting the frequency of searched words (popular) -/** - * @function wordFrequency(phrase) - * - * @param {string} phrase - a phrase or sentence - * - * @method split() - seperates words, letters, etc. given an argument - * @argument characters - in split() method - * - * @returns {object} - counts the certain specific words in a given phrase or sentence - */ -const examplePhrase = "hey hey wow good yes"; -const wordFrequency = (phrase) => { - let frequency = {}; - // use split() method to seperate words - const splitPhrase = phrase.split(" "); - for (const word of splitPhrase) { - // check if word exist - if (word in frequency) { - frequency[word] += 1; - } else { - // if new word then set value to 1 - frequency[word] = 1; - } - } - return frequency; -}; -console.log(wordFrequency(examplePhrase)); - - -/* ======= HIGHER ORDER FUNCTIONS ======= - --> functions that takes in another function as argument and/or returns a function -*/ - -// MAP, FILTER, REDUCE Array Methods -const nums = [1, 2, 3, 4, 7, 9, 5, 8]; - -/** - * MAP @method map() - * - selects all values and - * - changes the value of the whole given array - * - works like loops but returns new array - * - * @param callbackfn - * - * @returns {array} - new array - */ -const mapFunc = (numArray) => { - return numArray.map(number => number * 2); -} -console.log(mapFunc(arrayNum)); +// // use 'in' keyword for loops and use .length property for total amount of characters +// const countLetters = () => { +// let phrase = "hey how are you yes good yes yes ok wow"; +// return phrase.length; +// }; +// console.log(`The total letters are: ${countLetters()}`); + +// // ======= LOOPS using 'of' and 'in' keyword ======= // +// /** +// * of --> when looped, returns string or characters +// * in --> when looped, returns numbers instead +// * both can be used when populating a new array +// */ +// const countLettersWithInKeyword = () => { +// let phrase = "hey hey hey hye yesyesyeysys ok wow"; + +// for (letter in phrase) { +// console.log(letter); +// } +// }; +// countLettersWithInKeyword(); // prints 0 1 2 3 4 5 6 7 ... + +// const countLettersWithOfKeyword = () => { +// let phrase = "hey hey hey hye yesyesyeysys ok wow"; + +// for (letter of phrase) { +// console.log(letter); +// } +// }; +// countLettersWithOfKeyword(); // prints h e y h e y h e y h y e y e s y e s y ... + +// const phraseResult = "hey hey ywo hey hey hey good wow ok wow"; +// let counter = 0; + +// for (const index in phraseResult) { +// console.log(parseInt(index) + 1); +// counter = index; +// } +// console.log({ counter }); // returns result as an object { counter: '38' } + +// // ======= Sum of the numbers in an array ======= // +// const arrayNum = [1, 2, 3, 4, 5, 8, 12, 11, 7, 77, 70, 777]; + +// /** +// * sumOfArray function --> product goooooood +// * +// * @param {number} arr - given array +// * +// * @returns {number} - the sum of array by mutating the result variable - adding the current result value + values in the array +// */ +// const sumOfArray = (arr) => { +// let result = 0; +// for (const num of arr) { +// console.log(num); +// result = result + num; +// } +// return { result }; // returns object +// // can also do return result --> to return as a number +// }; +// console.log(sumOfArray(arrayNum)); + + + +// // ======= Max Function ======= // +// /** +// * @function max(array) +// * +// * @param {number} array - given array +// * +// * @returns {number} - the max number in an array +// */ +// const max = (array) => { +// let result = 0; +// for (let i = 0; i < array.length; i++) { +// let currentNum = array[i]; +// let nextNum = array[i + 1]; +// if (currentNum < nextNum) { +// result = nextNum; +// } +// if (currentNum > nextNum) { +// result = currentNum; +// } +// } +// return `The max number in the array is: ${result}`; +// }; +// console.log(max(arrayNum)); + +// // another way of doing the max function +// const anotherMax = (numbers) => { +// let result = numbers[0]; +// for (const number of numbers) { +// if (number > result) { +// result = number; +// } +// } +// // return result; --> can be used if want to return pure number +// return { result }; +// }; +// // console.log(`anotherMax function result: ${anotherMax(arrayNum)}`); --> use if want to use return result above +// console.log(anotherMax(arrayNum)); // outputs object in console + + + + +// /** +// * @function letterFrequency(phrase) +// * +// * @param {string} phrase - any phrase or even sentence +// * +// * @returns {object} - counts how many certain specific letters are in a phrase string +// * +// * @example { +// * h: 1, +// * o: 14, +// * e: 7, +// * t: 2, +// * w: 3 +// * } +// */ +// const letterFrequency = (phrase) => { +// let frequency = {}; +// for (const letter of phrase) { +// // check if letter exists +// if (letter in frequency) { +// // if yes then increment++ +// frequency[letter] += 1; +// } else { +// // otherwise set value to 1 +// frequency[letter] = 1; +// } +// } -// MAP simple ver -console.log(nums.map(num => num * 2)); +// return frequency; +// }; +// console.log(letterFrequency("Wow Ej Sadiarin you so good handsome everything code goooooood")); + + + + +// // REAL WORLD EXAMPLE: GOOGLE SEARCH ENGINE +// // --> detecting the frequency of searched words (popular) +// /** +// * @function wordFrequency(phrase) +// * +// * @param {string} phrase - a phrase or sentence +// * +// * @method split() - seperates words, letters, etc. given an argument +// * @argument characters - in split() method +// * +// * @returns {object} - counts the certain specific words in a given phrase or sentence +// */ +// const examplePhrase = "hey hey wow good yes"; +// const wordFrequency = (phrase) => { +// let frequency = {}; +// // use split() method to seperate words +// const splitPhrase = phrase.split(" "); +// for (const word of splitPhrase) { +// // check if word exist +// if (word in frequency) { +// frequency[word] += 1; +// } else { +// // if new word then set value to 1 +// frequency[word] = 1; +// } +// } +// return frequency; +// }; +// console.log(wordFrequency(examplePhrase)); + + +// /* ======= HIGHER ORDER FUNCTIONS ======= +// --> functions that takes in another function as argument and/or returns a function +// */ + +// // MAP, FILTER, REDUCE Array Methods +// const nums = [1, 2, 3, 4, 7, 9, 5, 8]; + +// /** +// * MAP @method map() +// * - selects all values and +// * - changes the value of the whole given array +// * - works like loops but returns new array +// * +// * @param callbackfn +// * +// * @returns {array} - new array +// */ +// const mapFunc = (numArray) => { +// return numArray.map(number => number * 2); +// } +// console.log(mapFunc(arrayNum)); +// // MAP simple ver +// console.log(nums.map(num => num * 2)); -// FILTER - filters out values from an array given a condition -// - works like loops but returns new array -console.log(nums.filter(num => num > 5 || num === 5)); -// works the same as this: -const filter = (numbers, greaterThan) => { - let result = []; - for (const number of numbers) { - if (number > greaterThan) { - result.push(number); - } - } - return result; -}; -console.log(filter([1, 2, 3, 7, 4, 5], 3)); -// REDUCE -/** - * reduces array to a single value - * used in SUM - * @returns one number / single value - */ +// // FILTER - filters out values from an array given a condition +// // - works like loops but returns new array +// console.log(nums.filter(num => num > 5 || num === 5)); -// -console.log(nums.reduce(num => num * 2, 9)); -// given an array called nums, use reduce() array method to return the product of the whole array -console.log(nums.reduce((a, b) => a * b)); +// // works the same as this: +// const filter = (numbers, greaterThan) => { +// let result = []; +// for (const number of numbers) { +// if (number > greaterThan) { +// result.push(number); +// } +// } +// return result; +// }; +// console.log(filter([1, 2, 3, 7, 4, 5], 3)); + +// // REDUCE +// /** +// * reduces array to a single value +// * used in SUM +// * @returns one number / single value +// */ + +// // +// console.log(nums.reduce(num => num * 2, 9)); +// // given an array called nums, use reduce() array method to return the product of the whole array +// console.log(nums.reduce((a, b) => a * b)); + +// // return sum of array using sum() function +// const sum = (a, b) => { +// return a + b; +// } +// const resultNumsArray = nums.reduce(sum); +// console.log(resultNumsArray); +// console.log(sumOfArray(nums)); -// return sum of array using sum() function -const sum = (a, b) => { - return a + b; -} -const resultNumsArray = nums.reduce(sum); -console.log(resultNumsArray); -console.log(sumOfArray(nums)); - - -const exampleObj = [ - {name: "personOne", money: 1000000}, - {name: "personTwo", money: 7080328}, - {name: "personThree", money: 891302777} -] -let moneyp = exampleObj[1]; -console.log(moneyp); - -// ======= given the array of objects above, get the sum of the money {number} ======= // -const moneySum = (objectPerson) => { - let result = 0; - for (const person of objectPerson) { - result = result + person.money; - } - return result; -} -console.log(moneySum(exampleObj)); -// TODO: optimize above solution (use reduce()) -// const reducef = nums.reduce(num => num.reduce()) -// const moneySumReduce = (arr) => { +// const exampleObj = [ +// {name: "personOne", money: 1000000}, +// {name: "personTwo", money: 7080328}, +// {name: "personThree", money: 891302777} +// ] +// let moneyp = exampleObj[1]; +// console.log(moneyp); + +// // ======= given the array of objects above, get the sum of the money {number} ======= // +// const moneySum = (objectPerson) => { // let result = 0; -// for (const num in arr) { - +// for (const person of objectPerson) { +// result = result + person.money; // } +// return result; // } -// const func = () => { +// console.log(moneySum(exampleObj)); + +// // TODO: optimize above solution (use reduce()) +// // const reducef = nums.reduce(num => num.reduce()) +// // const moneySumReduce = (arr) => { +// // let result = 0; +// // for (const num in arr) { + +// // } +// // } +// // const func = () => { +// // let result = 0; +// // for (const num of nums) { +// // result = result + num; +// // } +// // } + +// // ======= API ======= // +// /** +// * +// */ +// const dogImage = document.getElementById('dogImage'); +// const dogButton = document.getElementById('dogButton'); + +// // example: 'run 2nd' will run last since it is async +// console.log('run 1st') +// fetch('https://dog.ceo/api/breeds/image/random') +// .then(response => response.json) +// .then(json => { +// console.log(json.message) +// dogImage.innerHTML = `` +// }) + +// console.log('run 3rd'); + +// // Get a random number and set minimum value to 1 and max to 734 +// const minMax = (number) => { // let result = 0; -// for (const num of nums) { -// result = result + num; +// // reroll +// if (number > 735 && number < 1) { +// minMax(number); +// } else { +// result = number; +// return result; // } + // } +// // @argument {number} 734 - defines the maximum value +// console.log(minMax(Math.floor(Math.random() * 735))); + +// Superhero API +// ACCESS TOKEN: 127759136856495 + +// get the data of a hero (simple consume API in JavaScript) +const accessToken = "127759136856495"; +const baseURL = `https://superheroapi.com/api.php/${accessToken}`; -// ======= API ======= // -/** - * - */ -const dogImage = document.getElementById('dogImage'); -const dogButton = document.getElementById('dogButton'); - -// example: 'run 2nd' will run last since it is async -console.log('run 1st') -fetch('https://dog.ceo/api/breeds/image/random') - .then(response => response.json) +const getHero = (id) => { + fetch(`${baseURL}/${id}`) + .then(response => response.json()) .then(json => { - console.log(json.message) - dogImage.innerHTML = `` + console.log(json); + }) +} +getHero(1); - console.log('run 3rd'); - -// Get a random number and set minimum value to 1 and max to 734 -const minMax = (number) => { - let result = 0; - // reroll - if (number > 735 && number < 1) { - minMax(number); - } else { - result = number; - return result; - } - +// Call getHero() with random number argument - create a function for the random number +const randomNumber = () => { + const numberOfHeroes = 731; + return Math.floor(Math.random() * numberOfHeroes) + 1; } -// @argument {number} 734 - defines the maximum value -console.log(minMax(Math.floor(Math.random() * 735))); \ No newline at end of file +console.log(randomNumber()); + + From d84890764ed9ce1fe147965b9842ac6915bc7c80 Mon Sep 17 00:00:00 2001 From: ejsadiarin Date: Sat, 14 Jan 2023 21:33:05 +0800 Subject: [PATCH 26/31] added API fetch with access token and calling getHero API with random number --- yourPlayground.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/yourPlayground.js b/yourPlayground.js index 1885759..0200779 100644 --- a/yourPlayground.js +++ b/yourPlayground.js @@ -490,9 +490,9 @@ const getHero = (id) => { .then(response => response.json()) .then(json => { console.log(json); - }) } + getHero(1); // Call getHero() with random number argument - create a function for the random number @@ -500,6 +500,8 @@ const randomNumber = () => { const numberOfHeroes = 731; return Math.floor(Math.random() * numberOfHeroes) + 1; } + console.log(randomNumber()); +getHero(randomNumber()); \ No newline at end of file From 778bcef8c2ccac120f80964d9db33e1004db890d Mon Sep 17 00:00:00 2001 From: ejsadiarin Date: Sat, 14 Jan 2023 23:16:14 +0800 Subject: [PATCH 27/31] added readme, uncomment yourPlayground --- README.md | 8 ++ index.html | 1 + yourPlayground.js | 212 +++++++++++++++++++++++----------------------- 3 files changed, 115 insertions(+), 106 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..f9d2838 --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +# JavaScript Course 2023 +- by Clever Programmer + +## Coverage (Topics Covered) +- + +## Projects Created +- \ No newline at end of file diff --git a/index.html b/index.html index a0d6899..9f797ad 100644 --- a/index.html +++ b/index.html @@ -19,6 +19,7 @@

Official Github REPO (🌟 Go Star This ♥️)

+

Lesgowoowowow

👉 JavaScript Course by Clever Programmer (Github Repo)

Let's get this trending 🚀

Lessons

diff --git a/yourPlayground.js b/yourPlayground.js index 0200779..82db945 100644 --- a/yourPlayground.js +++ b/yourPlayground.js @@ -354,129 +354,129 @@ // // MAP, FILTER, REDUCE Array Methods // const nums = [1, 2, 3, 4, 7, 9, 5, 8]; -// /** -// * MAP @method map() -// * - selects all values and -// * - changes the value of the whole given array -// * - works like loops but returns new array -// * -// * @param callbackfn -// * -// * @returns {array} - new array -// */ -// const mapFunc = (numArray) => { -// return numArray.map(number => number * 2); -// } -// console.log(mapFunc(arrayNum)); - +/** + * MAP @method map() + * - selects all values and + * - changes the value of the whole given array + * - works like loops but returns new array + * + * @param callbackfn + * + * @returns {array} - new array + */ +const mapFunc = (numArray) => { + return numArray.map(number => number * 2); +} +console.log(mapFunc(arrayNum)); -// // MAP simple ver -// console.log(nums.map(num => num * 2)); +// MAP simple ver +console.log(nums.map(num => num * 2)); -// // FILTER - filters out values from an array given a condition -// // - works like loops but returns new array -// console.log(nums.filter(num => num > 5 || num === 5)); -// // works the same as this: -// const filter = (numbers, greaterThan) => { -// let result = []; -// for (const number of numbers) { -// if (number > greaterThan) { -// result.push(number); -// } -// } -// return result; -// }; -// console.log(filter([1, 2, 3, 7, 4, 5], 3)); +// FILTER - filters out values from an array given a condition +// - works like loops but returns new array +console.log(nums.filter(num => num > 5 || num === 5)); -// // REDUCE -// /** -// * reduces array to a single value -// * used in SUM -// * @returns one number / single value -// */ - -// // -// console.log(nums.reduce(num => num * 2, 9)); -// // given an array called nums, use reduce() array method to return the product of the whole array -// console.log(nums.reduce((a, b) => a * b)); - -// // return sum of array using sum() function -// const sum = (a, b) => { -// return a + b; -// } -// const resultNumsArray = nums.reduce(sum); -// console.log(resultNumsArray); -// console.log(sumOfArray(nums)); +// works the same as this: +const filter = (numbers, greaterThan) => { + let result = []; + for (const number of numbers) { + if (number > greaterThan) { + result.push(number); + } + } + return result; +}; +console.log(filter([1, 2, 3, 7, 4, 5], 3)); +// REDUCE +/** + * reduces array to a single value + * used in SUM + * @returns one number / single value + */ -// const exampleObj = [ -// {name: "personOne", money: 1000000}, -// {name: "personTwo", money: 7080328}, -// {name: "personThree", money: 891302777} -// ] -// let moneyp = exampleObj[1]; -// console.log(moneyp); +// +console.log(nums.reduce(num => num * 2, 9)); +// given an array called nums, use reduce() array method to return the product of the whole array +console.log(nums.reduce((a, b) => a * b)); -// // ======= given the array of objects above, get the sum of the money {number} ======= // -// const moneySum = (objectPerson) => { -// let result = 0; -// for (const person of objectPerson) { -// result = result + person.money; -// } -// return result; -// } -// console.log(moneySum(exampleObj)); +// return sum of array using sum() function +const sum = (a, b) => { + return a + b; +} +const resultNumsArray = nums.reduce(sum); +console.log(resultNumsArray); +console.log(sumOfArray(nums)); + + +const exampleObj = [ + {name: "personOne", money: 1000000}, + {name: "personTwo", money: 7080328}, + {name: "personThree", money: 891302777} +] +let moneyp = exampleObj[1]; +console.log(moneyp); + +// ======= given the array of objects above, get the sum of the money {number} ======= // +const moneySum = (objectPerson) => { + let result = 0; + for (const person of objectPerson) { + result = result + person.money; + } + return result; +} +console.log(moneySum(exampleObj)); -// // TODO: optimize above solution (use reduce()) -// // const reducef = nums.reduce(num => num.reduce()) -// // const moneySumReduce = (arr) => { -// // let result = 0; -// // for (const num in arr) { +// TODO: optimize above solution (use reduce()) +const reducef = nums.reduce(num => num.reduce()) +const moneySumReduce = (arr) => { + let result = 0; + for (const num in arr) { -// // } -// // } -// // const func = () => { -// // let result = 0; -// // for (const num of nums) { -// // result = result + num; -// // } -// // } + } +} +const func = () => { + let result = 0; + for (const num of nums) { + result = result + num; + } +} // // ======= API ======= // // /** // * // */ -// const dogImage = document.getElementById('dogImage'); -// const dogButton = document.getElementById('dogButton'); - -// // example: 'run 2nd' will run last since it is async -// console.log('run 1st') -// fetch('https://dog.ceo/api/breeds/image/random') -// .then(response => response.json) -// .then(json => { -// console.log(json.message) -// dogImage.innerHTML = `` -// }) - -// console.log('run 3rd'); - -// // Get a random number and set minimum value to 1 and max to 734 -// const minMax = (number) => { -// let result = 0; -// // reroll -// if (number > 735 && number < 1) { -// minMax(number); -// } else { -// result = number; -// return result; -// } +const dogImage = document.getElementById('dogImage'); +const dogButton = document.getElementById('dogButton'); + +// example: 'run 2nd' will run last since it is async +console.log('run 1st') +fetch('https://dog.ceo/api/breeds/image/random') + .then(response => response.json) + .then(json => { + console.log(json.message) + dogImage.innerHTML = `` + }) + + console.log('run 3rd'); + +// Get a random number and set minimum value to 1 and max to 734 +const minMax = (number) => { + let result = 0; + // reroll + if (number > 735 && number < 1) { + minMax(number); + } else { + result = number; + return result; + } -// } -// // @argument {number} 734 - defines the maximum value -// console.log(minMax(Math.floor(Math.random() * 735))); +} +// @argument {number} 734 - defines the maximum value +console.log(minMax(Math.floor(Math.random() * 735))); // Superhero API // ACCESS TOKEN: 127759136856495 From 794470b93dbc15c8f818bc246a40a3743560688f Mon Sep 17 00:00:00 2001 From: ejsadiarin Date: Sun, 15 Jan 2023 00:09:59 +0800 Subject: [PATCH 28/31] changed vscode theme to Monokai Night --- index.html | 1 + yourPlayground.js | 659 +++++++++++++++++++++++----------------------- 2 files changed, 332 insertions(+), 328 deletions(-) diff --git a/index.html b/index.html index 9f797ad..18d0e9b 100644 --- a/index.html +++ b/index.html @@ -20,6 +20,7 @@

Official Github REPO (🌟 Go Star This ♥️)

Lesgowoowowow

+

👉 JavaScript Course by Clever Programmer (Github Repo)

Let's get this trending 🚀

Lessons

diff --git a/yourPlayground.js b/yourPlayground.js index 82db945..c2e8de2 100644 --- a/yourPlayground.js +++ b/yourPlayground.js @@ -1,358 +1,358 @@ -// console.log("EJ SADIARIN IN THE UNVERSE!!!"); -// const food = 200; -// const pricePercent = 0.2; -// const tip = food * pricePercent; -// console.log(tip); - -// let input = "sentence"; -// const typeInput = typeof input; - -// console.log(`input type is: ${typeInput}`); - -// let inputInt = Number(input); -// console.log(typeof inputInt); -// console.log(inputInt); - -// // Date -// let now = new Date().toDateString(); -// console.log(`The type of date: ${typeof now}`); -// console.log(now); -// function floorRandom() { -// let random = Math.random() * 3; -// let randomFloor = Math.floor(random); -// return randomFloor; -// // or console.log(randomFloor); -// // and just call floorRandom(); -// } -// // floorRandom(); -// console.log(floorRandom()); - -// // THE REST IS IN VSCODE // - -// // Documentation Best Practices: +console.log("EJ SADIARIN IN THE UNVERSE!!!"); +const food = 200; +const pricePercent = 0.2; +const tip = food * pricePercent; +console.log(tip); +// +let input = "sentence"; +const typeInput = typeof input; +// +console.log(`input type is: ${typeInput}`); +// +let inputInt = Number(input); +console.log(typeof inputInt); +console.log(inputInt); +// +// Date +let now = new Date().toDateString(); +console.log(`The type of date: ${typeof now}`); +console.log(now); +function floorRandom() { + let random = Math.random() * 3; + let randomFloor = Math.floor(random); + return randomFloor; + // or console.log(randomFloor); + // and just call floorRandom(); +} +// floorRandom(); +console.log(floorRandom()); +// +// THE REST IS IN VSCODE // +// +// Documentation Best Practices: +// +/** + * In this format (for functions) + * + * @param {number} num1 - first number to add + * @param {number} num2 - second number to add + * + * @return {number} - returns result sum of adding num1 and num2 + */ -// /** -// * In this format (for functions) -// * -// * @param {number} num1 - first number to add -// * @param {number} num2 - second number to add -// * -// * @return {number} - returns result sum of adding num1 and num2 -// */ +// Array +let letters = ["a", "b", "c", "d", "e"]; +console.log(letters); +console.log(letters[0]); -// // Array -// let letters = ["a", "b", "c", "d", "e"]; -// console.log(letters); -// console.log(letters[0]); +// ======= Array methods ======= // -// // ======= Array methods ======= // +/* ********************************** +1: push --> adds new value to the end of the array +*/ -// /* ********************************** -// 1: push --> adds new value to the end of the array -// */ +letters.push("f"); +console.log(letters); -// letters.push("f"); -// console.log(letters); +/* ********************************** +2: slice --> gets value from array +*/ -// /* ********************************** -// 2: slice --> gets value from array -// */ +console.log(letters.slice(0, 3)); +/** + * @param {indexStart} - index of the value in the array to start from + * @param {indexEnd} - x (index of value you want to get) < indexEnd + * + * Basically: indexStart < x < indexEnd + */ -// console.log(letters.slice(0, 3)); -// /** -// * @param {indexStart} - index of the value in the array to start from -// * @param {indexEnd} - x (index of value you want to get) < indexEnd -// * -// * Basically: indexStart < x < indexEnd -// */ +// Get d, e, f from letters array: +console.log(letters.slice(3, 6)); + +/* ********************************** +3: indexOf --> gets index of the value selected from the specific array +*/ +console.log(letters.indexOf("e")); + +/* ********************************** +4: length --> gets the total index length of an array +*/ +console.log(letters.length); + +// ======= OBJECTS {} ======= // +// objects --> enclosed by curly braces {}, are generic --> can hold different value types or data types (string, number, etc.) +// key: value +// key is also called as property +const person = { + name: "Ej", // '' or "" are both fine + face: "handsome", + shirt: "green", + age: 18, +}; +// Access object property (key) +// dot notation: +console.log(person.name); +console.log(person.face); +console.log(person.shirt); +console.log(person.age); + +// bracket notation: +console.log(person["name"]); +console.log(person["age"]); +console.log(person["shirt"]); +console.log(person["face"]); +console.log(typeof person.age); +// add new property to person object +person.hobbies = "programming and workout"; +person["birth month"] = "January"; +console.log(person); + +// ======= EXTRA: don't mind ======= // + +/** Instructions: + * - Given the formula, iterate two arrays e and z using a loop + * g(z) = 1 / (1 + e**-z) + * g(z) should be in list form / object form {} that holds generic data types (just store results in another new array) + */ +const e = [ + 1.718, 2.718, 3.718, 4.718, 5.718, 6.718, 7.718, 8.718, 9.718, 10.718, +]; +const z = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]; +const result = (e, z) => { + let resultsArr = []; + for (let i = 0; i < e.length; i++) { + let formula = 1 / (1 + Math.pow(e[i], -z[i])); + resultsArr.push(formula); + } + console.log(resultsArr); +}; -// // Get d, e, f from letters array: -// console.log(letters.slice(3, 6)); - -// /* ********************************** -// 3: indexOf --> gets index of the value selected from the specific array -// */ -// console.log(letters.indexOf("e")); - -// /* ********************************** -// 4: length --> gets the total index length of an array -// */ -// console.log(letters.length); - -// // ======= OBJECTS {} ======= // -// // objects --> enclosed by curly braces {}, are generic --> can hold different value types or data types (string, number, etc.) -// // key: value -// // key is also called as property -// const person = { -// name: "Ej", // '' or "" are both fine -// face: "handsome", -// shirt: "green", -// age: 18, -// }; -// // Access object property (key) -// // dot notation: -// console.log(person.name); -// console.log(person.face); -// console.log(person.shirt); -// console.log(person.age); - -// // bracket notation: -// console.log(person["name"]); -// console.log(person["age"]); -// console.log(person["shirt"]); -// console.log(person["face"]); -// console.log(typeof person.age); -// // add new property to person object -// person.hobbies = "programming and workout"; -// person["birth month"] = "January"; -// console.log(person); - -// // ======= EXTRA: don't mind ======= // - -// /** Instructions: -// * - Given the formula, iterate two arrays e and z using a loop -// * g(z) = 1 / (1 + e**-z) -// * g(z) should be in list form / object form {} that holds generic data types (just store results in another new array) -// */ -// const e = [ -// 1.718, 2.718, 3.718, 4.718, 5.718, 6.718, 7.718, 8.718, 9.718, 10.718, -// ]; -// const z = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]; -// const result = (e, z) => { -// let resultsArr = []; -// for (let i = 0; i < e.length; i++) { -// let formula = 1 / (1 + Math.pow(e[i], -z[i])); -// resultsArr.push(formula); -// } -// console.log(resultsArr); -// }; - -// result(e, z); - -// // ======= Create a function that uses ES6 arrow function with 2 arguments, object, and template literals ======= // -// // then store a function in the object, use 'this' keyword +result(e, z); -// /** -// * createPerson function expression -// * -// * @param {string} theName - takes in name -// * @param {string} shirt - takes in shirt -// * -// * @object property {networth} is a function that refers to assets subtracted by liabilities -// * 'this' keyword refers to the personNew object that houses the properties -// * -// * @returns {string} a template literal by personNew.{property key} -// * when calling or returning a function in an object add parenthesis () for ex. personNew.networth() -// */ -// const createPerson = (theName, shirt) => { -// const personNew = { -// name: theName, -// shirt: shirt, -// face: "handsome", -// assets: 1000000000, -// liabilities: 700000, -// networth: function () { -// return this.assets - this.liabilities; -// }, -// }; - -// return `${personNew.name} is the name and shirt is ${ -// personNew.shirt -// }, and face is ${personNew.face}. The networth is ${personNew.networth()}`; -// }; - -// console.log(createPerson("Ej", "green")); - -// // ======= LOOPS for loop ======= - -// const fruits = ["mango", "banana", "strawberry", "kiwi", "melon"]; - -// // beautiful normal loop using i iteration -// for (let i = 0; i < fruits.length; i++) { -// console.log(fruits[i]); -// } -// // beautiful shorter form of a for loop using 'of' keyword -// for (const fruit of fruits) { -// console.log(`\nThe fruits are: ${fruit}`); -// } - -// // use 'in' keyword for loops and use .length property for total amount of characters -// const countLetters = () => { -// let phrase = "hey how are you yes good yes yes ok wow"; -// return phrase.length; -// }; -// console.log(`The total letters are: ${countLetters()}`); - -// // ======= LOOPS using 'of' and 'in' keyword ======= // -// /** -// * of --> when looped, returns string or characters -// * in --> when looped, returns numbers instead -// * both can be used when populating a new array -// */ -// const countLettersWithInKeyword = () => { -// let phrase = "hey hey hey hye yesyesyeysys ok wow"; +// ======= Create a function that uses ES6 arrow function with 2 arguments, object, and template literals ======= // +// then store a function in the object, use 'this' keyword -// for (letter in phrase) { -// console.log(letter); -// } -// }; -// countLettersWithInKeyword(); // prints 0 1 2 3 4 5 6 7 ... +/** + * createPerson function expression + * + * @param {string} theName - takes in name + * @param {string} shirt - takes in shirt + * + * @object property {networth} is a function that refers to assets subtracted by liabilities + * 'this' keyword refers to the personNew object that houses the properties + * + * @returns {string} a template literal by personNew.{property key} + * when calling or returning a function in an object add parenthesis () for ex. personNew.networth() + */ +const createPerson = (theName, shirt) => { + const personNew = { + name: theName, + shirt: shirt, + face: "handsome", + assets: 1000000000, + liabilities: 700000, + networth: function () { + return this.assets - this.liabilities; + }, + }; + + return `${personNew.name} is the name and shirt is ${ + personNew.shirt + }, and face is ${personNew.face}. The networth is ${personNew.networth()}`; +}; -// const countLettersWithOfKeyword = () => { -// let phrase = "hey hey hey hye yesyesyeysys ok wow"; +console.log(createPerson("Ej", "green")); -// for (letter of phrase) { -// console.log(letter); -// } -// }; -// countLettersWithOfKeyword(); // prints h e y h e y h e y h y e y e s y e s y ... +// ======= LOOPS for loop ======= -// const phraseResult = "hey hey ywo hey hey hey good wow ok wow"; -// let counter = 0; +const fruits = ["mango", "banana", "strawberry", "kiwi", "melon"]; -// for (const index in phraseResult) { -// console.log(parseInt(index) + 1); -// counter = index; -// } -// console.log({ counter }); // returns result as an object { counter: '38' } +// beautiful normal loop using i iteration +for (let i = 0; i < fruits.length; i++) { + console.log(fruits[i]); +} +// beautiful shorter form of a for loop using 'of' keyword +for (const fruit of fruits) { + console.log(`\nThe fruits are: ${fruit}`); +} -// // ======= Sum of the numbers in an array ======= // -// const arrayNum = [1, 2, 3, 4, 5, 8, 12, 11, 7, 77, 70, 777]; +// use 'in' keyword for loops and use .length property for total amount of characters +const countLetters = () => { + let phrase = "hey how are you yes good yes yes ok wow"; + return phrase.length; +}; +console.log(`The total letters are: ${countLetters()}`); -// /** -// * sumOfArray function --> product goooooood -// * -// * @param {number} arr - given array -// * -// * @returns {number} - the sum of array by mutating the result variable - adding the current result value + values in the array -// */ -// const sumOfArray = (arr) => { -// let result = 0; -// for (const num of arr) { -// console.log(num); -// result = result + num; -// } -// return { result }; // returns object -// // can also do return result --> to return as a number -// }; -// console.log(sumOfArray(arrayNum)); +// ======= LOOPS using 'of' and 'in' keyword ======= // +/** + * of --> when looped, returns string or characters + * in --> when looped, returns numbers instead + * both can be used when populating a new array + */ +const countLettersWithInKeyword = () => { + let phrase = "hey hey hey hye yesyesyeysys ok wow"; + for (letter in phrase) { + console.log(letter); + } +}; +countLettersWithInKeyword(); // prints 0 1 2 3 4 5 6 7 ... +const countLettersWithOfKeyword = () => { + let phrase = "hey hey hey hye yesyesyeysys ok wow"; -// // ======= Max Function ======= // -// /** -// * @function max(array) -// * -// * @param {number} array - given array -// * -// * @returns {number} - the max number in an array -// */ -// const max = (array) => { -// let result = 0; -// for (let i = 0; i < array.length; i++) { -// let currentNum = array[i]; -// let nextNum = array[i + 1]; -// if (currentNum < nextNum) { -// result = nextNum; -// } -// if (currentNum > nextNum) { -// result = currentNum; -// } -// } -// return `The max number in the array is: ${result}`; -// }; -// console.log(max(arrayNum)); - -// // another way of doing the max function -// const anotherMax = (numbers) => { -// let result = numbers[0]; -// for (const number of numbers) { -// if (number > result) { -// result = number; -// } -// } - -// // return result; --> can be used if want to return pure number -// return { result }; -// }; -// // console.log(`anotherMax function result: ${anotherMax(arrayNum)}`); --> use if want to use return result above -// console.log(anotherMax(arrayNum)); // outputs object in console + for (letter of phrase) { + console.log(letter); + } +}; +countLettersWithOfKeyword(); // prints h e y h e y h e y h y e y e s y e s y ... +const phraseResult = "hey hey ywo hey hey hey good wow ok wow"; +let counter = 0; +for (const index in phraseResult) { + console.log(parseInt(index) + 1); + counter = index; +} +console.log({ counter }); // returns result as an object { counter: '38' } +// ======= Sum of the numbers in an array ======= // +const arrayNum = [1, 2, 3, 4, 5, 8, 12, 11, 7, 77, 70, 777]; -// /** -// * @function letterFrequency(phrase) -// * -// * @param {string} phrase - any phrase or even sentence -// * -// * @returns {object} - counts how many certain specific letters are in a phrase string -// * -// * @example { -// * h: 1, -// * o: 14, -// * e: 7, -// * t: 2, -// * w: 3 -// * } -// */ -// const letterFrequency = (phrase) => { -// let frequency = {}; -// for (const letter of phrase) { -// // check if letter exists -// if (letter in frequency) { -// // if yes then increment++ -// frequency[letter] += 1; -// } else { -// // otherwise set value to 1 -// frequency[letter] = 1; -// } -// } +/** + * sumOfArray function --> product goooooood + * + * @param {number} arr - given array + * + * @returns {number} - the sum of array by mutating the result variable - adding the current result value + values in the array + */ +const sumOfArray = (arr) => { + let result = 0; + for (const num of arr) { + console.log(num); + result = result + num; + } + return { result }; // returns object + // can also do return result --> to return as a number +}; +console.log(sumOfArray(arrayNum)); -// return frequency; -// }; -// console.log(letterFrequency("Wow Ej Sadiarin you so good handsome everything code goooooood")); +// ======= Max Function ======= // +/** + * @function max(array) + * + * @param {number} array - given array + * + * @returns {number} - the max number in an array + */ +const max = (array) => { + let result = 0; + for (let i = 0; i < array.length; i++) { + let currentNum = array[i]; + let nextNum = array[i + 1]; + if (currentNum < nextNum) { + result = nextNum; + } + if (currentNum > nextNum) { + result = currentNum; + } + } + return `The max number in the array is: ${result}`; +}; +console.log(max(arrayNum)); + +// another way of doing the max function +const anotherMax = (numbers) => { + let result = numbers[0]; + for (const number of numbers) { + if (number > result) { + result = number; + } + } + + // return result; --> can be used if want to return pure number + return { result }; +}; +// console.log(`anotherMax function result: ${anotherMax(arrayNum)}`); --> use if want to use return result above +console.log(anotherMax(arrayNum)); // outputs object in console -// // REAL WORLD EXAMPLE: GOOGLE SEARCH ENGINE -// // --> detecting the frequency of searched words (popular) -// /** -// * @function wordFrequency(phrase) -// * -// * @param {string} phrase - a phrase or sentence -// * -// * @method split() - seperates words, letters, etc. given an argument -// * @argument characters - in split() method -// * -// * @returns {object} - counts the certain specific words in a given phrase or sentence -// */ -// const examplePhrase = "hey hey wow good yes"; -// const wordFrequency = (phrase) => { -// let frequency = {}; -// // use split() method to seperate words -// const splitPhrase = phrase.split(" "); -// for (const word of splitPhrase) { -// // check if word exist -// if (word in frequency) { -// frequency[word] += 1; -// } else { -// // if new word then set value to 1 -// frequency[word] = 1; -// } -// } -// return frequency; -// }; -// console.log(wordFrequency(examplePhrase)); - - -// /* ======= HIGHER ORDER FUNCTIONS ======= -// --> functions that takes in another function as argument and/or returns a function -// */ - -// // MAP, FILTER, REDUCE Array Methods -// const nums = [1, 2, 3, 4, 7, 9, 5, 8]; + + +/** + * @function letterFrequency(phrase) + * + * @param {string} phrase - any phrase or even sentence + * + * @returns {object} - counts how many certain specific letters are in a phrase string + * + * @example { + * h: 1, + * o: 14, + * e: 7, + * t: 2, + * w: 3 + * } + */ +const letterFrequency = (phrase) => { + let frequency = {}; + for (const letter of phrase) { + // check if letter exists + if (letter in frequency) { + // if yes then increment++ + frequency[letter] += 1; + } else { + // otherwise set value to 1 + frequency[letter] = 1; + } + } + + return frequency; +}; +console.log(letterFrequency("Wow Ej Sadiarin you so good handsome everything code goooooood")); + + + + +// REAL WORLD EXAMPLE: GOOGLE SEARCH ENGINE +// --> detecting the frequency of searched words (popular) +/** + * @function wordFrequency(phrase) + * + * @param {string} phrase - a phrase or sentence + * + * @method split() - seperates words, letters, etc. given an argument + * @argument characters - in split() method + * + * @returns {object} - counts the certain specific words in a given phrase or sentence + */ +const examplePhrase = "hey hey wow good yes"; +const wordFrequency = (phrase) => { + let frequency = {}; + // use split() method to seperate words + const splitPhrase = phrase.split(" "); + for (const word of splitPhrase) { + // check if word exist + if (word in frequency) { + frequency[word] += 1; + } else { + // if new word then set value to 1 + frequency[word] = 1; + } + } + return frequency; +}; +console.log(wordFrequency(examplePhrase)); + + +/* ======= HIGHER ORDER FUNCTIONS ======= + --> functions that takes in another function as argument and/or returns a function +*/ + +// MAP, FILTER, REDUCE Array Methods +const nums = [1, 2, 3, 4, 7, 9, 5, 8]; /** * MAP @method map() @@ -364,6 +364,7 @@ * * @returns {array} - new array */ + const mapFunc = (numArray) => { return numArray.map(number => number * 2); } @@ -462,6 +463,7 @@ fetch('https://dog.ceo/api/breeds/image/random') }) console.log('run 3rd'); + console.log("hey"); // Get a random number and set minimum value to 1 and max to 734 const minMax = (number) => { @@ -502,6 +504,7 @@ const randomNumber = () => { } console.log(randomNumber()); - +console.log("new new\n"); +console.log("new new new\n"); getHero(randomNumber()); \ No newline at end of file From 0c44de011006b72c9d3b4bd7ed67fb30b5989fff Mon Sep 17 00:00:00 2001 From: ejsadiarin Date: Sun, 15 Jan 2023 18:40:34 +0800 Subject: [PATCH 29/31] commented out some --- yourPlayground.js | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/yourPlayground.js b/yourPlayground.js index c2e8de2..653218b 100644 --- a/yourPlayground.js +++ b/yourPlayground.js @@ -432,13 +432,13 @@ const moneySum = (objectPerson) => { console.log(moneySum(exampleObj)); // TODO: optimize above solution (use reduce()) -const reducef = nums.reduce(num => num.reduce()) -const moneySumReduce = (arr) => { - let result = 0; - for (const num in arr) { +// const reducef = nums.reduce(num => num.reduce()) +// const moneySumReduce = (arr) => { +// let result = 0; +// for (const num in arr) { - } -} +// } +// } const func = () => { let result = 0; for (const num of nums) { @@ -450,17 +450,17 @@ const func = () => { // /** // * // */ -const dogImage = document.getElementById('dogImage'); -const dogButton = document.getElementById('dogButton'); - -// example: 'run 2nd' will run last since it is async -console.log('run 1st') -fetch('https://dog.ceo/api/breeds/image/random') - .then(response => response.json) - .then(json => { - console.log(json.message) - dogImage.innerHTML = `` - }) +// const dogImage = document.getElementById('dogImage'); +// const dogButton = document.getElementById('dogButton'); + +// // example: 'run 2nd' will run last since it is async +// console.log('run 1st') +// fetch('https://dog.ceo/api/breeds/image/random') +// .then(response => response.json) +// .then(json => { +// console.log(json.message) +// dogImage.innerHTML = `` +// }) console.log('run 3rd'); console.log("hey"); @@ -507,4 +507,5 @@ console.log(randomNumber()); console.log("new new\n"); console.log("new new new\n"); -getHero(randomNumber()); \ No newline at end of file +getHero(randomNumber()); +console.log(typeof 123); \ No newline at end of file From ecfa161204e61e4d03b64362e8daf59f30f42559 Mon Sep 17 00:00:00 2001 From: ejsadiarin Date: Tue, 17 Jan 2023 23:26:27 +0800 Subject: [PATCH 30/31] random python 2 array loop in a formula --- looptwolistarray.py | 11 +++++++++++ nwepy.py | 11 +++++++++++ yourPlayground.js | 15 +++++++++++---- 3 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 looptwolistarray.py create mode 100644 nwepy.py diff --git a/looptwolistarray.py b/looptwolistarray.py new file mode 100644 index 0000000..6ec3eb1 --- /dev/null +++ b/looptwolistarray.py @@ -0,0 +1,11 @@ +import math +e = [1.718, 2.718, 3.718, 4.718, 5.718, 6.718, 7.718, 8.718, 9.718, 10.718,] +z = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] +g = [] + +for i in range(len(e)): + formula = 1 / (1 + math.pow(e[i], -z[i])) + g.append(formula) + +print(g) + diff --git a/nwepy.py b/nwepy.py new file mode 100644 index 0000000..6aa0f56 --- /dev/null +++ b/nwepy.py @@ -0,0 +1,11 @@ + +e = [1.718, 2.718, 3.718, 4.718, 5.718, 6.718, 7.718, 8.718, 9.718, 10.718,] +z = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] +g = [] + +for i in range(len(e)): + formula = 1 / (1 + e[i] ** -z[i]) + g.append(formula) + +print(g) + diff --git a/yourPlayground.js b/yourPlayground.js index 653218b..92aa2f8 100644 --- a/yourPlayground.js +++ b/yourPlayground.js @@ -114,14 +114,12 @@ console.log(person); * g(z) = 1 / (1 + e**-z) * g(z) should be in list form / object form {} that holds generic data types (just store results in another new array) */ -const e = [ - 1.718, 2.718, 3.718, 4.718, 5.718, 6.718, 7.718, 8.718, 9.718, 10.718, -]; +const e = [1.718, 2.718, 3.718, 4.718, 5.718, 6.718, 7.718, 8.718, 9.718, 10.718]; const z = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]; const result = (e, z) => { let resultsArr = []; for (let i = 0; i < e.length; i++) { - let formula = 1 / (1 + Math.pow(e[i], -z[i])); + let formula = 1 / (1 + Math.pow(e[i], -z[i])); // can do e[i] ** -z[i] for exponentiation resultsArr.push(formula); } console.log(resultsArr); @@ -129,6 +127,15 @@ const result = (e, z) => { result(e, z); +let g = []; +// only using loops +for (let i = 0; i < e.length; i++) { + let formula = 1 / (1 + Math.pow(e[i], -z[i])); + g.push(formula); +} +console.log(g) + + // ======= Create a function that uses ES6 arrow function with 2 arguments, object, and template literals ======= // // then store a function in the object, use 'this' keyword From 43cf19d3d8ce520d52bfe7d66bfce4bdad934874 Mon Sep 17 00:00:00 2001 From: ejsadiarin Date: Fri, 26 May 2023 12:03:26 +0800 Subject: [PATCH 31/31] finished some exercises, notes for self review --- yourPlayground.js | 361 ++++++++++++++++++++++------------------------ 1 file changed, 176 insertions(+), 185 deletions(-) diff --git a/yourPlayground.js b/yourPlayground.js index 92aa2f8..25ef6d7 100644 --- a/yourPlayground.js +++ b/yourPlayground.js @@ -3,34 +3,34 @@ const food = 200; const pricePercent = 0.2; const tip = food * pricePercent; console.log(tip); -// +// let input = "sentence"; const typeInput = typeof input; -// +// console.log(`input type is: ${typeInput}`); -// +// let inputInt = Number(input); console.log(typeof inputInt); console.log(inputInt); -// +// // Date let now = new Date().toDateString(); console.log(`The type of date: ${typeof now}`); console.log(now); function floorRandom() { - let random = Math.random() * 3; - let randomFloor = Math.floor(random); - return randomFloor; - // or console.log(randomFloor); - // and just call floorRandom(); + let random = Math.random() * 3; + let randomFloor = Math.floor(random); + return randomFloor; + // or console.log(randomFloor); + // and just call floorRandom(); } // floorRandom(); console.log(floorRandom()); -// +// // THE REST IS IN VSCODE // -// +// // Documentation Best Practices: -// +// /** * In this format (for functions) * @@ -84,10 +84,10 @@ console.log(letters.length); // key: value // key is also called as property const person = { - name: "Ej", // '' or "" are both fine - face: "handsome", - shirt: "green", - age: 18, + name: "Ej", // '' or "" are both fine + face: "handsome", + shirt: "green", + age: 18, }; // Access object property (key) // dot notation: @@ -114,15 +114,17 @@ console.log(person); * g(z) = 1 / (1 + e**-z) * g(z) should be in list form / object form {} that holds generic data types (just store results in another new array) */ -const e = [1.718, 2.718, 3.718, 4.718, 5.718, 6.718, 7.718, 8.718, 9.718, 10.718]; +const e = [ + 1.718, 2.718, 3.718, 4.718, 5.718, 6.718, 7.718, 8.718, 9.718, 10.718, +]; const z = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]; const result = (e, z) => { - let resultsArr = []; - for (let i = 0; i < e.length; i++) { - let formula = 1 / (1 + Math.pow(e[i], -z[i])); // can do e[i] ** -z[i] for exponentiation - resultsArr.push(formula); - } - console.log(resultsArr); + let resultsArr = []; + for (let i = 0; i < e.length; i++) { + let formula = 1 / (1 + Math.pow(e[i], -z[i])); // can do e[i] ** -z[i] for exponentiation + resultsArr.push(formula); + } + console.log(resultsArr); }; result(e, z); @@ -130,11 +132,10 @@ result(e, z); let g = []; // only using loops for (let i = 0; i < e.length; i++) { - let formula = 1 / (1 + Math.pow(e[i], -z[i])); - g.push(formula); + let formula = 1 / (1 + Math.pow(e[i], -z[i])); + g.push(formula); } -console.log(g) - +console.log(g); // ======= Create a function that uses ES6 arrow function with 2 arguments, object, and template literals ======= // // then store a function in the object, use 'this' keyword @@ -152,20 +153,20 @@ console.log(g) * when calling or returning a function in an object add parenthesis () for ex. personNew.networth() */ const createPerson = (theName, shirt) => { - const personNew = { - name: theName, - shirt: shirt, - face: "handsome", - assets: 1000000000, - liabilities: 700000, - networth: function () { - return this.assets - this.liabilities; - }, - }; - - return `${personNew.name} is the name and shirt is ${ - personNew.shirt - }, and face is ${personNew.face}. The networth is ${personNew.networth()}`; + const personNew = { + name: theName, + shirt: shirt, + face: "handsome", + assets: 1000000000, + liabilities: 700000, + networth: function () { + return this.assets - this.liabilities; + }, + }; + + return `${personNew.name} is the name and shirt is ${ + personNew.shirt + }, and face is ${personNew.face}. The networth is ${personNew.networth()}`; }; console.log(createPerson("Ej", "green")); @@ -176,17 +177,17 @@ const fruits = ["mango", "banana", "strawberry", "kiwi", "melon"]; // beautiful normal loop using i iteration for (let i = 0; i < fruits.length; i++) { - console.log(fruits[i]); + console.log(fruits[i]); } // beautiful shorter form of a for loop using 'of' keyword for (const fruit of fruits) { - console.log(`\nThe fruits are: ${fruit}`); + console.log(`\nThe fruits are: ${fruit}`); } // use 'in' keyword for loops and use .length property for total amount of characters const countLetters = () => { - let phrase = "hey how are you yes good yes yes ok wow"; - return phrase.length; + let phrase = "hey how are you yes good yes yes ok wow"; + return phrase.length; }; console.log(`The total letters are: ${countLetters()}`); @@ -197,20 +198,20 @@ console.log(`The total letters are: ${countLetters()}`); * both can be used when populating a new array */ const countLettersWithInKeyword = () => { - let phrase = "hey hey hey hye yesyesyeysys ok wow"; + let phrase = "hey hey hey hye yesyesyeysys ok wow"; - for (letter in phrase) { - console.log(letter); - } + for (letter in phrase) { + console.log(letter); + } }; countLettersWithInKeyword(); // prints 0 1 2 3 4 5 6 7 ... const countLettersWithOfKeyword = () => { - let phrase = "hey hey hey hye yesyesyeysys ok wow"; + let phrase = "hey hey hey hye yesyesyeysys ok wow"; - for (letter of phrase) { - console.log(letter); - } + for (letter of phrase) { + console.log(letter); + } }; countLettersWithOfKeyword(); // prints h e y h e y h e y h y e y e s y e s y ... @@ -218,8 +219,8 @@ const phraseResult = "hey hey ywo hey hey hey good wow ok wow"; let counter = 0; for (const index in phraseResult) { - console.log(parseInt(index) + 1); - counter = index; + console.log(parseInt(index) + 1); + counter = index; } console.log({ counter }); // returns result as an object { counter: '38' } @@ -234,18 +235,16 @@ const arrayNum = [1, 2, 3, 4, 5, 8, 12, 11, 7, 77, 70, 777]; * @returns {number} - the sum of array by mutating the result variable - adding the current result value + values in the array */ const sumOfArray = (arr) => { - let result = 0; - for (const num of arr) { - console.log(num); - result = result + num; - } - return { result }; // returns object - // can also do return result --> to return as a number + let result = 0; + for (const num of arr) { + console.log(num); + result = result + num; + } + return { result }; // returns object + // can also do return result --> to return as a number }; console.log(sumOfArray(arrayNum)); - - // ======= Max Function ======= // /** * @function max(array) @@ -255,39 +254,36 @@ console.log(sumOfArray(arrayNum)); * @returns {number} - the max number in an array */ const max = (array) => { - let result = 0; - for (let i = 0; i < array.length; i++) { - let currentNum = array[i]; - let nextNum = array[i + 1]; - if (currentNum < nextNum) { - result = nextNum; - } - if (currentNum > nextNum) { - result = currentNum; - } - } - return `The max number in the array is: ${result}`; + let result = 0; + for (let i = 0; i < array.length; i++) { + let currentNum = array[i]; + let nextNum = array[i + 1]; + if (currentNum < nextNum) { + result = nextNum; + } + if (currentNum > nextNum) { + result = currentNum; + } + } + return `The max number in the array is: ${result}`; }; console.log(max(arrayNum)); // another way of doing the max function const anotherMax = (numbers) => { - let result = numbers[0]; - for (const number of numbers) { - if (number > result) { - result = number; - } - } - - // return result; --> can be used if want to return pure number - return { result }; + let result = numbers[0]; + for (const number of numbers) { + if (number > result) { + result = number; + } + } + + // return result; --> can be used if want to return pure number + return { result }; }; // console.log(`anotherMax function result: ${anotherMax(arrayNum)}`); --> use if want to use return result above console.log(anotherMax(arrayNum)); // outputs object in console - - - /** * @function letterFrequency(phrase) * @@ -304,24 +300,25 @@ console.log(anotherMax(arrayNum)); // outputs object in console * } */ const letterFrequency = (phrase) => { - let frequency = {}; - for (const letter of phrase) { - // check if letter exists - if (letter in frequency) { - // if yes then increment++ - frequency[letter] += 1; - } else { - // otherwise set value to 1 - frequency[letter] = 1; - } - } - - return frequency; + let frequency = {}; + for (const letter of phrase) { + // check if letter exists + if (letter in frequency) { + // if yes then increment++ + frequency[letter] += 1; + } else { + // otherwise set value to 1 + frequency[letter] = 1; + } + } + + return frequency; }; -console.log(letterFrequency("Wow Ej Sadiarin you so good handsome everything code goooooood")); - - - +console.log( + letterFrequency( + "Wow Ej Sadiarin you so good handsome everything code goooooood" + ) +); // REAL WORLD EXAMPLE: GOOGLE SEARCH ENGINE // --> detecting the frequency of searched words (popular) @@ -330,72 +327,68 @@ console.log(letterFrequency("Wow Ej Sadiarin you so good handsome everything cod * * @param {string} phrase - a phrase or sentence * - * @method split() - seperates words, letters, etc. given an argument + * @method split() - seperates words, letters, etc. given an argument (returns new array) * @argument characters - in split() method * * @returns {object} - counts the certain specific words in a given phrase or sentence */ const examplePhrase = "hey hey wow good yes"; const wordFrequency = (phrase) => { - let frequency = {}; - // use split() method to seperate words - const splitPhrase = phrase.split(" "); - for (const word of splitPhrase) { - // check if word exist - if (word in frequency) { - frequency[word] += 1; - } else { - // if new word then set value to 1 - frequency[word] = 1; - } - } - return frequency; + let frequency = {}; + // use split() method to seperate words + const splitPhrase = phrase.split(" "); + for (const word of splitPhrase) { + // check if word exist + if (word in frequency) { + frequency[word] += 1; + } else { + // if new word then set value to 1 + frequency[word] = 1; + } + } + return frequency; }; console.log(wordFrequency(examplePhrase)); - /* ======= HIGHER ORDER FUNCTIONS ======= --> functions that takes in another function as argument and/or returns a function */ -// MAP, FILTER, REDUCE Array Methods +// MAP, FILTER, REDUCE Array Methods const nums = [1, 2, 3, 4, 7, 9, 5, 8]; /** * MAP @method map() - * - selects all values and + * - selects all values and * - changes the value of the whole given array * - works like loops but returns new array - * + * * @param callbackfn - * - * @returns {array} - new array + * + * @returns {array} - new array */ const mapFunc = (numArray) => { - return numArray.map(number => number * 2); -} + return numArray.map((number) => number * 2); +}; console.log(mapFunc(arrayNum)); - // MAP simple ver -console.log(nums.map(num => num * 2)); - - +console.log(nums.map((num) => num * 2)); // FILTER - filters out values from an array given a condition // - works like loops but returns new array -console.log(nums.filter(num => num > 5 || num === 5)); +console.log(nums.filter((num) => num > 5 || num === 5)); // works the same as this: const filter = (numbers, greaterThan) => { - let result = []; - for (const number of numbers) { - if (number > greaterThan) { - result.push(number); - } - } - return result; + let result = []; + for (const number of numbers) { + if (number > greaterThan) { + result.push(number); + } + } + return result; }; console.log(filter([1, 2, 3, 7, 4, 5], 3)); @@ -404,38 +397,37 @@ console.log(filter([1, 2, 3, 7, 4, 5], 3)); * reduces array to a single value * used in SUM * @returns one number / single value - */ + */ -// -console.log(nums.reduce(num => num * 2, 9)); +// +console.log(nums.reduce((num) => num * 2, 9)); // given an array called nums, use reduce() array method to return the product of the whole array -console.log(nums.reduce((a, b) => a * b)); +console.log(nums.reduce((a, b) => a * b)); // return sum of array using sum() function const sum = (a, b) => { - return a + b; -} + return a + b; +}; const resultNumsArray = nums.reduce(sum); console.log(resultNumsArray); console.log(sumOfArray(nums)); - const exampleObj = [ - {name: "personOne", money: 1000000}, - {name: "personTwo", money: 7080328}, - {name: "personThree", money: 891302777} -] + { name: "personOne", money: 1000000 }, + { name: "personTwo", money: 7080328 }, + { name: "personThree", money: 891302777 }, +]; let moneyp = exampleObj[1]; console.log(moneyp); // ======= given the array of objects above, get the sum of the money {number} ======= // const moneySum = (objectPerson) => { - let result = 0; - for (const person of objectPerson) { - result = result + person.money; - } - return result; -} + let result = 0; + for (const person of objectPerson) { + result = result + person.money; + } + return result; +}; console.log(moneySum(exampleObj)); // TODO: optimize above solution (use reduce()) @@ -443,19 +435,19 @@ console.log(moneySum(exampleObj)); // const moneySumReduce = (arr) => { // let result = 0; // for (const num in arr) { - + // } // } const func = () => { - let result = 0; - for (const num of nums) { - result = result + num; - } -} + let result = 0; + for (const num of nums) { + result = result + num; + } +}; // // ======= API ======= // // /** -// * +// * // */ // const dogImage = document.getElementById('dogImage'); // const dogButton = document.getElementById('dogButton'); @@ -469,21 +461,20 @@ const func = () => { // dogImage.innerHTML = `` // }) - console.log('run 3rd'); - console.log("hey"); +console.log("run 3rd"); +console.log("hey"); // Get a random number and set minimum value to 1 and max to 734 const minMax = (number) => { - let result = 0; - // reroll - if (number > 735 && number < 1) { - minMax(number); - } else { - result = number; - return result; - } - -} + let result = 0; + // reroll + if (number > 735 && number < 1) { + minMax(number); + } else { + result = number; + return result; + } +}; // @argument {number} 734 - defines the maximum value console.log(minMax(Math.floor(Math.random() * 735))); @@ -495,24 +486,24 @@ const accessToken = "127759136856495"; const baseURL = `https://superheroapi.com/api.php/${accessToken}`; const getHero = (id) => { - fetch(`${baseURL}/${id}`) - .then(response => response.json()) - .then(json => { - console.log(json); - }) -} + fetch(`${baseURL}/${id}`) + .then((response) => response.json()) + .then((json) => { + console.log(json); + }); +}; getHero(1); // Call getHero() with random number argument - create a function for the random number const randomNumber = () => { - const numberOfHeroes = 731; - return Math.floor(Math.random() * numberOfHeroes) + 1; -} + const numberOfHeroes = 731; + return Math.floor(Math.random() * numberOfHeroes) + 1; +}; console.log(randomNumber()); console.log("new new\n"); console.log("new new new\n"); getHero(randomNumber()); -console.log(typeof 123); \ No newline at end of file +console.log(typeof 123);