-
Notifications
You must be signed in to change notification settings - Fork 205
resolved tasks - js basics #155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
844d5b0
9fa2fb7
34c4a51
c9a81d7
11e493a
89c244d
17b9574
adbfe97
9dbb294
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,52 @@ | ||
| const a = '4.2'; | ||
| const b = 9; | ||
|
|
||
| console.log(a, b); | ||
| 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, '%')); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
|
||
| } else { | ||
|
|
||
| let i = 1; | ||
| let sum = a; | ||
| let equation = `${a}` | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Można po prostu zapisać |
||
|
|
||
| while (i < n){ | ||
| sum *= a; | ||
| equation += ` * ${a}`; | ||
| i++; | ||
| } | ||
|
|
||
| /* rozwiązanie z pętlą while */ | ||
| console.log(`${equation} = ${sum}`); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,6 @@ | |
| <title>devmentor.pl - JS BASICS - #02</title> | ||
| </head> | ||
| <body> | ||
|
|
||
| <script src = './app.js'></script> | ||
| </body> | ||
| </html> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
|
||
| } | ||
|
|
||
|
|
||
| const isEven = function(number){ | ||
|
|
||
| if (typeof(number) !== 'number'){ | ||
| return null; | ||
| } | ||
|
|
||
| function randomNumber(min, max) { | ||
| return Math.round((Math.random() * (max - min)) + min); | ||
| } | ||
| 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); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,6 @@ | |
| <title>devmentor.pl - JS BASICS - #04</title> | ||
| </head> | ||
| <body> | ||
|
|
||
| <script src = './app.js'></script> | ||
| </body> | ||
| </html> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,6 @@ | |
| <title>devmentor.pl - JS BASICS - #05</title> | ||
| </head> | ||
| <body> | ||
|
|
||
| <script src="./app.js"></script> | ||
| </body> | ||
| </html> | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍