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 diff --git a/02/app.js b/02/app.js index b397190c..0ca98105 100644 --- a/02/app.js +++ b/02/app.js @@ -1,7 +1,37 @@ /* rozwiązanie z pętlą for */ -const x = 4; +const x = Number(prompt('Podaj liczbę od 1 do 9!')); +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}`); + } +} -/* 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}`); +} + 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 diff --git a/03/app.js b/03/app.js index 5b9361e4..8dd8d18c 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) { diff --git a/04/app.js b/04/app.js new file mode 100644 index 00000000..9f076067 --- /dev/null +++ b/04/app.js @@ -0,0 +1,28 @@ + +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) { + 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 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 diff --git a/05/app.js b/05/app.js new file mode 100644 index 00000000..b34f409b --- /dev/null +++ b/05/app.js @@ -0,0 +1,41 @@ +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.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) { + return this.calculateAverage(this.grades[subject]); + } else { + const allGrades = Object.values(this.grades).flat(); + return this.calculateAverage(allGrades); + } +}; + +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