-
Notifications
You must be signed in to change notification settings - Fork 205
practis-js -basic #169
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?
practis-js -basic #169
Changes from all commits
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,44 @@ | ||
| const a = '4.2'; | ||
| const b = 9; | ||
|
|
||
| console.log(a, b); | ||
| const y = parseFloat(a); | ||
|
|
||
| console.log('a = ' + typeof(a)); | ||
| console.log('b = ' + typeof(b)); | ||
| console.log('y = ' + typeof(y)); | ||
|
|
||
| console.log(a, b); | ||
|
|
||
| const res1 = b + y; | ||
| const res2 = a - b; | ||
| const res3 = a * b; | ||
| const res4 = a / b; | ||
| const res5 = b % y; | ||
| let res6 = 1; res6++; | ||
| let res7 = 2; res7--; | ||
| const res8 = a + b; | ||
|
|
||
| console.log('konkatenacja: ' + res8); | ||
| console.log('Dodawanie: ' + res1); | ||
| console.log('Odejmowanie: ' + res2); | ||
| console.log('Mnożenie: ' + res3); | ||
| console.log('Dzielenie: ' + res4); | ||
| console.log('Reszta z dzielenia: ' + res5); | ||
| console.log('Inkrementacja: ' + res6); | ||
| console.log('Dekrementacja: ' + res7); | ||
|
|
||
|
|
||
| function checkResult(name, value) { | ||
| if (value > 20) { | ||
| console.log(name + value + ' jest większy od 20'); | ||
| } else { | ||
| console.log(name + value + ' jest mniejszy od 20'); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| checkResult('Dodawanie', res1); | ||
| checkResult('Odejmowanie', res2); | ||
| checkResult('Mnożenie ', res3); | ||
| checkResult('Dzielenie', res4); | ||
| checkResult('Reszta z dzielenia', res5); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,33 @@ | ||
|
|
||
| /* rozwiązanie z pętlą for */ | ||
| const x = 4; | ||
| const x = prompt('Podaj numer'); | ||
|
|
||
|
|
||
| for(i=1; i<10; i++){ | ||
|
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. Uwaga na słowo kluczowe |
||
| console.log(x + 'x' + i + '=' + i * x); | ||
| } | ||
|
|
||
| /* rozwiązanie z pętlą while */ | ||
|
|
||
| const a = parseInt(prompt('Podaj podstawe')); | ||
| const n = parseInt(prompt('Podaj wykładnik')); | ||
|
|
||
|
|
||
| if(isNaN(a) || isNaN(n) || n < 0 ){ | ||
| console.log('podaj poprawne liczby'); | ||
| }else { | ||
| let result = 1; | ||
| let expression = ""; | ||
| let j = 0; | ||
|
|
||
| while (j < n) { | ||
| result *= a; | ||
| expression += a; | ||
| if (j < n - 1) { | ||
| expression += " * "; | ||
| } | ||
| j++; | ||
| } | ||
| console.log(expression + " = " + result); | ||
| } | ||
|
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. 👍 |
||
|
|
||
| /* rozwiązanie z pętlą while */ | ||
| 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 |
|---|---|---|
|
|
@@ -5,10 +5,46 @@ const a = randomNumber(min, max); | |
| const b = randomNumber(min, max); | ||
| const c = randomNumber(min, max); | ||
|
|
||
| console.log(a, b, c); | ||
|
|
||
|
|
||
|
|
||
| const result = getSum(a, b, c); | ||
| const even = isEven(result); | ||
|
|
||
| showInfo(result, even) | ||
|
|
||
|
|
||
|
|
||
| function getSum(a, b, c){ | ||
| a = parseInt(a); | ||
| b = parseInt(b); | ||
| c = parseInt(c); | ||
|
|
||
| const sorted = [a, b, c].sort((x, y) => y - x); | ||
|
|
||
| return sorted[0] + sorted[1]; | ||
| } | ||
|
|
||
| function isEven(num) { | ||
| if (typeof num !== "number") { | ||
| return null; | ||
| } | ||
| return num % 2 === 0; | ||
| } | ||
|
|
||
|
|
||
| function showInfo(value, flag) { | ||
| switch (flag) { | ||
| 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; | ||
| default: | ||
| console.log("Nieprawidłowa wartość drugiego parametru"); | ||
| } | ||
| } | ||
|
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 arr = []; | ||
| for (let i = 0; i < 20; i++) { | ||
| const random = Math.floor(Math.random() * (max - min + 1)) + min; | ||
| arr.push(random); | ||
| } | ||
| return arr; | ||
| } | ||
|
|
||
|
|
||
| function getLargest(arr) { | ||
| return arr | ||
| .sort((a, b) => b - a) | ||
| .slice(0, 10) | ||
| } | ||
|
|
||
| function getAvg(arr) { | ||
| const sum = arr.reduce((acc, num) => acc + num, 0); | ||
| return sum / arr.length; | ||
| } | ||
|
|
||
|
|
||
| const arr = createArray(10, 200); | ||
| console.log( 'Tabela ', arr); | ||
|
|
||
| const largest = getLargest(arr); | ||
| console.log(largest); | ||
|
|
||
| const avg = getAvg(largest); | ||
| console.log("Średnia arytmetyczna ", avg); | ||
|
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 - #04</title> | ||
| </head> | ||
| <body> | ||
|
|
||
| <script src="app.js"></script> | ||
| </body> | ||
| </html> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| 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 subject !== 'undefined') { | ||
|
|
||
| const grades = this.grades[subject]; | ||
| if (!grades || grades.length === 0) return 0; | ||
|
|
||
| const sum = grades.reduce((acc, g) => acc + g, 0); | ||
| return (sum / grades.length).toFixed(2); | ||
|
Comment on lines
+20
to
+23
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. Zwróć uwagę, że zaznaczona część jest dość podobna do tej z lini 30-33. Można spróbować napisać metodę i uruchomić w obu miejscach - wtedy nie będzie duplikacji kodu. Traktuj to jako zadanie dodatkowe, które nie jest konieczne. |
||
| } else { | ||
|
|
||
| let allGrades = []; | ||
| for (const key in this.grades) { | ||
| allGrades = allGrades.concat(this.grades[key]); | ||
| } | ||
| if (allGrades.length === 0) return 0; | ||
|
|
||
| const sum = allGrades.reduce((acc, g) => acc + g, 0); | ||
| return (sum / allGrades.length).toFixed(2); | ||
| } | ||
| }; | ||
|
|
||
| Student.prototype.getFullName = function() { | ||
| return `${this.firstName} ${this.lastName}`; | ||
| }; | ||
|
|
||
|
|
||
| const student = new Student('Jan', 'Kowalski'); | ||
|
|
||
| student.addGrade('maths', 4); | ||
| student.addGrade('maths', 6); | ||
| student.addGrade('english', 3); | ||
|
|
||
|
|
||
| console.log(student.getFullName()); | ||
| console.log(student.getAverageGrade('maths')); | ||
| console.log(student.getAverageGrade()); | ||
| 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.
👍