From 8913f240baa5e3803d493c6d1a59e2bb57924822 Mon Sep 17 00:00:00 2001 From: Bartosz Dudziak Date: Sun, 22 Dec 2024 10:52:36 +0100 Subject: [PATCH 1/6] Task 01 completed --- 01/assets/js/app.js | 29 +++++++++++++++++++++++++++-- 01/index.html | 18 +++++++++--------- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/01/assets/js/app.js b/01/assets/js/app.js index 9323b98d..572230ad 100644 --- a/01/assets/js/app.js +++ b/01/assets/js/app.js @@ -1,4 +1,29 @@ -const a = '4.2'; +const a = "4.2"; const b = 9; -console.log(a, b); \ No newline at end of file +console.log(a, b); + +console.log("Typem zmienne b jest:", typeof b); +console.log("Typem zmienne a jest:", typeof a); + +const result1 = Number(a) + Number(b); +const result2 = Number(a) - Number(b); +const result3 = Number(a) * Number(b); +const result4 = Number(a) / Number(b); +const result5 = Number(a) % Number(b); + +console.log( + result1 > 20 ? "Result1 is greater than 20" : "Result1 is less than 20" +); +console.log( + result2 > 20 ? "Result2 is greater than 20" : "Result2 is less than 20" +); +console.log( + result3 > 20 ? "Result3 is greater than 20" : "Result3 is less than 20" +); +console.log( + result4 > 20 ? "Result4 is greater than 20" : "Result4 is less than 20" +); +console.log( + result5 > 20 ? "Result5 is greater than 20" : "Result5 is less than 20" +); diff --git a/01/index.html b/01/index.html index 5c55d687..fa2fad9d 100644 --- a/01/index.html +++ b/01/index.html @@ -1,12 +1,12 @@ - - - - + + + + devmentor.pl - JS BASICS - #01 - - - - - \ No newline at end of file + + + + + From 406f4e6244f5938b668d4491c2664f779ee38c78 Mon Sep 17 00:00:00 2001 From: Bartosz Dudziak Date: Sun, 22 Dec 2024 13:21:27 +0100 Subject: [PATCH 2/6] Task 02 completed --- 02/README.md | 12 ++++++------ 02/app.js | 32 +++++++++++++++++++++++++++++--- 02/index.html | 18 +++++++++--------- 3 files changed, 44 insertions(+), 18 deletions(-) diff --git a/02/README.md b/02/README.md index 06f58c96..e9c11316 100644 --- a/02/README.md +++ b/02/README.md @@ -1,10 +1,9 @@ -> :white_check_mark: *Jeśli będziesz mieć problem z rozwiązaniem tego zadania, poproś o pomoc na odpowiednim kanale na Slacku, tj. `s1e04-js-basics` (dotyczy [mentee](https://devmentor.pl/mentoring-javascript/)) lub na ogólnodostępnej i bezpłatnej [społeczności na Discordzie](https://devmentor.pl/discord). Pamiętaj, aby treść Twojego wpisu spełniała [odpowiednie kryteria](https://devmentor.pl/jak-prosic-o-pomoc/).* +> :white_check_mark: _Jeśli będziesz mieć problem z rozwiązaniem tego zadania, poproś o pomoc na odpowiednim kanale na Slacku, tj. `s1e04-js-basics` (dotyczy [mentee](https://devmentor.pl/mentoring-javascript/)) lub na ogólnodostępnej i bezpłatnej [społeczności na Discordzie](https://devmentor.pl/discord). Pamiętaj, aby treść Twojego wpisu spełniała [odpowiednie kryteria](https://devmentor.pl/jak-prosic-o-pomoc/)._   # `#02` JavaScript: Podstawy - Tym razem wykorzystasz dwa rodzaje pętli. ## Pętla for @@ -12,6 +11,7 @@ Tym razem wykorzystasz dwa rodzaje pętli. Twój kolega ma syna w podstawówce i właśnie uczy go tabliczki mnożenia. Zaoferowałeś pomoc w formie programu, który pokaże wszystkie działania mnożenia dla wybranej liczby. Za pomocą pętli `for` stwórz rozwiązanie, które będzie wyświetlać kolejne wyniki mnożenia. + - Pierwszym czynnikiem niech będzie wartość zmiennej `x`, np. `const x = 4`. - W miejsce drugiego czynnika wstaw kolejne cyfry od 1 do 9. @@ -31,14 +31,14 @@ Zwróć uwagę, że najpierw tworzymy rozwiązanie dla konkretnego przypadku, a ## Pętla while -Pomyślałeś, że przy okazji stworzysz program do nauki potęgowania. +Pomyślałeś, że przy okazji stworzysz program do nauki potęgowania. Niech działa on następująco: jeśli `a = 3` (podstawa) oraz `n = 4` (wykładnik) to w konsoli wyświetla się `3 * 3 * 3 * 3 = 81` W tym rozwiązaniu również możesz użyć `prompt()` i sprawdzać, czy użytkownik wprowadza odpowiednie dane. -   -> :no_entry: *Jeśli nie posiadasz materiałów do tego zadania tj. **PDF + wideo, projekt + Code Review**, znajdziesz je na stronie [devmentor.pl](https://devmentor.pl/workshop-js-basics/)* -> :arrow_left: [*poprzednie zadanie*](./../01) | [*następne zadanie*](./../03) :arrow_right: +> :no_entry: _Jeśli nie posiadasz materiałów do tego zadania tj. **PDF + wideo, projekt + Code Review**, znajdziesz je na stronie [devmentor.pl](https://devmentor.pl/workshop-js-basics/)_ + +> :arrow_left: [_poprzednie zadanie_](./../01) | [_następne zadanie_](./../03) :arrow_right: diff --git a/02/app.js b/02/app.js index b397190c..273fa4ec 100644 --- a/02/app.js +++ b/02/app.js @@ -1,7 +1,33 @@ - /* rozwiązanie z pętlą for */ -const x = 4; +const x = prompt("Type number: "); + +if (x) { + for (let i = 1; i <= 9; i++) { + console.log(`${x} x ${i} = ${Number(x) * i}`); + } +} + +/* rozwiązanie z pętlą while */ +let a; +let n; + +let counter = 0; +let text = ""; + +while (!a) { + a = Number(prompt("Podstawa: ")); + + while (!n || n <= 1) { + n = Number(prompt("Wykladnik:")); + while (counter < n) { + text += a; + if (counter !== n - 1) text += " * "; + else text = text + " = " + Math.pow(a, n); -/* rozwiązanie z pętlą while */ \ No newline at end of file + counter++; + } + } +} +console.log(text); diff --git a/02/index.html b/02/index.html index 30150099..ca814da1 100644 --- a/02/index.html +++ b/02/index.html @@ -1,12 +1,12 @@ - - - - + + + + devmentor.pl - JS BASICS - #02 - - - - - \ No newline at end of file + + + + + From f1aa4f8ab31fb8b256002ac3dec050162b4ff4e2 Mon Sep 17 00:00:00 2001 From: Bartosz Dudziak Date: Sun, 22 Dec 2024 14:10:34 +0100 Subject: [PATCH 3/6] Task 03 completed --- 03/app.js | 44 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/03/app.js b/03/app.js index 5b9361e4..1f6cbc82 100644 --- a/03/app.js +++ b/03/app.js @@ -7,12 +7,40 @@ const c = randomNumber(min, max); console.log(a, b, c); - - - - - - function randomNumber(min, max) { - return Math.round((Math.random() * (max - min)) + min); -} \ No newline at end of file + return Math.round(Math.random() * (max - min) + min); +} + +function getSum(x, y, z) { + const numbers = [x, y, z] + .map((number) => parseInt(number)) + .sort((a, b) => b - a); + + return numbers.at(0) + numbers.at(1); +} + +const isEven = function (number) { + if (isNaN(number)) return null; + + return number % 2 === 0; +}; + +function showInfo(arg1, arg2) { + if (!(typeof arg2 === "boolean" || arg2 === null)) return; + + switch (arg2) { + case null: + console.log(`Podany argument ${arg1} nie jest liczbą`); + case true: + console.log(`Podany argument ${arg1} jest parzysty`); + break; + case false: + console.log(`Podany argument ${arg1} jest nieparzysty`); + break; + } +} + +const sum = getSum(a, b, c); +const even = isEven(sum); + +showInfo(sum, even); From 1c0fb70b4d6ad332ae3832015957183ee1449114 Mon Sep 17 00:00:00 2001 From: Bartosz Dudziak Date: Sun, 22 Dec 2024 14:41:09 +0100 Subject: [PATCH 4/6] Task 04 completed --- 04/app.js | 28 ++++++++++++++++++++++++++++ 04/index.html | 18 +++++++++--------- 2 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 04/app.js diff --git a/04/app.js b/04/app.js new file mode 100644 index 00000000..b13999f0 --- /dev/null +++ b/04/app.js @@ -0,0 +1,28 @@ +function createArray(min, max) { + if (typeof min !== "number" || typeof max !== "number") + throw new Error("min and max must be a number"); + + const array = []; + + for (let i = 0; i < 20; i++) { + const randomNumber = Math.random() * (max - min) + min; + array.push(Math.round(randomNumber)); + } + + return array; +} + +function getLargest(array) { + return array.sort((a, b) => b - a).slice(0, 10); +} + +function getAvg(array) { + const sum = array.reduce((acc, cur) => acc + cur, 0); + return sum / array.length; +} + +const arr = createArray(10, 200); +const largest = getLargest(arr); +const avg = getAvg(largest); + +console.log(avg); diff --git a/04/index.html b/04/index.html index 06aebbb8..ff7afe86 100644 --- a/04/index.html +++ b/04/index.html @@ -1,12 +1,12 @@ - - - - + + + + devmentor.pl - JS BASICS - #04 - - - - - \ No newline at end of file + + + + + From 28a89d3f134abd44bd38b8b08bf6faa37cfd0817 Mon Sep 17 00:00:00 2001 From: Bartosz Dudziak Date: Sun, 22 Dec 2024 20:48:24 +0100 Subject: [PATCH 5/6] Task 05 completed --- 05/app.js | 36 ++++++++++++++++++++++++++++++++++++ 05/index.html | 18 +++++++++--------- 2 files changed, 45 insertions(+), 9 deletions(-) create mode 100644 05/app.js diff --git a/05/app.js b/05/app.js new file mode 100644 index 00000000..4cd5147e --- /dev/null +++ b/05/app.js @@ -0,0 +1,36 @@ +function Student(firstName, lastName) { + this.firstName = firstName; + this.lastName = lastName; + this.grades = {}; +} + +Student.prototype.addGrade = function (subject, grade) { + if (typeof this.grades[subject] === "undefined") { + this.grades[subject] = []; + } + + this.grades[subject].push(grade); +}; + +Student.prototype.getAverageGrade = function (subject) { + const calcAverage = (grades) => + grades.reduce((acc, cur) => acc + cur, 0) / grades.length; + + if (typeof subject === "undefined") { + const grades = Object.values(this.grades).flat(); + return Number(calcAverage(grades).toFixed(2)); + } + + return calcAverage(this.grades[subject]); +}; + +const student = new Student("Jan", "Kowalski"); +student.addGrade("maths", 4); +student.addGrade("maths", 6); +student.addGrade("english", 3); +const avgMath = student.getAverageGrade("maths"); +const avg = student.getAverageGrade(); + +console.log(student); +console.log(avgMath); +console.log(avg); diff --git a/05/index.html b/05/index.html index 52a42709..fd3cfe87 100644 --- a/05/index.html +++ b/05/index.html @@ -1,12 +1,12 @@ - - - - + + + + devmentor.pl - JS BASICS - #05 - - - - - \ No newline at end of file + + + + + From 0eaad033d9aa943edf648c7dbc37375f301a30fc Mon Sep 17 00:00:00 2001 From: Bartosz Dudziak Date: Thu, 26 Dec 2024 19:58:59 +0100 Subject: [PATCH 6/6] Bugs fixed --- 01/assets/js/app.js | 42 ++++++++++++++++++++++-------------------- 02/app.js | 31 +++++++++++++++++-------------- 04/app.js | 14 ++++++++++++-- 05/app.js | 6 ++++-- 4 files changed, 55 insertions(+), 38 deletions(-) diff --git a/01/assets/js/app.js b/01/assets/js/app.js index 572230ad..aaa0a2d6 100644 --- a/01/assets/js/app.js +++ b/01/assets/js/app.js @@ -6,24 +6,26 @@ console.log(a, b); console.log("Typem zmienne b jest:", typeof b); console.log("Typem zmienne a jest:", typeof a); -const result1 = Number(a) + Number(b); -const result2 = Number(a) - Number(b); -const result3 = Number(a) * Number(b); -const result4 = Number(a) / Number(b); -const result5 = Number(a) % Number(b); +const operations = ["+", "-", "*", "/", "%"]; -console.log( - result1 > 20 ? "Result1 is greater than 20" : "Result1 is less than 20" -); -console.log( - result2 > 20 ? "Result2 is greater than 20" : "Result2 is less than 20" -); -console.log( - result3 > 20 ? "Result3 is greater than 20" : "Result3 is less than 20" -); -console.log( - result4 > 20 ? "Result4 is greater than 20" : "Result4 is less than 20" -); -console.log( - result5 > 20 ? "Result5 is greater than 20" : "Result5 is less than 20" -); +const results = operations.map((operation) => { + switch (operation) { + case "+": + return Number(a) + Number(b); + case "-": + return Number(a) - Number(b); + case "*": + return Number(a) * Number(b); + case "/": + return Number(a) / Number(b); + case "%": + return Number(a) % Number(b); + } +}); + +results.forEach((result, index) => { + const operation = operations.at(index); + console.log( + `${a} ${operation} ${b} is ${result > 20 ? "greater" : "less"} than 20` + ); +}); diff --git a/02/app.js b/02/app.js index 273fa4ec..24747cfb 100644 --- a/02/app.js +++ b/02/app.js @@ -1,33 +1,36 @@ /* rozwiązanie z pętlą for */ const x = prompt("Type number: "); -if (x) { +if (x && !isNaN(x)) { for (let i = 1; i <= 9; i++) { console.log(`${x} x ${i} = ${Number(x) * i}`); } +} else { + console.error("Invalid input"); } /* rozwiązanie z pętlą while */ let a; let n; -let counter = 0; -let text = ""; - -while (!a) { +while (!a || isNaN(a)) { a = Number(prompt("Podstawa: ")); +} + +while (!n || isNaN(n) || n <= 1) { + n = Number(prompt("Wykladnik:")); +} - while (!n || n <= 1) { - n = Number(prompt("Wykladnik:")); +let counter = 0; +let text = ""; - while (counter < n) { - text += a; +while (counter < n) { + text += a; - if (counter !== n - 1) text += " * "; - else text = text + " = " + Math.pow(a, n); + if (counter !== n - 1) text += " * "; + else text = text + " = " + Math.pow(a, n); - counter++; - } - } + counter++; } + console.log(text); diff --git a/04/app.js b/04/app.js index b13999f0..f21366f6 100644 --- a/04/app.js +++ b/04/app.js @@ -1,6 +1,16 @@ function createArray(min, max) { - if (typeof min !== "number" || typeof max !== "number") - throw new Error("min and max must be a number"); + if ( + typeof min !== "number" || + typeof max !== "number" || + isNaN(min) || + isNaN(max) + ) { + throw new Error("min and max must be valid numbers."); + } + + if (min > max) { + throw new Error("min must be less than or equal to max."); + } const array = []; diff --git a/05/app.js b/05/app.js index 4cd5147e..2b6b09da 100644 --- a/05/app.js +++ b/05/app.js @@ -13,8 +13,10 @@ Student.prototype.addGrade = function (subject, grade) { }; Student.prototype.getAverageGrade = function (subject) { - const calcAverage = (grades) => - grades.reduce((acc, cur) => acc + cur, 0) / grades.length; + const calcAverage = (grades) => { + if (!grades || grades.length === 0) throw new Error("No grades found!"); + return grades.reduce((acc, cur) => acc + cur, 0) / grades.length; + }; if (typeof subject === "undefined") { const grades = Object.values(this.grades).flat();