From bdca1e1167dab91e50cf7000666326a48856cfb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Mako=CC=81wka?= Date: Sat, 15 Feb 2025 13:02:04 +0100 Subject: [PATCH 1/5] Task 01 --- 01/assets/js/app.js | 48 ++++++++++++++++++++++++++++++++++++++++++++- 01/index.html | 2 +- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/01/assets/js/app.js b/01/assets/js/app.js index 9323b98d..b6395a6c 100644 --- a/01/assets/js/app.js +++ b/01/assets/js/app.js @@ -1,4 +1,50 @@ const a = '4.2'; const b = 9; -console.log(a, b); \ No newline at end of file +console.log(a, b); + +// 1. Check the types of variables +console.log("Type of variable a: ", typeof a); // string +console.log("Type of variable b: ", typeof b); // number + +// 2. Arithmetic operations +// Convert 'a' to a number to avoid string concatenation +const addition = parseFloat(a) + b; + +const subtraction = a - b; +const multiplication = a * b; +const division = a / b; +const modulo = a % b; +const exponentiation = a ** b; + +// console.log("Addition: ", addition); +// console.log("Subtraction: ", subtraction); +// console.log("Multiplication: ", multiplication); +// console.log("Division: ", division); +// console.log("Modulo: ", modulo); +// console.log("Exponentiation: ", exponentiation); + +// 3. Check which result is greater than 20, which is less than 20 and which is equal to 20 +const results = [ + { name: 'Addition', value: addition }, + { name: 'Subtraction', value: subtraction }, + { name: 'Multiplication', value: multiplication }, + { name: 'Division', value: division }, + { name: 'Modulo', value: modulo }, + { name: 'Exponentiation', value: exponentiation } +]; + +results.forEach(result => { + if (!result || typeof result.value !== 'number' || typeof result.name !== 'string') { + console.error('Invalid result object encountered:', result); + return; + } + + if (result.value > 20) { + console.log(`${result.name} result ${result.value} is greater than 20.`); + } else if (result.value < 20) { + console.log(`${result.name} result ${result.value} is less than 20.`); + } else { + console.log(`${result.name} result ${result.value} is equal to 20.`); + } +}); \ No newline at end of file diff --git a/01/index.html b/01/index.html index 5c55d687..b7d8e32f 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 aeaf136d01dce0e9919217b4c4f937c32de11d7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Mako=CC=81wka?= Date: Sat, 15 Feb 2025 14:16:02 +0100 Subject: [PATCH 2/5] Task 02 --- 02/app.js | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 02/index.html | 2 +- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/02/app.js b/02/app.js index b397190c..242b4875 100644 --- a/02/app.js +++ b/02/app.js @@ -1,7 +1,49 @@ /* rozwiązanie z pętlą for */ -const x = 4; +const input = prompt('Enter the multiplication factor: '); +const x = Number(input); +if (isNaN(x)) { + console.error(`Provided value (${input}) is not a number`); +} else if (!Number.isInteger(x)) { + console.error(`Provided value (${x}) is not an integer`); +} else if (x < 1 || x > 9) { + console.error(`Provided value (${x}) is not between 1 and 9`); +} else { + for (let i = 1; i <= 9; i++) { + console.log(`${x} * ${i} = ${x * i}`); + } +} -/* rozwiązanie z pętlą while */ \ No newline at end of file +/* rozwiązanie z pętlą while */ + +const baseInput = prompt('Enter the base number for exponentiation (a): '); +const a = Number(baseInput); + +const exponentInput = prompt('Enter the exponent (n): '); +const n = Number(exponentInput); + +if (isNaN(a) || !Number.isInteger(a)) { + console.error(`Base must be an integer. Provided value: ${baseInput}`); +} else if (isNaN(n) || !Number.isInteger(n) || n < 1) { + console.error(`Exponent must be an integer greater than or equal to 1. Provided value: ${exponentInput}`); +} else { + let i = 0; + let result = 1; + let chain = ""; + + while (i < n) { + result *= a; + + if (i === 0) { + chain += a; + } else { + chain += " * " + a; + } + + i++; + } + + console.log(`${chain} = ${result}`); +} \ 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 00f8d18cfc05768017d1137f1d55785f1785d20e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Mako=CC=81wka?= Date: Sat, 15 Feb 2025 15:07:53 +0100 Subject: [PATCH 3/5] Task 03 --- 03/app.js | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/03/app.js b/03/app.js index 5b9361e4..b861185c 100644 --- a/03/app.js +++ b/03/app.js @@ -7,12 +7,45 @@ const c = randomNumber(min, max); console.log(a, b, c); - - - - - +function getSum(a, b, c) { + a = parseInt(a, 10); + b = parseInt(b, 10); + c = parseInt(c, 10); + + return a + b + c - Math.min(a, b, c); +} + +const isEven = function (a) { + if (typeof a !== 'number') return null; + + return a % 2 === 0; +}; + +function showInfo(a, b) { + let message; + + switch (b) { + case null: + message = `The provided argument ${a} is not a number.`; + break; + case true: + message = `The provided argument ${a} is even.`; + break; + case false: + message = `The provided argument ${a} is odd.`; + break; + default: + message = `Invalid value for the second parameter: ${b}`; + } + + console.log(message); +} function randomNumber(min, max) { return Math.round((Math.random() * (max - min)) + min); -} \ No newline at end of file +} + +const sum = getSum(a, b, c); +const even = isEven(sum); + +showInfo(sum, even); \ No newline at end of file From cc2eb44aea423890968de002904dc92ec0a34efb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Mako=CC=81wka?= Date: Sun, 16 Feb 2025 01:51:27 +0100 Subject: [PATCH 4/5] Task 04 --- 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..a561d2d4 --- /dev/null +++ b/04/app.js @@ -0,0 +1,33 @@ +const createArray = (min, max) => { + const arr = []; + + for (let i = 0; i < 20; i++) { + arr.push(getRandomInt(min, max)); + } + return arr; +}; + +const getRandomInt = (min, max) => { + return Math.floor(Math.random() * (max - min + 1)) + min; //Assumption: both min & max parameters are integers +}; + +const getLargest = arr => { + if (!Array.isArray(arr) || arr.length === 0) return null; + + return [...arr].sort((a, b) => b - a).slice(0, 10); +}; + + +const getAverage = arr => { + if (!Array.isArray(arr) || arr.length === 0) return null; + + return arr.reduce((sum, num) => sum + num, 0) / arr.length; +}; + +const testArr = createArray(10, 200); +const largestNumbersArray = getLargest(testArr); +const averageFromLargest = getAverage(largestNumbersArray); + +console.log(testArr); +console.log(largestNumbersArray); +console.log(averageFromLargest); \ 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 8de6290122ffbb177c83765f3e7db7138b731180 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Mako=CC=81wka?= Date: Sun, 16 Feb 2025 22:39:45 +0100 Subject: [PATCH 5/5] Task 05 --- 05/app.js | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ 05/index.html | 2 +- 2 files changed, 62 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..cfe3ff14 --- /dev/null +++ b/05/app.js @@ -0,0 +1,61 @@ +class Student { + constructor(firstName, lastName) { + this.firstName = firstName; + this.lastName = lastName; + this.grades = {}; + } + + // Static method to normalize subject names + static normalizeSubject(subject) { + return subject.trim().toLowerCase() //this could be adjusted further if we decide to handle spaces in subject names differently, perhaps by adding: .replace(/\s+/g, '_') + } + + // Static method to compute the average of an array of numbers. + static computeAverage(arr) { + if (!Array.isArray(arr) || arr.length === 0) return null; + + const sum = arr.reduce((acc, num) => acc + num, 0); + + return sum / arr.length; + } + + addGrade(subject, grade) { + if (typeof subject !== 'string' || subject.trim() === '') { + throw new Error('Subject must be a non-empty string'); + } + if (typeof grade !== 'number' || isNaN(grade)) { + throw new Error('Grade must be a valid number'); + } + + const subjectKey = Student.normalizeSubject(subject); + + if (!this.grades[subjectKey]) { + this.grades[subjectKey] = []; + } + + this.grades[subjectKey].push(grade); + } + + getAverageGrade(subject) { + const gradesToCalculate = (typeof subject === 'string' && subject.trim()) + ? (this.grades[Student.normalizeSubject(subject)] || []) + : Object.values(this.grades).flat(); + + return Student.computeAverage(gradesToCalculate); + } +} + +const student = new Student('Jan', 'Kowalski'); +student.addGrade('maths', 4); +student.addGrade('maths', 6); +student.addGrade('english', 3); +student.addGrade('english', 6); + +const avgMath = student.getAverageGrade('maths'); +const avgEnglish = student.getAverageGrade('english'); +const overallAvg = student.getAverageGrade(); + +console.log(student); +console.log('Average Maths:', avgMath); +console.log('Average English:', avgEnglish); +console.log('Overall Average:', overallAvg); \ 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