From 844d5b0fdcc9301a255013239f78b0cb7efb1d1b Mon Sep 17 00:00:00 2001 From: marrcelp Date: Fri, 29 Nov 2024 13:38:16 +0100 Subject: [PATCH 1/9] resolved exercise 1, add script to html file --- .DS_Store | Bin 0 -> 6148 bytes 01/assets/js/app.js | 50 +++++++++++++++++++++++++++++++++++++++++++- 01/index.html | 2 +- 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..0747df2ab5c489072699df27dde6d160aeab4b0e GIT binary patch literal 6148 zcmeH~JqiLr422W55Nx)zoW@>nFo>SO3y5yHU?KK%bYFfDU9Cmr1(KJ^Oj!0Sb~YlS z+gra8=|p4>H^OBMFcI2|Nhc_o2W|YibMiPX~gJ0MHrAZpeL>fEFu2YibKc1*XvojaKzB#OmG- zEqSh{w$Nx7&EZ4y$!b#!Oru@2U;)$W!axEfFd{IGb!Yeg9{y?mAGL5v0wnNf1a!7L z?6!ERyj$O1&+5mlTHfGLKaTM76M%(X#Y?yw_KPLZn%Y89f$>McF))z8R|(t!&UX*I literal 0 HcmV?d00001 diff --git a/01/assets/js/app.js b/01/assets/js/app.js index 9323b98d..8c17421d 100644 --- a/01/assets/js/app.js +++ b/01/assets/js/app.js @@ -1,4 +1,52 @@ 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), typeof(b)); + +function performAction(number1, number2, mathOperation) { + + const numA = parseFloat(number1); + const numB = parseFloat(number2); + + switch (mathOperation) { + case '+': + result = numA + numB; + break; + + case '-': + result = numA - numB; + break; + + case '*': + result = numA * numB; + break; + + case '/': + result = numA / numB; + break; + + case '%': + result = numA % numB; + break; + + default: + result = 'Niepoprawna operacja'; + } + + if (result > 20){ + console.log('Wynik operacji jest większy od 20') + } else if (result === 20) { + console.log('Wynik operacji wynosi 20'); + } else { + console.log('Wynik operacji jest mniejszy od 20') + } + + return result; +} + +console.log(performAction(a, b, '+')); +console.log(performAction(a, b, '-')); +console.log(performAction(a, b, '*')); +console.log(performAction(a, b, '/')); +console.log(performAction(a, b, '%')); \ No newline at end of file diff --git a/01/index.html b/01/index.html index 5c55d687..c723b964 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 9fa2fb72c1d9227926b4a384e142e690f2003114 Mon Sep 17 00:00:00 2001 From: marrcelp Date: Fri, 29 Nov 2024 13:39:09 +0100 Subject: [PATCH 2/9] add script to html file --- 02/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/02/index.html b/02/index.html index 30150099..5659652c 100644 --- a/02/index.html +++ b/02/index.html @@ -7,6 +7,6 @@ devmentor.pl - JS BASICS - #02 - + \ No newline at end of file From 34c4a5194fc335246c3055686d15d9e594979c27 Mon Sep 17 00:00:00 2001 From: marrcelp Date: Fri, 29 Nov 2024 14:17:47 +0100 Subject: [PATCH 3/9] resolved exercise 2 --- 02/app.js | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/02/app.js b/02/app.js index b397190c..0770d5bc 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('Podaj liczbę!'); + +// if (x > 9 || x < 1){ +// alert('Wartość powinna mieścić się w przedziale 1-9! Podaj cyfrę!'); +// location.reload(); +// } + +// for (let i = 0; i < 10; i++){ +// console.log(`${x} * ${i} = ${x*i}`); +// } + + +/* rozwiązanie z pętlą while */ + +const a = prompt('Podaj podstawę!'); +const n = prompt('Podaj wykładnik!'); +if (a > 9 || a < 0 || n < 0 || n > 10){ + alert('Wartość powinna mieścić się w przedziale 1-9! Podaj cyfrę!'); + location.reload(); +} else if (n == 0) { + console.log(`${a}^${n} = 1`); +} +let i = 1; +let sum = a; -/* rozwiązanie z pętlą while */ \ No newline at end of file +while (i < n){ + sum *= a; + console.log(`${a}^${n} = ${sum}`); + i++; +} \ No newline at end of file From c9a81d7f68ebb107f39c13492dbbf5d0b9060a54 Mon Sep 17 00:00:00 2001 From: marrcelp Date: Fri, 29 Nov 2024 16:21:06 +0100 Subject: [PATCH 4/9] resolved exercise 3 --- 03/app.js | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/03/app.js b/03/app.js index 5b9361e4..ed532685 100644 --- a/03/app.js +++ b/03/app.js @@ -7,12 +7,61 @@ const c = randomNumber(min, max); console.log(a, b, c); +function randomNumber(min, max) { + return Math.round((Math.random() * (max - min)) + min); +} +function getSum(a, b, c) { + if (a > b && b > c){ + return parseInt(a) + parseInt(b); + } else if (a > b && c > b){ + return parseInt(a) + parseInt(c); + } else if (a < b && a < c){ + return parseInt(b) + parseInt(c); + } else if (a < b && a >c){ + return parseInt(a) + parseInt(b); + } +} -function randomNumber(min, max) { - return Math.round((Math.random() * (max - min)) + min); -} \ No newline at end of file + +const isEven = function(number){ + + if (typeof(number) !== 'number'){ + return null; + } + + if(number%2 === 0){ + return true; + } else { + return false; + } +} + +function showInfo(value, boolean){ + + switch(value, boolean){ + case null: + console.log(`Podany argument ${value} nie jest liczbą`); + break; + + case true: + console.log(`Podany argument ${value} jest parzysty`); + break; + + case false: + console.log(`Podany argument ${value} jest nieparzysty`); + break; + } +} + +const sum = getSum(a, b, c); +console.log(sum); + +const isSumEven = isEven(sum); +console.log(isSumEven); + +showInfo(sum, isSumEven); \ No newline at end of file From 11e493a70b42e19a101dbc224777f17e35be8f0d Mon Sep 17 00:00:00 2001 From: marrcelp Date: Fri, 29 Nov 2024 18:25:52 +0100 Subject: [PATCH 5/9] resolved ex 4, add script to index.html --- 04/app.js | 33 +++++++++++++++++++++++++++++++++ 04/index.html | 2 +- 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 04/app.js diff --git a/04/app.js b/04/app.js new file mode 100644 index 00000000..93d15496 --- /dev/null +++ b/04/app.js @@ -0,0 +1,33 @@ +function createArray(min, max){ + + const array = []; + + for (let i = 0; i < 20; i++){ + const randomNumber = Math.round((Math.random() * (max - min)) + min); + array.push(randomNumber); + } + + return array; +} + +function sortArray(array){ + const sortedArray = array.sort((a, b) => b - a); + const highestNumbers = sortedArray.slice(0, 10); + return highestNumbers; +} + +function getAvg(array){ + const sum = array.reduce((acc, element) => acc + element); + const avg = sum / array.length; + return avg; +} + +const arr = createArray(10, 200); +console.log(arr); + +const sort = sortArray(arr); +console.log(sort); + +const avg =getAvg(sort); +console.log(avg); + diff --git a/04/index.html b/04/index.html index 06aebbb8..d8d84401 100644 --- a/04/index.html +++ b/04/index.html @@ -7,6 +7,6 @@ devmentor.pl - JS BASICS - #04 - + \ No newline at end of file From 89c244d822f3e61afc49725ade679f690140ceb2 Mon Sep 17 00:00:00 2001 From: marrcelp Date: Fri, 29 Nov 2024 20:05:39 +0100 Subject: [PATCH 6/9] resolved exercise 5 --- 05/app.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 05/index.html | 2 +- 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 05/app.js diff --git a/05/app.js b/05/app.js new file mode 100644 index 00000000..8ac84e1c --- /dev/null +++ b/05/app.js @@ -0,0 +1,42 @@ +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){ + + if(typeof this.grades[subject] === 'undefined') { + + const allGrades = Object.values(this.grades).flat(); + const generalSum = allGrades.reduce((acc, element) => acc + element); + const generalAvg = (generalSum / allGrades.length).toFixed(2); + return generalAvg; + } + + const sum = this.grades[subject].reduce((acc, element) => acc + element); + const avg = sum / this.grades[subject].length; + return avg; +} + +const student = new Student('Jan', 'Kowalski'); +student.addGrade('math', 4); +student.addGrade('math', 6); +student.addGrade('english', 3); + +const avgMath = student.getAverageGrade('math'); +console.log(avgMath); + +const avg = student.getAverageGrade(); +console.log(avg); + +console.log(student); \ No newline at end of file diff --git a/05/index.html b/05/index.html index 52a42709..0dfb10b7 100644 --- a/05/index.html +++ b/05/index.html @@ -7,6 +7,6 @@ devmentor.pl - JS BASICS - #05 - + \ No newline at end of file From 17b9574015b662fcf84a3f2cddb4db4d46c3cce8 Mon Sep 17 00:00:00 2001 From: marrcelp Date: Wed, 4 Dec 2024 19:13:05 +0100 Subject: [PATCH 7/9] fix equation --- 02/app.js | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/02/app.js b/02/app.js index 0770d5bc..96d03b2e 100644 --- a/02/app.js +++ b/02/app.js @@ -13,21 +13,28 @@ /* rozwiązanie z pętlą while */ -const a = prompt('Podaj podstawę!'); +const a = prompt('Podaj podstawę!'); const n = prompt('Podaj wykładnik!'); if (a > 9 || a < 0 || n < 0 || n > 10){ alert('Wartość powinna mieścić się w przedziale 1-9! Podaj cyfrę!'); location.reload(); -} else if (n == 0) { - console.log(`${a}^${n} = 1`); -} - -let i = 1; -let sum = a; -while (i < n){ - sum *= a; - console.log(`${a}^${n} = ${sum}`); - i++; +} else if (n == 0) { + console.log(`${a}^${n} = 1`); + //zostawilem w tym przypadku zapis a^n, poniewaz a^0 = 1, ciezko to inaczej zapisac + +} else { + + let i = 1; + let sum = a; + let equation = `${a}` + + while (i < n){ + sum *= a; + equation += ` * ${a}`; + i++; + } + + console.log(`${equation} = ${sum}`); } \ No newline at end of file From adbfe97242a0c4fa82c6362dd865df801e6c9560 Mon Sep 17 00:00:00 2001 From: marrcelp Date: Wed, 4 Dec 2024 19:26:25 +0100 Subject: [PATCH 8/9] fix function getSum --- 03/app.js | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/03/app.js b/03/app.js index ed532685..a08cb9d5 100644 --- a/03/app.js +++ b/03/app.js @@ -13,18 +13,12 @@ function randomNumber(min, max) { function getSum(a, b, c) { - if (a > b && b > c){ - return parseInt(a) + parseInt(b); - - } else if (a > b && c > b){ - return parseInt(a) + parseInt(c); - - } else if (a < b && a < c){ - return parseInt(b) + parseInt(c); - - } else if (a < b && a >c){ - return parseInt(a) + parseInt(b); - } + const array = [parseInt(a), parseInt(b), parseInt(c)]; + const sortedArray = array.sort((a ,b) => b - a); + const arrayWith2Elements = sortedArray.slice(0, 2); + const sum = arrayWith2Elements.reduce((acc, el) => acc + el, 0); + return sum; + } From 9dbb294f5f96338deabbb0640885c4c822a51f0d Mon Sep 17 00:00:00 2001 From: marrcelp Date: Wed, 4 Dec 2024 19:29:31 +0100 Subject: [PATCH 9/9] fix spacing, add second parameter to reduce --- 04/app.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/04/app.js b/04/app.js index 93d15496..896b99c9 100644 --- a/04/app.js +++ b/04/app.js @@ -3,8 +3,8 @@ function createArray(min, max){ const array = []; for (let i = 0; i < 20; i++){ - const randomNumber = Math.round((Math.random() * (max - min)) + min); - array.push(randomNumber); + const randomNumber = Math.round((Math.random() * (max - min)) + min); + array.push(randomNumber); } return array; @@ -17,7 +17,7 @@ function sortArray(array){ } function getAvg(array){ - const sum = array.reduce((acc, element) => acc + element); + const sum = array.reduce((acc, element) => acc + element, 0); const avg = sum / array.length; return avg; }