diff --git a/01/assets/js/app.js b/01/assets/js/app.js index 9323b98d..25f9ac39 100644 --- a/01/assets/js/app.js +++ b/01/assets/js/app.js @@ -1,4 +1,80 @@ const a = '4.2'; -const b = 9; +const b = 20; +const c = parseFloat(a); + +console.log(a, b); +console.log(typeof a); +console.log(typeof b); + +const addVariable = c + b; +console.log(addVariable); + +const concatVariable = a + b; +console.log(concatVariable); + +const subtractVariable = a - b; +console.log(subtractVariable); + +const multiVariable = a * b; +console.log(multiVariable); + +const divideVariable = a / b; +console.log(divideVariable); + +const modVariable = Number(a) % b; +console.log(modVariable); + +let incrementVar = a; +incrementVar++; +console.log(incrementVar); + +let decrementVar = b; +decrementVar--; +console.log(decrementVar); + + +if (addVariable > 20) { + console.log('a+b jest wieksze od 20') +} else { + console.log('a+b jest mniejsze od 20') +} + +if (subtractVariable > 20) { + console.log('a-b jest wieksze od 20') +} else { + console.log('a-b jest mniejsze od 20') +} + +if (multiVariable > 20) { + console.log('a*b jest wieksze od 20') +} else { + console.log('a*b jest mniejsze od 20') +} + +if (divideVariable > 20) { + console.log('a/b jest wieksze od 20') +} else { + console.log('a/b jest mniejsze od 20') +} + +if (modVariable > 20) { + console.log('a%b jest wieksze od 20') +} else { + console.log('a%b jest mniejsze od 20') +} + +if (incrementVar > 20) { + console.log('a++ jest wieksze od 20') +} else { + console.log('a++ jest mniejsze od 20') +} + +if (decrementVar > 20) { + console.log('a-- jest wieksze od 20') +} else { + console.log('a-- jest mniejsze od 20') +} + +console.log(); +console.log(); -console.log(a, b); \ No newline at end of file diff --git a/01/index.html b/01/index.html index 5c55d687..b014de3a 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..f0116eea 100644 --- a/02/app.js +++ b/02/app.js @@ -1,7 +1,42 @@ /* rozwiązanie z pętlą for */ -const x = 4; +const x = prompt('Podaj liczbe z przedzialu od 1 do 9!'); +let result; +for (let i = 1; i <= 9; i++) { + if (x <= 9 && x > 0) { + result = i * x; + console.log(x + ' x ' + i + ' = ' + result); + } else { + + alert('Wybrana liczba nie jest z przedzialu 1-9'); + + } +} + +/* rozwiązanie z pętlą while */ + +const a = parseInt(prompt('Podaj liczbe z przedzialu od 1 do 9')); +const n = parseInt(prompt('Podaj liczbe z przedzialu od 1 do 9')); +let result2 = 1 +// console.log(result) + + +let j = 0; +let str = ''; +if (a <= 9 && a > 0 && n <= 9 && n > 0) { + while (j < n) { + j++; + if (n === j) { + str += a; + } else { + str += a + '*'; + } + } + console.log(str + '=' + Math.pow(a, n)); +} +else { + alert('Wybrana liczba nie jest z przedzialu 1-9'); +} -/* rozwiązanie z pętlą while */ \ No newline at end of file diff --git a/02/index.html b/02/index.html index 30150099..12794631 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..280d99b7 100644 --- a/03/app.js +++ b/03/app.js @@ -1,18 +1,48 @@ -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 randomNumber(min, max) { - return Math.round((Math.random() * (max - min)) + min); -} \ No newline at end of file +const a = 41.5234; +const b = 25; +const c = 14; + +const sum = getSum(a, b, c); + + +function getSum(a, b, c) { + let largestNum = Math.max(a, b, c); + let secondLargestNum; + + if (largestNum === a) { + secondLargestNum = Math.max(b, c); + } else if (largestNum === b) { + secondLargestNum = Math.max(a, c); + } else { + secondLargestNum = Math.max(a, b); + } + return sum1 = parseInt(largestNum) + parseInt(secondLargestNum); +} + +const even = isEven(2); + +function isEven(num) { + if (typeof num !== "number") { + return null; + } else if (num % 2 === 0) { + return true; + } else { + return false; + } +} + +function showInfo(para1, para2) { + switch (para2) { + case null: + console.log('Podany argument ' + para1 + ' nie jest liczbą'); + break; + case true: + console.log('Podany argument ' + para1 + ' jest parzystybą'); + break; + case false: + console.log('Podany argument ' + para1 + ' jest nieparzysty'); + break; + } +} + +showInfo(sum, even); \ No newline at end of file diff --git a/04/app.js b/04/app.js new file mode 100644 index 00000000..9c14302e --- /dev/null +++ b/04/app.js @@ -0,0 +1,43 @@ +function createArray(min, max) { + const arr = []; + for (let i = 1; i <= 20; i++) { + const randomNumber = Math.floor(Math.random() * (max - min + 1)) + min; + arr.push(randomNumber); + } + return arr; +} + +const arr = createArray(10, 200); +console.log(arr); + + + +function getLargest() { + arr.sort(function (a, b) { + return b - a; + }); + return arr.slice(0, 10); + +} +const largest = getLargest(arr); +console.log(largest); + + + +function getAvg(arr) { + if (arr.length === 0) { + return 0; + } + let sum = 0; + arr.forEach(num => { + sum += num; + }); + return sum / arr.length; + +} + +const avg = getAvg(largest); +console.log(avg); + + + diff --git a/04/index.html b/04/index.html index 06aebbb8..961a6455 100644 --- a/04/index.html +++ b/04/index.html @@ -1,12 +1,21 @@ + - - + + 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..8f9f018a --- /dev/null +++ b/05/app.js @@ -0,0 +1,74 @@ +// Tym razem stworzysz konstruktor, na podstawie którego będzie można generować obiekt przechowujący informacje o studencie. + +// Wymagania: +// 1. Obiekt posiada imię i nazwisko przekazywane podczas inicjalizacji (przy użyciu `new`). + +function Student(initFirstName, initLastName) { + 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); +}; + +// 2. Konstruktor: +// - udostępnia metody (przez `prototype`), +// - metody te pozwalają: +// - dodać ocenę z danego przedmiotu, +// - obliczyć średnią arytmetyczną z konkretnego przedmiotu, +// - obliczyć ogólną średnią arytmetyczną. + +Student.prototype.getAverageGrade = function (subject) { + if (typeof subject !== 'undefined') { + const grades = this.grades[subject]; + if (!grades || grades.length === 0) return null; + console.log(grades); + let sum = 0; + for (let i = 0; i < grades.length; i++) { + sum += grades[i]; + } + return (sum / grades.length); + } + + let totalSum = 0; + let totalCount = 0; + console.log(this.grades); + for (let subjectName in this.grades) { + const subjectGrades = this.grades[subjectName]; + console.log(subjectGrades); + for (let i = 0; i < subjectGrades.length; i++) { + totalSum += subjectGrades[i]; + totalCount++; + } + } + if (totalCount === 0) return null; + + return ((totalSum / totalCount).toFixed(2)); +} + +const maths = 4; + +const student = new Student('Jan', 'Kowalski'); +console.log(student) +student.addGrade('maths', 4); +student.addGrade('maths', 6); +student.addGrade('english', 3); +const avgMath = student.getAverageGrade('maths'); // 5 +const avg = student.getAverageGrade(); // 4.33 + +console.log(avgMath); +console.log(avg) + + +// Zwróć uwagę, że pobranie informacji o ocenach z konkretnego przedmiotu może się odbywać w ten sposób: +// ``` +// const subject = 'maths'; +// const grades = this.grades[subject]; +// ``` + diff --git a/05/index.html b/05/index.html index 52a42709..996c6de0 100644 --- a/05/index.html +++ b/05/index.html @@ -1,12 +1,21 @@ + - - + + devmentor.pl - JS BASICS - #05 + - + + \ No newline at end of file