diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 00000000..0747df2a Binary files /dev/null and b/.DS_Store differ diff --git a/01/assets/js/app.js b/01/assets/js/app.js index 9323b98d..8c17421d 100644 --- a/01/assets/js/app.js +++ b/01/assets/js/app.js @@ -1,4 +1,52 @@ const a = '4.2'; const b = 9; -console.log(a, b); \ No newline at end of file +console.log(a, b); +console.log(typeof(a), typeof(b)); + +function performAction(number1, number2, mathOperation) { + + const numA = parseFloat(number1); + const numB = parseFloat(number2); + + switch (mathOperation) { + case '+': + result = numA + numB; + break; + + case '-': + result = numA - numB; + break; + + case '*': + result = numA * numB; + break; + + case '/': + result = numA / numB; + break; + + case '%': + result = numA % numB; + break; + + default: + result = 'Niepoprawna operacja'; + } + + if (result > 20){ + console.log('Wynik operacji jest większy od 20') + } else if (result === 20) { + console.log('Wynik operacji wynosi 20'); + } else { + console.log('Wynik operacji jest mniejszy od 20') + } + + return result; +} + +console.log(performAction(a, b, '+')); +console.log(performAction(a, b, '-')); +console.log(performAction(a, b, '*')); +console.log(performAction(a, b, '/')); +console.log(performAction(a, b, '%')); \ 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..96d03b2e 100644 --- a/02/app.js +++ b/02/app.js @@ -1,7 +1,40 @@ - /* rozwiązanie z pętlą for */ -const x = 4; +// const x = prompt('Podaj liczbę!'); + +// if (x > 9 || x < 1){ +// alert('Wartość powinna mieścić się w przedziale 1-9! Podaj cyfrę!'); +// location.reload(); +// } + +// for (let i = 0; i < 10; i++){ +// console.log(`${x} * ${i} = ${x*i}`); +// } + + +/* rozwiązanie z pętlą while */ + +const a = prompt('Podaj podstawę!'); +const n = prompt('Podaj wykładnik!'); + +if (a > 9 || a < 0 || n < 0 || n > 10){ + alert('Wartość powinna mieścić się w przedziale 1-9! Podaj cyfrę!'); + location.reload(); + +} else if (n == 0) { + console.log(`${a}^${n} = 1`); + //zostawilem w tym przypadku zapis a^n, poniewaz a^0 = 1, ciezko to inaczej zapisac + +} else { + let i = 1; + let sum = a; + let equation = `${a}` + while (i < n){ + sum *= a; + equation += ` * ${a}`; + i++; + } -/* rozwiązanie z pętlą while */ \ No newline at end of file + console.log(`${equation} = ${sum}`); +} \ No newline at end of file diff --git a/02/index.html b/02/index.html index 30150099..5659652c 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..a08cb9d5 100644 --- a/03/app.js +++ b/03/app.js @@ -7,12 +7,55 @@ const c = randomNumber(min, max); console.log(a, b, c); +function randomNumber(min, max) { + return Math.round((Math.random() * (max - min)) + min); +} +function getSum(a, b, c) { + const array = [parseInt(a), parseInt(b), parseInt(c)]; + const sortedArray = array.sort((a ,b) => b - a); + const arrayWith2Elements = sortedArray.slice(0, 2); + const sum = arrayWith2Elements.reduce((acc, el) => acc + el, 0); + return sum; + +} +const isEven = function(number){ + if (typeof(number) !== 'number'){ + return null; + } -function randomNumber(min, max) { - return Math.round((Math.random() * (max - min)) + min); -} \ No newline at end of file + if(number%2 === 0){ + return true; + } else { + return false; + } +} + +function showInfo(value, boolean){ + + switch(value, boolean){ + case null: + console.log(`Podany argument ${value} nie jest liczbą`); + break; + + case true: + console.log(`Podany argument ${value} jest parzysty`); + break; + + case false: + console.log(`Podany argument ${value} jest nieparzysty`); + break; + } +} + +const sum = getSum(a, b, c); +console.log(sum); + +const isSumEven = isEven(sum); +console.log(isSumEven); + +showInfo(sum, isSumEven); \ No newline at end of file diff --git a/04/app.js b/04/app.js new file mode 100644 index 00000000..896b99c9 --- /dev/null +++ b/04/app.js @@ -0,0 +1,33 @@ +function createArray(min, max){ + + const array = []; + + for (let i = 0; i < 20; i++){ + const randomNumber = Math.round((Math.random() * (max - min)) + min); + array.push(randomNumber); + } + + return array; +} + +function sortArray(array){ + const sortedArray = array.sort((a, b) => b - a); + const highestNumbers = sortedArray.slice(0, 10); + return highestNumbers; +} + +function getAvg(array){ + const sum = array.reduce((acc, element) => acc + element, 0); + const avg = sum / array.length; + return avg; +} + +const arr = createArray(10, 200); +console.log(arr); + +const sort = sortArray(arr); +console.log(sort); + +const avg =getAvg(sort); +console.log(avg); + diff --git a/04/index.html b/04/index.html index 06aebbb8..d8d84401 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..8ac84e1c --- /dev/null +++ b/05/app.js @@ -0,0 +1,42 @@ +function Student(firstName, lastName){ + this.firstName = firstName; + this.lastName = lastName; + 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 this.grades[subject] === 'undefined') { + + const allGrades = Object.values(this.grades).flat(); + const generalSum = allGrades.reduce((acc, element) => acc + element); + const generalAvg = (generalSum / allGrades.length).toFixed(2); + return generalAvg; + } + + const sum = this.grades[subject].reduce((acc, element) => acc + element); + const avg = sum / this.grades[subject].length; + return avg; +} + +const student = new Student('Jan', 'Kowalski'); +student.addGrade('math', 4); +student.addGrade('math', 6); +student.addGrade('english', 3); + +const avgMath = student.getAverageGrade('math'); +console.log(avgMath); + +const avg = student.getAverageGrade(); +console.log(avg); + +console.log(student); \ 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