From 6462b972770b1780244ffbb4f3efc4184be1dd2f Mon Sep 17 00:00:00 2001 From: Sandra Date: Mon, 18 Aug 2025 13:35:36 +0200 Subject: [PATCH 1/8] complete task 01 --- 01/assets/js/app.js | 73 ++++++++++++++++++++++++++++++++++++++++++++- 01/index.html | 3 +- 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/01/assets/js/app.js b/01/assets/js/app.js index 9323b98d..00a170d6 100644 --- a/01/assets/js/app.js +++ b/01/assets/js/app.js @@ -1,4 +1,75 @@ const a = '4.2'; const b = 9; -console.log(a, b); \ No newline at end of file +console.log(a, b); +console.log("Constant a is of type:", typeof(a), ". Constant b is of type:", typeof(b)); + +function sum(a, b) { + return Number(a) + b; +} + +function difference(a, b) { + return Number(a) - b; +} + +function product(a, b) { + return Number(a) * b; +} + +function quotient(a, b) { + return Number(a) / b; +} + +function remainder(a, b) { + return Number(a) % b; +} + +function increment(a, b) { + let numA = Number(a); + numA++; + let numB = b; + numB++; + return [numA, numB]; +} + +function decrement(a, b) { + let numA = Number(a); + numA--; + let numB = b; + numB--; + return [numA, numB]; +} + +const addition = sum(a, b); +const subtraction = difference(a, b); +const multipliplication = product(a, b); +const division = quotient(a, b); +const modulo = remainder(a, b); +const postIncrement = increment(a, b); +const postDecrement = decrement(a, b); + +console.log("Score of addition:", addition); +console.log("Score of subtraction:", subtraction); +console.log("Score of multipliplication:", multipliplication.toFixed(2)); +console.log("Score of division:", division.toFixed(2)); +console.log("Score of modulo:", modulo); +console.log("Scores od increment:", postIncrement); +console.log("Scores of decrement:", postDecrement); + +const results = [ + { name: "Addition", value: addition}, + { name: "Substraction", value: subtraction}, + { name: "Multipliplication", value: multipliplication}, + { name: "Division", value: division}, + { name: "Modulo", value: modulo}, + { name: "postIncrement", value: postIncrement}, + { name: "postDecrement", value: postDecrement}, +]; + +for (const result of results) { + if (result.value > 20) { + console.log(`${result.name} is greater than 20`); + } else { + console.log(`${result.name} is less than or equal to 20`) + } +} \ No newline at end of file diff --git a/01/index.html b/01/index.html index 5c55d687..699f2f9b 100644 --- a/01/index.html +++ b/01/index.html @@ -4,9 +4,10 @@ + devmentor.pl - JS BASICS - #01 - + \ No newline at end of file From 8e68e15ddfb267c29dbfb44450c47d61897aae79 Mon Sep 17 00:00:00 2001 From: Sandra Date: Mon, 18 Aug 2025 13:59:09 +0200 Subject: [PATCH 2/8] complete 1/2 of task 02 --- 02/app.js | 12 ++++++++++-- 02/index.html | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/02/app.js b/02/app.js index b397190c..91eeb314 100644 --- a/02/app.js +++ b/02/app.js @@ -1,7 +1,15 @@ /* rozwiązanie z pętlą for */ -const x = 4; - +const x = prompt('Podaj liczbę!'); + +if(x > 9) { + alert("Za duża liczba, uczymy się do 9!"); +} else { + for(let i = 1; i <= 9; i++) { + const results = x * i; + console.log(`${x} x ${i} = ${results}`); + } +}10 /* rozwiązanie z pętlą while */ \ No newline at end of file diff --git a/02/index.html b/02/index.html index 30150099..1546e75c 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 f1a47f00dd73cdbf78c98055114c35a41c031070 Mon Sep 17 00:00:00 2001 From: Sandra Date: Mon, 18 Aug 2025 14:40:41 +0200 Subject: [PATCH 3/8] complete task 02 --- 02/app.js | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/02/app.js b/02/app.js index 91eeb314..0ca98105 100644 --- a/02/app.js +++ b/02/app.js @@ -1,6 +1,6 @@ /* rozwiązanie z pętlą for */ -const x = prompt('Podaj liczbę!'); +const x = Number(prompt('Podaj liczbę od 1 do 9!')); if(x > 9) { alert("Za duża liczba, uczymy się do 9!"); @@ -9,7 +9,29 @@ if(x > 9) { const results = x * i; console.log(`${x} x ${i} = ${results}`); } -}10 +} -/* rozwiązanie z pętlą while */ \ No newline at end of file + +/* rozwiązanie z pętlą while */ + +const a = Number(prompt('Podaj podstawę potęgowania! (max liczba 10)')); +const n = Number(prompt('Podaj wykładnij potęgowania! (max liczba 5)')); + +let result = 1; +let sequence = ''; + + +if(a < 1 || a > 10 && n < 1 || n > 5) { + alert('Prosze podac prawidłowe liczby!'); +} else { + let i = 1; + while(i <= n) { + result *= a; + sequence += a; + if( i < n) sequence += ' * '; + i++; + } + console.log(`${sequence} = ${result}`); +} + From bb59c2602d020721f3008c782efe010bf55a1298 Mon Sep 17 00:00:00 2001 From: Sandra Date: Mon, 18 Aug 2025 17:18:58 +0200 Subject: [PATCH 4/8] complete task 03 --- 03/app.js | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/03/app.js b/03/app.js index 5b9361e4..61824d21 100644 --- a/03/app.js +++ b/03/app.js @@ -7,10 +7,44 @@ const c = randomNumber(min, max); console.log(a, b, c); - - - - +function getSum(a, b, c) { + const numbers = [a, b, c]; + numbers.sort(function(a, b){ return b-a;}); + console.log("sort numbers:", numbers); + const sum = numbers[0] + numbers[1]; + return sum; +} +const sum = getSum(a, b, c); +console.log("Sum of two greater numbers:", sum); + +function isEven(num) { + if(typeof(num) === Number) { + return null; + } else { + if (num % 2 === 0 ) { + return true; + } else { + return false; + } + } +} +const isSumEven = isEven(sum); +console.log("Sum is even?", isSumEven); + + +function showInfo(sum, isSumEven) { + switch(isSumEven) { + case null: + console.log(`Podany argument ${sum} nie jest liczbą`); + break; + case true: + console.log(`Podany argument ${sum} jest parzysty`); + break; + case false: + console.log(`Podany argument ${sum} jest nieparzysty`); + } +} +const completeInfo = showInfo(sum, isSumEven); function randomNumber(min, max) { From b25796830979ede8d65d2afb874bc483cd48851b Mon Sep 17 00:00:00 2001 From: Sandra Date: Mon, 18 Aug 2025 18:23:02 +0200 Subject: [PATCH 5/8] complete task 04 --- 03/app.js | 2 +- 04/app.js | 25 +++++++++++++++++++++++++ 04/index.html | 2 +- 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 04/app.js diff --git a/03/app.js b/03/app.js index 61824d21..8dd8d18c 100644 --- a/03/app.js +++ b/03/app.js @@ -10,7 +10,7 @@ console.log(a, b, c); function getSum(a, b, c) { const numbers = [a, b, c]; numbers.sort(function(a, b){ return b-a;}); - console.log("sort numbers:", numbers); + console.log("Sort numbers:", numbers); const sum = numbers[0] + numbers[1]; return sum; } diff --git a/04/app.js b/04/app.js new file mode 100644 index 00000000..b3ca2a4f --- /dev/null +++ b/04/app.js @@ -0,0 +1,25 @@ + +function randomNumber(min, max) { + return Math.round((Math.random() * (max - min)) + min); +} + +const arr = []; + +for(let i = 0; i < 20; i++) { + arr.push(randomNumber(1, 200)); +} +console.log("Full array random 20 numbers:", arr); + + +function sortNumbers(arr) { + return arr.sort(function(a, b){return b-a}).slice(0, 9); +} +const largestNumbers = sortNumbers(arr); +console.log("Largest numbers:", largestNumbers); + + + +function arithmeticMean(arr) { + return arr.reduce((acc, curr) => acc + curr, 0) / arr.length; +} +console.log(`Arithmetic mean of ${arr} is ${arithmeticMean(arr)}`); \ No newline at end of file diff --git a/04/index.html b/04/index.html index 06aebbb8..f825f47f 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 e707d5959fd5ae6f69043ac433125a3f9c08942a Mon Sep 17 00:00:00 2001 From: Sandra Date: Mon, 18 Aug 2025 19:05:30 +0200 Subject: [PATCH 6/8] complete task 05/ complete all tasks --- 05/app.js | 40 ++++++++++++++++++++++++++++++++++++++++ 05/index.html | 2 +- 2 files changed, 41 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..38924df4 --- /dev/null +++ b/05/app.js @@ -0,0 +1,40 @@ +function Student(initFirstName, initLastName, initGrades) { + 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); +}; + +const student = new Student('Sandra', 'Mstowska'); +student.addGrade('maths', 4); +student.addGrade('maths', 5); +student.addGrade('english', 3); +student.addGrade('english', 5); + + +Student.prototype.getAverageGrade = function(subject) { + if (subject) { + const grades = this.grades[subject]; + if (!grades || grades.length === 0) return 0; + const sum = grades.reduce((acc, val) => acc + val, 0); + return sum /grades.length; + } else { + const allGrades = Object.values(this.grades).flat(); + if (allGrades.length === 0) return 0; + const sum = allGrades.reduce((acc, val) => acc + val, 0); + return sum /allGrades.length; + } +}; + +student.addGrade('polish', 1); + +console.log(student); +console.log("Średnia wszytskich ocen:", student.getAverageGrade()); +console.log("Średnia ocen z matmy:", student.getAverageGrade('maths')); +console.log("Średnia ocen z polski język:", student.getAverageGrade('polish')); \ 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 d4878b6e4becb37f6ce8d4ae70f6612fedbcdde4 Mon Sep 17 00:00:00 2001 From: Sandra Date: Tue, 19 Aug 2025 12:19:15 +0200 Subject: [PATCH 7/8] add security in task 04 and add a general calculation method in task 05 --- 04/app.js | 6 +++++- 05/app.js | 15 ++++++++------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/04/app.js b/04/app.js index b3ca2a4f..79d9c80d 100644 --- a/04/app.js +++ b/04/app.js @@ -20,6 +20,10 @@ console.log("Largest numbers:", largestNumbers); function arithmeticMean(arr) { - return arr.reduce((acc, curr) => acc + curr, 0) / arr.length; + if( arr.length !== 0) { + return arr.reduce((acc, curr) => acc + curr, 0) / arr.length; + } else { + return 0; + } } console.log(`Arithmetic mean of ${arr} is ${arithmeticMean(arr)}`); \ No newline at end of file diff --git a/05/app.js b/05/app.js index 38924df4..b34f409b 100644 --- a/05/app.js +++ b/05/app.js @@ -17,18 +17,19 @@ student.addGrade('maths', 5); student.addGrade('english', 3); student.addGrade('english', 5); +Student.prototype.calculateAverage = function(grades) { + if (!grades || grades.length === 0) return 0; + const sum = grades.reduce((acc, val) => acc + val, 0); + return sum /grades.length; +}; + Student.prototype.getAverageGrade = function(subject) { if (subject) { - const grades = this.grades[subject]; - if (!grades || grades.length === 0) return 0; - const sum = grades.reduce((acc, val) => acc + val, 0); - return sum /grades.length; + return this.calculateAverage(this.grades[subject]); } else { const allGrades = Object.values(this.grades).flat(); - if (allGrades.length === 0) return 0; - const sum = allGrades.reduce((acc, val) => acc + val, 0); - return sum /allGrades.length; + return this.calculateAverage(allGrades); } }; From c56889b90d8e2057e6c239e4b878646910a17eec Mon Sep 17 00:00:00 2001 From: Sandra Date: Wed, 20 Aug 2025 10:52:05 +0200 Subject: [PATCH 8/8] change the order of conditions in a function arithmeticMean --- 04/app.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/04/app.js b/04/app.js index 79d9c80d..9f076067 100644 --- a/04/app.js +++ b/04/app.js @@ -20,10 +20,9 @@ console.log("Largest numbers:", largestNumbers); function arithmeticMean(arr) { - if( arr.length !== 0) { - return arr.reduce((acc, curr) => acc + curr, 0) / arr.length; - } else { + if( arr.length === 0) { return 0; } + return arr.reduce((acc, curr) => acc + curr, 0) / arr.length; } console.log(`Arithmetic mean of ${arr} is ${arithmeticMean(arr)}`); \ No newline at end of file