diff --git a/01/assets/js/app.js b/01/assets/js/app.js index 9323b98d..aaa0a2d6 100644 --- a/01/assets/js/app.js +++ b/01/assets/js/app.js @@ -1,4 +1,31 @@ -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 operations = ["+", "-", "*", "/", "%"]; + +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/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 + + + + + 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..24747cfb 100644 --- a/02/app.js +++ b/02/app.js @@ -1,7 +1,36 @@ - /* rozwiązanie z pętlą for */ -const x = 4; +const x = prompt("Type number: "); + +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; + +while (!a || isNaN(a)) { + a = Number(prompt("Podstawa: ")); +} + +while (!n || isNaN(n) || n <= 1) { + n = Number(prompt("Wykladnik:")); +} + +let counter = 0; +let text = ""; + +while (counter < n) { + text += a; + if (counter !== n - 1) text += " * "; + else text = text + " = " + Math.pow(a, n); + counter++; +} -/* rozwiązanie z pętlą while */ \ No newline at end of file +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 + + + + + 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); diff --git a/04/app.js b/04/app.js new file mode 100644 index 00000000..f21366f6 --- /dev/null +++ b/04/app.js @@ -0,0 +1,38 @@ +function createArray(min, max) { + 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 = []; + + 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 + + + + + diff --git a/05/app.js b/05/app.js new file mode 100644 index 00000000..2b6b09da --- /dev/null +++ b/05/app.js @@ -0,0 +1,38 @@ +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) => { + 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(); + 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 + + + + +