From 6972bb1d19c42c2989dce2bd8ca3c81ba72382f9 Mon Sep 17 00:00:00 2001 From: Michal Wasiak Date: Sun, 15 Jun 2025 21:04:59 +1000 Subject: [PATCH 1/5] Task 01 done --- 01/assets/js/app.js | 80 +++++++++++++++++++++++++++++++++++++++++++-- 01/index.html | 2 +- 2 files changed, 79 insertions(+), 3 deletions(-) diff --git a/01/assets/js/app.js b/01/assets/js/app.js index 9323b98d..25f9ac39 100644 --- a/01/assets/js/app.js +++ b/01/assets/js/app.js @@ -1,4 +1,80 @@ const a = '4.2'; -const b = 9; +const b = 20; +const c = parseFloat(a); + +console.log(a, b); +console.log(typeof a); +console.log(typeof b); + +const addVariable = c + b; +console.log(addVariable); + +const concatVariable = a + b; +console.log(concatVariable); + +const subtractVariable = a - b; +console.log(subtractVariable); + +const multiVariable = a * b; +console.log(multiVariable); + +const divideVariable = a / b; +console.log(divideVariable); + +const modVariable = Number(a) % b; +console.log(modVariable); + +let incrementVar = a; +incrementVar++; +console.log(incrementVar); + +let decrementVar = b; +decrementVar--; +console.log(decrementVar); + + +if (addVariable > 20) { + console.log('a+b jest wieksze od 20') +} else { + console.log('a+b jest mniejsze od 20') +} + +if (subtractVariable > 20) { + console.log('a-b jest wieksze od 20') +} else { + console.log('a-b jest mniejsze od 20') +} + +if (multiVariable > 20) { + console.log('a*b jest wieksze od 20') +} else { + console.log('a*b jest mniejsze od 20') +} + +if (divideVariable > 20) { + console.log('a/b jest wieksze od 20') +} else { + console.log('a/b jest mniejsze od 20') +} + +if (modVariable > 20) { + console.log('a%b jest wieksze od 20') +} else { + console.log('a%b jest mniejsze od 20') +} + +if (incrementVar > 20) { + console.log('a++ jest wieksze od 20') +} else { + console.log('a++ jest mniejsze od 20') +} + +if (decrementVar > 20) { + console.log('a-- jest wieksze od 20') +} else { + console.log('a-- jest mniejsze od 20') +} + +console.log(); +console.log(); -console.log(a, b); \ No newline at end of file diff --git a/01/index.html b/01/index.html index 5c55d687..b014de3a 100644 --- a/01/index.html +++ b/01/index.html @@ -7,6 +7,6 @@ devmentor.pl - JS BASICS - #01 - + , \ No newline at end of file From fab43a45b285279634a4f0143f67eada375ba78d Mon Sep 17 00:00:00 2001 From: Michal Wasiak Date: Thu, 19 Jun 2025 13:03:11 +1000 Subject: [PATCH 2/5] Task 03 done --- 03/app.js | 94 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 85 insertions(+), 9 deletions(-) diff --git a/03/app.js b/03/app.js index 5b9361e4..b8a27a2c 100644 --- a/03/app.js +++ b/03/app.js @@ -1,18 +1,94 @@ -const min = 1; -const max = 100; +// const min = 1; +// const max = 100; -const a = randomNumber(min, max); -const b = randomNumber(min, max); -const c = randomNumber(min, max); +// const a = randomNumber(min, max); +// const b = randomNumber(min, max); +// const c = randomNumber(min, max); -console.log(a, b, c); +// console.log(a, b, c); +// function randomNumber(min, max) { +// return Math.round((Math.random() * (max - min)) + min); +// } +/* +Funkcja o nazwie getSum: +-przyjmuje 3 parametry. Ich wartość w ciele funkcji ma zostać przekonwertowana na liczby całkowite, +-z podanych 3 liczb wybierane są 2 największe, +-wartości tych 2 liczb są sumowane, a wynik jest zwracany przez funkcję. +*/ +const a = 41.5234; +const b = 25; +const c = 14; +const sum = getSum(a, b, c); +// zapisac w tablicy i posortowac +function getSum(a, b, c) { + let largestNum = Math.max(a, b, c); + let secondLargestNum; + if (largestNum === a) { + secondLargestNum = Math.max(b, c); + } else if (largestNum === b) { + secondLargestNum = Math.max(a, c); + } else { + secondLargestNum = Math.max(a, b); + } + return sum1 = parseInt(largestNum) + parseInt(secondLargestNum); +} -function randomNumber(min, max) { - return Math.round((Math.random() * (max - min)) + min); -} \ No newline at end of file +// console.log(sum1) + +/* +Funkcja o nazwie isEven: +-ma formę wyrażenia funkcyjnego, +-przyjmuje przez parametr tylko liczbę: + -jeśli parametrem jest inny typ wartości niż number, to zwróć null, + -jeśli jest to liczba, to zwróć: + -true - jeśli jest parzysta, + -false - jeśli jest nieparzysta. +*/ + + +const even = isEven(2); + +function isEven(num) { + if (typeof num !== "number") { + return null; + } else if (num % 2 === 0) { + return true; + } else { + return false; + } +} +// console.log(even); + +/* +Funkcja o nazwie showInfo: + +-przyjmuje 2 parametry: + pierwszy to dowolna wartość, + drugi to jedna z podanych wartości: null, true, false, +-wyświetla w konsoli podane niżej informacje i wykorzystuje do tego strukturę switch. Jeśli drugi parametr to: + null => Podany argument [tutaj jej wartość] nie jest liczbą + true => Podany argument [tutaj jej wartość] jest parzysty + false => Podany argument [tutaj jej wartość] jest nieparzysty +*/ + +function showInfo(para1, para2) { + switch (para2) { + case null: + console.log('Podany argument ' + para1 + ' nie jest liczbą'); + break; + case true: + console.log('Podany argument ' + para1 + ' jest parzystybą'); + break; + case false: + console.log('Podany argument ' + para1 + ' jest nieparzysty'); + break; + } +} + +showInfo(sum, even); \ No newline at end of file From 06ed1fda4b329b306f02b4fcf59a9dbc8edb967f Mon Sep 17 00:00:00 2001 From: Michal Wasiak Date: Thu, 19 Jun 2025 13:32:59 +1000 Subject: [PATCH 3/5] Task 04 done --- 04/app.js | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++ 04/index.html | 15 ++++++++-- 2 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 04/app.js diff --git a/04/app.js b/04/app.js new file mode 100644 index 00000000..b9e9e55f --- /dev/null +++ b/04/app.js @@ -0,0 +1,77 @@ +// 1. Napisz funkcję, która utworzy i zwróci tablicę z 20 elementami wylosowanymi z podanego zakresu. Zakres mają określać dwa parametry: +// - liczba minimalna, +// - liczba maksymalna. + +// W ciele tej funkcji wykorzystaj pętlę `for` oraz obiekt `Math`. + +// Poniżej przedstawiam przykładowe działanie takiej funkcji: +// ``` +// const arr = createArray(1, 100); +// console.log(arr); // [1, 4, 34, 12, 16, 45, 12, 38, 78, 99, 1, 84, 44, 61, 16, 45, 33, 31, 68, 93 ] + + +function createArray(min, max) { + const arr = []; + for (let i = 1; i <= 20; i++) { + const randomNumber = Math.floor(Math.random() * (max - min + 1)) + min; + arr.push(randomNumber); + } + return arr; +} + +// const arr = createArray(0, 100); +// console.log(arr); +const arr = createArray(10, 200); +console.log(arr); + +// 2. Napisz kolejną funkcję, która posortuje liczby z otrzymanej powyżej tablicy od wartości największej do najmniejszej i zwróci tablicę zawierającą tylko 10 największych liczb. W ciele funkcji możesz użyć metod `.sort()` oraz `.slice()`. + +// Poniżej przedstawiam przykładowe działanie takiej funkcji: +// ``` +// const largest = getLargest(arr); +// console.log(largest); // [99, 93, 84, 78, 68, 61, 45, 45, 44, 38] +// ``` + +// arr.sort(function (a, b) { +// return b - a; +// }); +// console.log(arr); + +// console.log(arr.slice(10)); + +function getLargest() { + arr.sort(function (a, b) { + return b - a; + }); + return arr.slice(0, 10); + +} +const largest = getLargest(arr); +console.log(largest); + +// 3. Ostatnia funkcja ma obliczać [średnią arytmetyczną](https://pl.wikipedia.org/wiki/%C5%9Arednia_arytmetyczna) z liczb przechowyanych w przekazanej przez parametr tablicy. Możesz do tego wykorzystać `.forEach()` lub `.reduce()`. + +// Przykładowe działanie takiej funkcji to: +// ``` +// const avg = getAvg([1, 2, 3, 4, 5]); +// console.log(avg); // 3 +// ``` + +function getAvg(arr) { + // return arr.reduce((total, arr) => (total + arr), 0)/arr.length; + let sum = 0; + arr.forEach(num => { + sum += num; + + }); + return sum / arr.length; + +} + +const avg = getAvg(largest); +console.log(avg); + +// 4. Wykorzystaj napisane funkcje w taki sposób, aby utworzyć tablicę składającą się z 20 losowych liczb z przedziału od 10 do 200. Z utworzonej tablicy wybiesz 10 największych i obliczych ich średnią arytmetyczną. + +// Zadbaj o odpowiednie nazwy dla funkcji - nie muszą być one identyczne jak w przykładach. + diff --git a/04/index.html b/04/index.html index 06aebbb8..961a6455 100644 --- a/04/index.html +++ b/04/index.html @@ -1,12 +1,21 @@ + - - + + devmentor.pl - JS BASICS - #04 + - + + \ No newline at end of file From e711ffdc5ff257773ce792781d8222ad2aae1da6 Mon Sep 17 00:00:00 2001 From: Michal Wasiak Date: Sun, 22 Jun 2025 13:48:30 +1000 Subject: [PATCH 4/5] Task 05 done --- 02/app.js | 33 +++++++++++++++++++++-- 02/index.html | 4 +-- 05/app.js | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++ 05/index.html | 15 ++++++++--- 4 files changed, 117 insertions(+), 7 deletions(-) create mode 100644 05/app.js diff --git a/02/app.js b/02/app.js index b397190c..765fb2f0 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('Podaj liczbe z przedzialu od 1 do 9!'); +let result; +for (let i = 1; i <= 9; i++) { + if (x <= 9 && x > 0) { + result = i * x; + console.log(x + ' x ' + i + ' = ' + result); + } else { + alert('Wybrana liczba nie jest z przedzialu 1-9'); -/* rozwiązanie z pętlą while */ \ No newline at end of file + } +} + +/* rozwiązanie z pętlą while */ + +const a = parseInt(prompt('Podaj liczbe z przedzialu od 1 do 9')); +const n = parseInt(prompt('Podaj liczbe z przedzialu od 1 do 9')); +let result2 = 1 +// console.log(result) + + +let j = 0; + +while (j <= n) { + if (a <= 9 && a > 0 && n <= 9 && n > 0) { + result2 = Math.pow(a, n); + j++; + } else { + alert('Wybrana liczba nie jest z przedzialu 1-9'); + } +} + +console.log('Wynik ' + a + ' do potegi ' + n + ' = ' + result2); diff --git a/02/index.html b/02/index.html index 30150099..12794631 100644 --- a/02/index.html +++ b/02/index.html @@ -7,6 +7,6 @@ devmentor.pl - JS BASICS - #02 - - + + \ No newline at end of file diff --git a/05/app.js b/05/app.js new file mode 100644 index 00000000..eff39d49 --- /dev/null +++ b/05/app.js @@ -0,0 +1,72 @@ +// Tym razem stworzysz konstruktor, na podstawie którego będzie można generować obiekt przechowujący informacje o studencie. + +// Wymagania: +// 1. Obiekt posiada imię i nazwisko przekazywane podczas inicjalizacji (przy użyciu `new`). + +function Student(initFirstName, initLastName) { + this.firstName = initFirstName; + this.lastName = initLastName; + this.grades = {}; +} + + +Student.prototype.addGrade = function (subject, grade) { + if (typeof this.grades[subject] === 'undefined') { + this.grades[subject] = []; + } + this.grades[subject].push(grade); +}; + +// 2. Konstruktor: +// - udostępnia metody (przez `prototype`), +// - metody te pozwalają: +// - dodać ocenę z danego przedmiotu, +// - obliczyć średnią arytmetyczną z konkretnego przedmiotu, +// - obliczyć ogólną średnią arytmetyczną. + +Student.prototype.getAverageGrade = function (subject) { + if (typeof subject !== 'undefine') { + const grades = this.grades[subject]; + if (!grades || grades.length === 0) return null; + let sum = 0; + for (let i = 0; i < grades.length; i++) { + sum += grades[i]; + } + console.log(sum / grades.length); + } + + let totalSum = 0; + let totalCount = 0; + + for (let subjectName in this.grades) { + const subjectGrades = this.grades[subjectName]; + + for (let i = 0; i < subjectGrades.length; i++) { + totalSum += subjectGrades[i]; + totalCount++; + } + } + if (totalCount === 0) return null; + + console.log((totalSum / totalCount).toFixed(2)); +} + +const maths = 4; + +const student = new Student('Jan', 'Kowalski'); +console.log(student) +student.addGrade('maths', 4); +student.addGrade('maths', 6); +student.addGrade('english', 3); +const avgMath = student.getAverageGrade('maths'); // 5 +const avg = student.getAverageGrade(); // 4.33 + + + + +// Zwróć uwagę, że pobranie informacji o ocenach z konkretnego przedmiotu może się odbywać w ten sposób: +// ``` +// const subject = 'maths'; +// const grades = this.grades[subject]; +// ``` + diff --git a/05/index.html b/05/index.html index 52a42709..996c6de0 100644 --- a/05/index.html +++ b/05/index.html @@ -1,12 +1,21 @@ + - - + + devmentor.pl - JS BASICS - #05 + - + + \ No newline at end of file From 7350985cc53c0cb17da0ef0d6b88ae8cb0e35c9f Mon Sep 17 00:00:00 2001 From: Michal Wasiak Date: Thu, 3 Jul 2025 21:35:46 +1000 Subject: [PATCH 5/5] All task done --- 02/app.js | 20 +++++++++++++------- 03/app.js | 46 ---------------------------------------------- 04/app.js | 40 +++------------------------------------- 05/app.js | 14 ++++++++------ 4 files changed, 24 insertions(+), 96 deletions(-) diff --git a/02/app.js b/02/app.js index 765fb2f0..f0116eea 100644 --- a/02/app.js +++ b/02/app.js @@ -23,14 +23,20 @@ let result2 = 1 let j = 0; - -while (j <= n) { - if (a <= 9 && a > 0 && n <= 9 && n > 0) { - result2 = Math.pow(a, n); +let str = ''; +if (a <= 9 && a > 0 && n <= 9 && n > 0) { + while (j < n) { j++; - } else { - alert('Wybrana liczba nie jest z przedzialu 1-9'); + if (n === j) { + str += a; + } else { + str += a + '*'; + } } + console.log(str + '=' + Math.pow(a, n)); } +else { + alert('Wybrana liczba nie jest z przedzialu 1-9'); +} + -console.log('Wynik ' + a + ' do potegi ' + n + ' = ' + result2); diff --git a/03/app.js b/03/app.js index b8a27a2c..280d99b7 100644 --- a/03/app.js +++ b/03/app.js @@ -1,29 +1,9 @@ -// const min = 1; -// const max = 100; - -// const a = randomNumber(min, max); -// const b = randomNumber(min, max); -// const c = randomNumber(min, max); - -// console.log(a, b, c); -// function randomNumber(min, max) { -// return Math.round((Math.random() * (max - min)) + min); -// } - -/* -Funkcja o nazwie getSum: --przyjmuje 3 parametry. Ich wartość w ciele funkcji ma zostać przekonwertowana na liczby całkowite, --z podanych 3 liczb wybierane są 2 największe, --wartości tych 2 liczb są sumowane, a wynik jest zwracany przez funkcję. -*/ - const a = 41.5234; const b = 25; const c = 14; const sum = getSum(a, b, c); -// zapisac w tablicy i posortowac function getSum(a, b, c) { let largestNum = Math.max(a, b, c); @@ -39,19 +19,6 @@ function getSum(a, b, c) { return sum1 = parseInt(largestNum) + parseInt(secondLargestNum); } -// console.log(sum1) - -/* -Funkcja o nazwie isEven: --ma formę wyrażenia funkcyjnego, --przyjmuje przez parametr tylko liczbę: - -jeśli parametrem jest inny typ wartości niż number, to zwróć null, - -jeśli jest to liczba, to zwróć: - -true - jeśli jest parzysta, - -false - jeśli jest nieparzysta. -*/ - - const even = isEven(2); function isEven(num) { @@ -63,19 +30,6 @@ function isEven(num) { return false; } } -// console.log(even); - -/* -Funkcja o nazwie showInfo: - --przyjmuje 2 parametry: - pierwszy to dowolna wartość, - drugi to jedna z podanych wartości: null, true, false, --wyświetla w konsoli podane niżej informacje i wykorzystuje do tego strukturę switch. Jeśli drugi parametr to: - null => Podany argument [tutaj jej wartość] nie jest liczbą - true => Podany argument [tutaj jej wartość] jest parzysty - false => Podany argument [tutaj jej wartość] jest nieparzysty -*/ function showInfo(para1, para2) { switch (para2) { diff --git a/04/app.js b/04/app.js index b9e9e55f..9c14302e 100644 --- a/04/app.js +++ b/04/app.js @@ -1,15 +1,3 @@ -// 1. Napisz funkcję, która utworzy i zwróci tablicę z 20 elementami wylosowanymi z podanego zakresu. Zakres mają określać dwa parametry: -// - liczba minimalna, -// - liczba maksymalna. - -// W ciele tej funkcji wykorzystaj pętlę `for` oraz obiekt `Math`. - -// Poniżej przedstawiam przykładowe działanie takiej funkcji: -// ``` -// const arr = createArray(1, 100); -// console.log(arr); // [1, 4, 34, 12, 16, 45, 12, 38, 78, 99, 1, 84, 44, 61, 16, 45, 33, 31, 68, 93 ] - - function createArray(min, max) { const arr = []; for (let i = 1; i <= 20; i++) { @@ -19,25 +7,10 @@ function createArray(min, max) { return arr; } -// const arr = createArray(0, 100); -// console.log(arr); const arr = createArray(10, 200); console.log(arr); -// 2. Napisz kolejną funkcję, która posortuje liczby z otrzymanej powyżej tablicy od wartości największej do najmniejszej i zwróci tablicę zawierającą tylko 10 największych liczb. W ciele funkcji możesz użyć metod `.sort()` oraz `.slice()`. -// Poniżej przedstawiam przykładowe działanie takiej funkcji: -// ``` -// const largest = getLargest(arr); -// console.log(largest); // [99, 93, 84, 78, 68, 61, 45, 45, 44, 38] -// ``` - -// arr.sort(function (a, b) { -// return b - a; -// }); -// console.log(arr); - -// console.log(arr.slice(10)); function getLargest() { arr.sort(function (a, b) { @@ -49,20 +22,15 @@ function getLargest() { const largest = getLargest(arr); console.log(largest); -// 3. Ostatnia funkcja ma obliczać [średnią arytmetyczną](https://pl.wikipedia.org/wiki/%C5%9Arednia_arytmetyczna) z liczb przechowyanych w przekazanej przez parametr tablicy. Możesz do tego wykorzystać `.forEach()` lub `.reduce()`. -// Przykładowe działanie takiej funkcji to: -// ``` -// const avg = getAvg([1, 2, 3, 4, 5]); -// console.log(avg); // 3 -// ``` function getAvg(arr) { - // return arr.reduce((total, arr) => (total + arr), 0)/arr.length; + if (arr.length === 0) { + return 0; + } let sum = 0; arr.forEach(num => { sum += num; - }); return sum / arr.length; @@ -71,7 +39,5 @@ function getAvg(arr) { const avg = getAvg(largest); console.log(avg); -// 4. Wykorzystaj napisane funkcje w taki sposób, aby utworzyć tablicę składającą się z 20 losowych liczb z przedziału od 10 do 200. Z utworzonej tablicy wybiesz 10 największych i obliczych ich średnią arytmetyczną. -// Zadbaj o odpowiednie nazwy dla funkcji - nie muszą być one identyczne jak w przykładach. diff --git a/05/app.js b/05/app.js index eff39d49..8f9f018a 100644 --- a/05/app.js +++ b/05/app.js @@ -25,22 +25,23 @@ Student.prototype.addGrade = function (subject, grade) { // - obliczyć ogólną średnią arytmetyczną. Student.prototype.getAverageGrade = function (subject) { - if (typeof subject !== 'undefine') { + if (typeof subject !== 'undefined') { const grades = this.grades[subject]; if (!grades || grades.length === 0) return null; + console.log(grades); let sum = 0; for (let i = 0; i < grades.length; i++) { sum += grades[i]; } - console.log(sum / grades.length); + return (sum / grades.length); } let totalSum = 0; let totalCount = 0; - + console.log(this.grades); for (let subjectName in this.grades) { const subjectGrades = this.grades[subjectName]; - + console.log(subjectGrades); for (let i = 0; i < subjectGrades.length; i++) { totalSum += subjectGrades[i]; totalCount++; @@ -48,7 +49,7 @@ Student.prototype.getAverageGrade = function (subject) { } if (totalCount === 0) return null; - console.log((totalSum / totalCount).toFixed(2)); + return ((totalSum / totalCount).toFixed(2)); } const maths = 4; @@ -61,7 +62,8 @@ student.addGrade('english', 3); const avgMath = student.getAverageGrade('maths'); // 5 const avg = student.getAverageGrade(); // 4.33 - +console.log(avgMath); +console.log(avg) // Zwróć uwagę, że pobranie informacji o ocenach z konkretnego przedmiotu może się odbywać w ten sposób: