diff --git a/01/assets/js/app.js b/01/assets/js/app.js index 9323b98d..2785928f 100644 --- a/01/assets/js/app.js +++ b/01/assets/js/app.js @@ -1,4 +1,25 @@ const a = '4.2'; const b = 9; +console.log(a, b); +console.log(typeof a, typeof b); +const add = parseFloat(a) + b; +console.log(add); -console.log(a, b); \ No newline at end of file +const sub = a - b; +console.log(sub); + +const multiply = a * b; +console.log(multiply.toFixed(2)); + +const divided = a / b; +console.log(divided.toFixed(2)); + +const arr = [add, sub, multiply, divided]; + +arr.forEach(function (num) { + if (num > 20) { + console.log(`${num.toFixed(2)} jest większe od 20`); + } else { + console.log(`${num.toFixed(2)} jest mniejsze lub równe 20`); + } +}); \ 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 diff --git a/02/app.js b/02/app.js index b397190c..9bae31e0 100644 --- a/02/app.js +++ b/02/app.js @@ -1,7 +1,33 @@ +const x = Number(prompt('Podaj liczbę!')); -/* rozwiązanie z pętlą for */ -const x = 4; +if (x >= 1 && x < 10) { + for (let i = 1; i <= 9; i++) { + res = i * x; + console.log(`${i} x ${x} = ${res}`); + } +} else { + console.log('Podałeś błędne dane!'); +} +/* rozwiązanie z pętlą while */ +const a = Number(prompt('Podaj liczbę z przedziału 1-100!')); +const n = Number(prompt('Podaj potęgę od 0-10!')); - -/* rozwiązanie z pętlą while */ \ No newline at end of file +if (a > 1 && a < 100 && n > 0 && n <= 10) { + let i = 0; + let result = 1; + let info = ''; + while (i < n) { + result *= a; + if (i > 0) { + info += ' * '; + } + info += a; + i++; + } + info = info + ' = ' + result; + console.log(result); + console.log(info); +} else { + console.log('Podałeś błędne dane!'); +} \ 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 diff --git a/03/app.js b/03/app.js index 5b9361e4..dd96760f 100644 --- a/03/app.js +++ b/03/app.js @@ -1,18 +1,43 @@ -const min = 1; -const max = 100; - -const a = randomNumber(min, max); -const b = randomNumber(min, max); -const c = randomNumber(min, max); - -console.log(a, b, c); - - - - - - - +function getSum(a, b, c) { + const aInt = parseInt(a); + const bInt = parseInt(b); + const cInt = parseInt(c); + + const arr = [aInt, bInt, cInt]; + arr.sort(function (a, b) { + return b - a; + }); + return arr[0] + arr[1]; +} + +function isEven(num) { + if (typeof num !== 'number') { + return null; + } + return num % 2 === 0; +} + +console.log(isEven(2), isEven(1), isEven('r')); + +function showInfo(value, isEven) { + switch (isEven) { + case null: + console.log(`${value} nie jest liczbą`); + + break; + case true: + console.log(`Liczba ${value} jest parzysta`); + + break; + case false: + console.log(`Liczba ${value} jest nieparzysta`); + break; + } +} + +const sum = getSum(a, b, c); +const even = isEven(sum); +showInfo(sum, even); function randomNumber(min, max) { - return Math.round((Math.random() * (max - min)) + min); + return Math.round(Math.random() * (max - min) + min); } \ No newline at end of file diff --git a/04/app.js b/04/app.js new file mode 100644 index 00000000..2ffa9372 --- /dev/null +++ b/04/app.js @@ -0,0 +1,32 @@ +const arr = createArray(10, 200); +const largest = getLargest(arr); +const avg = getAvg(largest); + +console.log(arr, largest, avg); + +function createArray(min, max) { + const arr = []; + + for (let i = 0; i < 20; i++) { + const num = Math.round(Math.random() * (max - min) + min); + arr.push(num); + } + return arr; +} + +function getLargest(arr) { + arr.sort(function (a, b) { + return b - a; + }); + return arr.slice(0, 10); +} + +function getAvg(arr) { + if (arr.length === 0) { + return 0; + } + const sum = arr.reduce(function (acc, curr) { + return acc + curr; + }, 0); + return sum / arr.length; +} \ 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..d8b0e4e5 --- /dev/null +++ b/05/app.js @@ -0,0 +1,50 @@ +function Student(fName, lName) { + this.firstName = fName; + this.lastName = lName; + 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 subject === 'undefined') { + const grades = []; + for (const grade in this.grades) { + const arr = this.grades[grade]; + arr.forEach(function (item) { + grades.push(item); + }); + } + + return this.avg(grades); + } + if (typeof this.grades[subject] === 'undefined') { + return 0; + } + const grades = this.grades[subject]; + return this.avg(grades); +}; + +Student.prototype.avg = function (arr) { + if (arr.length === 0) { + return 0; + } + const sum = arr.reduce(function (acc, curr) { + return acc + curr; + }, 0); + return sum / arr.length; +}; +const student = new Student('Jan', 'Kowalski'); +student.addGrade('math', 4); +student.addGrade('math', 2); +student.addGrade('german', 1); +student.addGrade('german', 3); +student.addGrade('german', 2); +student.addGrade('german', 5); +console.log(student.getAverageGrade('math')); +console.log(student.getAverageGrade('german')); \ 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