From 4ca7432c6d7ad72967ccad2f0f232dfa724fe9d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82?= Date: Wed, 22 Jan 2025 16:04:21 +0100 Subject: [PATCH 1/6] task 01 done --- .vscode/settings.json | 3 ++ 01/assets/js/app.js | 75 ++++++++++++++++++++++++++++++++++++++++++- 01/index.html | 17 ++++++++-- 3 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..6f3a2913 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "liveServer.settings.port": 5501 +} \ No newline at end of file diff --git a/01/assets/js/app.js b/01/assets/js/app.js index 9323b98d..60a8f617 100644 --- a/01/assets/js/app.js +++ b/01/assets/js/app.js @@ -1,4 +1,77 @@ const a = '4.2'; const b = 9; -console.log(a, b); \ No newline at end of file +console.log(a, b); +console.log(typeof a); +console.log(typeof b); + +const numA = parseFloat(a); + +const res1 = numA + b; +const res2 = numA - b; +const res3 = numA * b; +const res4 = numA / b; +const res5 = numA % b; +let res6 = numA ; +res6++; +let res7 = b; +res7++; +let res8 = numA ; +res8--; +let res9 = b; +res9--; + +if(res1 > 20) { + console.log('Wynik ' + res1 + ' jest wiekszy od 20!'); +} else { + console.log('Wynik ' + res1 + ' nie jest większy od 20!'); +} + +if(res2 > 20) { + console.log('Wynik ' + res2 + ' jest wiekszy od 20!'); +} else { + console.log('Wynik ' + res2 + ' nie jest większy od 20!'); +} + +if(res3 > 20) { + console.log('Wynik ' + res3 + ' jest wiekszy od 20!'); +} else { + console.log('Wynik ' + res3 + ' nie jest większy od 20!'); +} + +if(res4 > 20) { + console.log('Wynik ' + res4 + ' jest wiekszy od 20!'); +} else { + console.log('Wynik ' + res4 + ' nie jest większy od 20!'); +} + +if(res5 > 20) { + console.log('Wynik ' + res5 + ' jest wiekszy od 20!'); +} else { + console.log('Wynik ' + res5 + ' nie jest większy od 20!'); +} + + +console.log(res1, res2, res3, res4, res5, res6, res7, res8, res9); + +// nie wiem czy do końca chodziło o to rozwiązanie, +// wyniki się zgadzają, ale wydaje mi się, że można by +// przypisać funkcję z instrukcją warunkową w jej ciele +// i wtedy zajęło by to o wiele mniej linijek kodu + +// np: + +// function checkResult(resultNumber, result) { +// if(result > 20) { +// console.log(resultNumber + ' jest większy od 20!'); +// } else { +// console.log(resultNumber + ' nie jest większy od 20!'); +// } +// } + +// checkResult(`Wynik ${res1}`, res1); +// checkResult(`Wynik ${res2}`, res2); +// checkResult(`Wynik ${res3}`, res3); +// checkResult(`Wynik ${res4}`, res4); +// checkResult(`Wynik ${res5}`, res5); + diff --git a/01/index.html b/01/index.html index 5c55d687..6e9c8216 100644 --- a/01/index.html +++ b/01/index.html @@ -1,12 +1,23 @@ + - - + + devmentor.pl - JS BASICS - #01 + - + + + + \ No newline at end of file From d4127e4336915855965ae3d861ec0c884e26aecc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82?= Date: Wed, 29 Jan 2025 13:31:27 +0100 Subject: [PATCH 2/6] task 02 done --- .vscode/settings.json | 2 +- 02/app.js | 38 ++++++++++++++++++++++++++++++++++++-- 02/index.html | 17 ++++++++++++++--- 3 files changed, 51 insertions(+), 6 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 6f3a2913..f673a71b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,3 @@ { - "liveServer.settings.port": 5501 + "liveServer.settings.port": 5502 } \ No newline at end of file diff --git a/02/app.js b/02/app.js index b397190c..0c1f63b9 100644 --- a/02/app.js +++ b/02/app.js @@ -1,7 +1,41 @@ /* rozwiązanie z pętlą for */ -const x = 4; +// const x = Number(prompt('Podaj liczbę od 1 do 9!')); +// if(x >= 1 && x <= 9) { +// for(let i=1; i<=9; i++) { +// const result = x * i; +// console.log(x + ' x ' + i + ' = ' + result); +// } +// } else { +// console.log(alert('Podałeś niepoprawną liczbę!')); +// } -/* rozwiązanie z pętlą while */ \ No newline at end of file +/* rozwiązanie z pętlą while */ + +const a = Number(prompt('Podaj podstawę!')) +const n = Number(prompt('Podaj wykładnik!')) + + +if(a > 0 && n > 0) { +let result = 1 +let counter = 1 +let showCalculation = '' + +while(counter <= n) { + result = result * a + + if(counter === 1) { + showCalculation = showCalculation + a + } else { + showCalculation = showCalculation + ' * ' + a + } + + counter++ +} + +console.log(showCalculation + ' = ' + result); +} else { + console.log(alert('Podaj liczbę większą od 0')); +} diff --git a/02/index.html b/02/index.html index 30150099..26aea3f7 100644 --- a/02/index.html +++ b/02/index.html @@ -1,12 +1,23 @@ + - - + + devmentor.pl - JS BASICS - #02 + - + + + + \ No newline at end of file From f53a421126b8b0d6cd0fe76c1d8ba205ab351e60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82?= Date: Wed, 29 Jan 2025 19:47:59 +0100 Subject: [PATCH 3/6] task 03 done --- 03/app.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/03/app.js b/03/app.js index 5b9361e4..7ec1169a 100644 --- a/03/app.js +++ b/03/app.js @@ -15,4 +15,58 @@ console.log(a, b, c); function randomNumber(min, max) { return Math.round((Math.random() * (max - min)) + min); -} \ No newline at end of file +} + + +function getSum(a, b, c) { + const num1 = Math.floor(a) + const num2 = Math.floor(b) + const num3 = Math.floor(c) + + let largestNumber = Math.max(num1, num2, num3) + let middleNumber + + if(largestNumber === num1) { + middleNumber = Math.max(num2, num3) + } else if (largestNumber === num2) { + middleNumber = Math.max(num1, num3) + } else { + middleNumber = Math.max(num1, num2) + } + return largestNumber + middleNumber +} + + + +const isEven = function(number) { + if(typeof number !== 'number') { + return null + } + return number % 2 === 0 +} + + + +function showInfo(anyValue, specificValue) { + switch(specificValue) { + case null: + console.log('Podany argument ' + anyValue + ' nie jest liczbą!'); + break + case true: + console.log('Podany argument ' + anyValue + ' jest parzysty!'); + break + case false: + console.log('Podany argument ' + anyValue + ' jest nieparzysty!'); + break + default: + console.log('Nieprawidłowa wartość!'); + } +} + +const sum = getSum(a, b, c) +console.log(sum); + +const even = isEven(sum) +console.log(even); + +showInfo(sum, even) \ No newline at end of file From 88a1ef569293beb2d00e8c5da6a86eff9af62f18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82?= Date: Wed, 29 Jan 2025 22:53:53 +0100 Subject: [PATCH 4/6] task 04 done --- 04/index.html | 15 ++++++++++++--- 04/script.js | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 04/script.js diff --git a/04/index.html b/04/index.html index 06aebbb8..ba2bc690 100644 --- a/04/index.html +++ b/04/index.html @@ -1,12 +1,21 @@ + - - + + devmentor.pl - JS BASICS - #04 + - + + \ No newline at end of file diff --git a/04/script.js b/04/script.js new file mode 100644 index 00000000..3fc17222 --- /dev/null +++ b/04/script.js @@ -0,0 +1,52 @@ +'use strict' + +function createArray(min, max) { + const newArray = [] + + for(let i=0; i < 20; i++) { + const randomNumber = Math.round((Math.random() * (max - min)) + min); + newArray.push(randomNumber) + } + + return newArray + +} + + +function get10LargestNumbers(array) { + const sortedArray = array.sort(function(a, b) + { + return b - a + }); + + return sortedArray.slice(0, 10) + +} + + +function calculateAverage(array) { + if(array.length === 0) { + return 0 + } + + const sumArrNumbers = array.reduce(function(acc, currentValue) { + return acc + currentValue + }, 0) + + return sumArrNumbers / array.length +} + + + +const min = 10 +const max = 200 + +const array = createArray(min, max) +console.log('Wygenerowana tablica: ' + array); + +const largestNumbers = get10LargestNumbers(array) +console.log('10 największych liczb z tablicy: ' + largestNumbers); + +const average = calculateAverage(largestNumbers) +console.log('Średnia arytmetyczna 10-ciu największych liczb z tablicy: ' + average); + From 4e6f31180d4dfc56ef4468388884a58355baa853 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82?= Date: Fri, 14 Feb 2025 08:46:04 +0100 Subject: [PATCH 5/6] task 05 done --- 05/index.html | 17 +++++++++-- 05/script.js | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 05/script.js diff --git a/05/index.html b/05/index.html index 52a42709..b7bc3876 100644 --- a/05/index.html +++ b/05/index.html @@ -1,12 +1,23 @@ + - - + + devmentor.pl - JS BASICS - #05 + - + + + + \ No newline at end of file diff --git a/05/script.js b/05/script.js new file mode 100644 index 00000000..146d920b --- /dev/null +++ b/05/script.js @@ -0,0 +1,83 @@ +// konstruktor Student, który przyjmuje argumenty: 'firstName' i 'lastName' +// oraz posiada właściwość 'grades', która jest obiektem z ocenami studenta +const Student = function (firstName, lastName) { + this.firstName = firstName; + this.lastName = lastName; + this.grades = { + // math: [], + // english: [], + }; +} + +// metoda 'addGrade' powinna dodawać ocenę do przedmiotu +// oraz sprawdzać czy dany przedmiot istnieje w obiekcie 'grades', +// jeśli nie to ma go dodać +Student.prototype.addGrade = function (subject, grade) { + if(typeof this.grades[subject] === 'undefined') { + this.grades[subject] = []; + } + this.grades[subject].push(grade); +} + + +// metoda 'getAverageGrade' powinna zwracać średnią ocen z danego przedmiotu, lub ze wszystkich przedmiotów, jesli nie podamy argumentu +Student.prototype.getAverageGrade = function (subject) { + if(typeof subject !== 'undefined') { + const grades = this.grades[subject]; + console.log(grades); + const sum = grades.reduce(function(acc, grade) { + return acc + grade + }, 0); + + const average = sum / grades.length; + return Math.round(average * 100) / 100; + + + } else { + let allGrades = [] + for(let key in this.grades) { + const gradesFromSubject = this.grades[key]; + allGrades = allGrades.concat(gradesFromSubject); + } + + const sum = allGrades.reduce(function(acc, grade) { + return acc + grade + }, 0); + + const averageAll = sum / allGrades.length; + return Math.round(averageAll * 100) / 100; + } +} + + +const student1 = new Student('Jan', 'Kowalski'); +// const student2 = new Student('Anna', 'Nowak'); +// const student3 = new Student('Piotr', 'Piotrowski'); + +student1.addGrade('math', 4); +student1.addGrade('math', 3); + +// student2.addGrade('math', 5); +// student2.addGrade('math', 4); + +// student3.addGrade('math', 3); +// student3.addGrade('math', 5); + +student1.addGrade('english', 5); +// student2.addGrade('english', 3); +// student3.addGrade('english', 4); + +const avgMath = student1.getAverageGrade('math'); +const avgAll = student1.getAverageGrade(); +// const avgMath = student2.getAverageGrade('math'); +// const avgMath = student3.getAverageGrade('math'); + + +console.log(student1); // wyswietla obiekt z imieniem, nazwiskiem i ocenami + +console.log(avgAll); +console.log(avgMath); +console.log(student1.getAverageGrade('math')); + + + From aa54821d2bb0e83d1001a4be61d7dc6767f926c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82?= Date: Wed, 19 Feb 2025 10:46:41 +0100 Subject: [PATCH 6/6] modifid task 01 and 05 --- 01/assets/js/app.js | 82 +++++++++++++++++++++------------------------ 05/script.js | 22 ++++++------ 2 files changed, 48 insertions(+), 56 deletions(-) diff --git a/01/assets/js/app.js b/01/assets/js/app.js index 60a8f617..5751660b 100644 --- a/01/assets/js/app.js +++ b/01/assets/js/app.js @@ -21,57 +21,51 @@ res8--; let res9 = b; res9--; -if(res1 > 20) { - console.log('Wynik ' + res1 + ' jest wiekszy od 20!'); -} else { - console.log('Wynik ' + res1 + ' nie jest większy od 20!'); -} - -if(res2 > 20) { - console.log('Wynik ' + res2 + ' jest wiekszy od 20!'); -} else { - console.log('Wynik ' + res2 + ' nie jest większy od 20!'); -} +// if(res1 > 20) { +// console.log('Wynik ' + res1 + ' jest wiekszy od 20!'); +// } else { +// console.log('Wynik ' + res1 + ' nie jest większy od 20!'); +// } -if(res3 > 20) { - console.log('Wynik ' + res3 + ' jest wiekszy od 20!'); -} else { - console.log('Wynik ' + res3 + ' nie jest większy od 20!'); -} +// if(res2 > 20) { +// console.log('Wynik ' + res2 + ' jest wiekszy od 20!'); +// } else { +// console.log('Wynik ' + res2 + ' nie jest większy od 20!'); +// } -if(res4 > 20) { - console.log('Wynik ' + res4 + ' jest wiekszy od 20!'); -} else { - console.log('Wynik ' + res4 + ' nie jest większy od 20!'); -} +// if(res3 > 20) { +// console.log('Wynik ' + res3 + ' jest wiekszy od 20!'); +// } else { +// console.log('Wynik ' + res3 + ' nie jest większy od 20!'); +// } -if(res5 > 20) { - console.log('Wynik ' + res5 + ' jest wiekszy od 20!'); -} else { - console.log('Wynik ' + res5 + ' nie jest większy od 20!'); -} +// if(res4 > 20) { +// console.log('Wynik ' + res4 + ' jest wiekszy od 20!'); +// } else { +// console.log('Wynik ' + res4 + ' nie jest większy od 20!'); +// } +// if(res5 > 20) { +// console.log('Wynik ' + res5 + ' jest wiekszy od 20!'); +// } else { +// console.log('Wynik ' + res5 + ' nie jest większy od 20!'); +// } -console.log(res1, res2, res3, res4, res5, res6, res7, res8, res9); -// nie wiem czy do końca chodziło o to rozwiązanie, -// wyniki się zgadzają, ale wydaje mi się, że można by -// przypisać funkcję z instrukcją warunkową w jej ciele -// i wtedy zajęło by to o wiele mniej linijek kodu +// console.log(res1, res2, res3, res4, res5, res6, res7, res8, res9); -// np: -// function checkResult(resultNumber, result) { -// if(result > 20) { -// console.log(resultNumber + ' jest większy od 20!'); -// } else { -// console.log(resultNumber + ' nie jest większy od 20!'); -// } -// } +function checkResult(resultNumber, result) { + if(result > 20) { + console.log(resultNumber + ' jest większy od 20!'); + } else { + console.log(resultNumber + ' nie jest większy od 20!'); + } +} -// checkResult(`Wynik ${res1}`, res1); -// checkResult(`Wynik ${res2}`, res2); -// checkResult(`Wynik ${res3}`, res3); -// checkResult(`Wynik ${res4}`, res4); -// checkResult(`Wynik ${res5}`, res5); +checkResult(`Wynik ${res1}`, res1); +checkResult(`Wynik ${res2}`, res2); +checkResult(`Wynik ${res3}`, res3); +checkResult(`Wynik ${res4}`, res4); +checkResult(`Wynik ${res5}`, res5); diff --git a/05/script.js b/05/script.js index 146d920b..98c63c87 100644 --- a/05/script.js +++ b/05/script.js @@ -19,19 +19,22 @@ Student.prototype.addGrade = function (subject, grade) { this.grades[subject].push(grade); } +// funkcja licząca średnia z przedmiotu lub przedmiotów do dwóch miejsc po przecinku +function calculateAverage(grades) { + const sum = grades.reduce(function(acc, grade) { + return acc + grade}, 0); + const average = sum / grades.length; + return Math.round(average * 100) / 100; +} + // metoda 'getAverageGrade' powinna zwracać średnią ocen z danego przedmiotu, lub ze wszystkich przedmiotów, jesli nie podamy argumentu Student.prototype.getAverageGrade = function (subject) { if(typeof subject !== 'undefined') { const grades = this.grades[subject]; console.log(grades); - const sum = grades.reduce(function(acc, grade) { - return acc + grade - }, 0); - - const average = sum / grades.length; - return Math.round(average * 100) / 100; + return calculateAverage(grades) } else { let allGrades = [] @@ -40,12 +43,7 @@ Student.prototype.getAverageGrade = function (subject) { allGrades = allGrades.concat(gradesFromSubject); } - const sum = allGrades.reduce(function(acc, grade) { - return acc + grade - }, 0); - - const averageAll = sum / allGrades.length; - return Math.round(averageAll * 100) / 100; + return calculateAverage(allGrades) } }