-
Notifications
You must be signed in to change notification settings - Fork 205
practice-js-basics #157
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?
practice-js-basics #157
Changes from all commits
8913f24
406f4e6
f1aa4f8
1c0fb70
28a89d3
0eaad03
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,31 @@ | ||
| const a = '4.2'; | ||
| const a = "4.2"; | ||
| const b = 9; | ||
|
|
||
| console.log(a, b); | ||
| console.log(a, b); | ||
|
|
||
| console.log("Typem zmienne b jest:", typeof b); | ||
| console.log("Typem zmienne a jest:", typeof a); | ||
|
|
||
| const operations = ["+", "-", "*", "/", "%"]; | ||
|
|
||
| const results = operations.map((operation) => { | ||
| switch (operation) { | ||
| case "+": | ||
| return Number(a) + Number(b); | ||
| case "-": | ||
| return Number(a) - Number(b); | ||
| case "*": | ||
| return Number(a) * Number(b); | ||
| case "/": | ||
| return Number(a) / Number(b); | ||
| case "%": | ||
| return Number(a) % Number(b); | ||
| } | ||
| }); | ||
|
|
||
| results.forEach((result, index) => { | ||
| const operation = operations.at(index); | ||
| console.log( | ||
| `${a} ${operation} ${b} is ${result > 20 ? "greater" : "less"} than 20` | ||
| ); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,12 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="pl"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <meta http-equiv="X-UA-Compatible" content="ie=edge" /> | ||
| <title>devmentor.pl - JS BASICS - #01</title> | ||
| </head> | ||
| <body> | ||
|
|
||
| </body> | ||
| </html> | ||
| </head> | ||
| <body> | ||
| <script src="./assets/js/app.js"></script> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,36 @@ | ||
|
|
||
| /* rozwiązanie z pętlą for */ | ||
| const x = 4; | ||
| const x = prompt("Type number: "); | ||
|
|
||
| if (x && !isNaN(x)) { | ||
| for (let i = 1; i <= 9; i++) { | ||
| console.log(`${x} x ${i} = ${Number(x) * i}`); | ||
| } | ||
| } else { | ||
| console.error("Invalid input"); | ||
| } | ||
|
|
||
| /* rozwiązanie z pętlą while */ | ||
| let a; | ||
| let n; | ||
|
|
||
| while (!a || isNaN(a)) { | ||
| a = Number(prompt("Podstawa: ")); | ||
| } | ||
|
|
||
| while (!n || isNaN(n) || n <= 1) { | ||
| n = Number(prompt("Wykladnik:")); | ||
| } | ||
|
|
||
| let counter = 0; | ||
| let text = ""; | ||
|
|
||
| while (counter < n) { | ||
| text += a; | ||
|
|
||
| if (counter !== n - 1) text += " * "; | ||
| else text = text + " = " + Math.pow(a, n); | ||
|
|
||
| counter++; | ||
| } | ||
|
|
||
|
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 */ | ||
| console.log(text); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,12 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="pl"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <meta http-equiv="X-UA-Compatible" content="ie=edge" /> | ||
| <title>devmentor.pl - JS BASICS - #02</title> | ||
| </head> | ||
| <body> | ||
|
|
||
| </body> | ||
| </html> | ||
| </head> | ||
| <body> | ||
| <script src="./app.js"></script> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,12 +7,40 @@ const c = randomNumber(min, max); | |
|
|
||
| console.log(a, b, c); | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| function randomNumber(min, max) { | ||
| return Math.round((Math.random() * (max - min)) + min); | ||
| } | ||
| return Math.round(Math.random() * (max - min) + min); | ||
| } | ||
|
|
||
| function getSum(x, y, z) { | ||
| const numbers = [x, y, z] | ||
| .map((number) => parseInt(number)) | ||
| .sort((a, b) => b - a); | ||
|
|
||
| return numbers.at(0) + numbers.at(1); | ||
| } | ||
|
|
||
| const isEven = function (number) { | ||
| if (isNaN(number)) return null; | ||
|
|
||
| return number % 2 === 0; | ||
| }; | ||
|
|
||
| function showInfo(arg1, arg2) { | ||
| if (!(typeof arg2 === "boolean" || arg2 === null)) return; | ||
|
|
||
| switch (arg2) { | ||
| case null: | ||
| console.log(`Podany argument ${arg1} nie jest liczbą`); | ||
| case true: | ||
| console.log(`Podany argument ${arg1} jest parzysty`); | ||
| break; | ||
| case false: | ||
| console.log(`Podany argument ${arg1} jest nieparzysty`); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| const sum = getSum(a, b, c); | ||
| const even = isEven(sum); | ||
|
|
||
| showInfo(sum, even); | ||
|
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,38 @@ | ||
| function createArray(min, max) { | ||
| if ( | ||
| typeof min !== "number" || | ||
| typeof max !== "number" || | ||
| isNaN(min) || | ||
| isNaN(max) | ||
| ) { | ||
| throw new Error("min and max must be valid numbers."); | ||
| } | ||
|
|
||
| if (min > max) { | ||
| throw new Error("min must be less than or equal to max."); | ||
| } | ||
|
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 array = []; | ||
|
|
||
| for (let i = 0; i < 20; i++) { | ||
| const randomNumber = Math.random() * (max - min) + min; | ||
| array.push(Math.round(randomNumber)); | ||
| } | ||
|
|
||
| return array; | ||
| } | ||
|
|
||
| function getLargest(array) { | ||
| return array.sort((a, b) => b - a).slice(0, 10); | ||
| } | ||
|
|
||
| function getAvg(array) { | ||
| const sum = array.reduce((acc, cur) => acc + cur, 0); | ||
| return sum / array.length; | ||
| } | ||
|
|
||
| const arr = createArray(10, 200); | ||
| const largest = getLargest(arr); | ||
| const avg = getAvg(largest); | ||
|
|
||
| console.log(avg); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,12 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="pl"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <meta http-equiv="X-UA-Compatible" content="ie=edge" /> | ||
| <title>devmentor.pl - JS BASICS - #04</title> | ||
| </head> | ||
| <body> | ||
|
|
||
| </body> | ||
| </html> | ||
| </head> | ||
| <body> | ||
| <script src="./app.js"></script> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| 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) { | ||
| const calcAverage = (grades) => { | ||
| if (!grades || grades.length === 0) throw new Error("No grades found!"); | ||
| return grades.reduce((acc, cur) => acc + cur, 0) / grades.length; | ||
| }; | ||
|
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. 👍 |
||
|
|
||
| if (typeof subject === "undefined") { | ||
| const grades = Object.values(this.grades).flat(); | ||
| return Number(calcAverage(grades).toFixed(2)); | ||
| } | ||
|
|
||
| return calcAverage(this.grades[subject]); | ||
| }; | ||
|
|
||
| const student = new Student("Jan", "Kowalski"); | ||
| student.addGrade("maths", 4); | ||
| student.addGrade("maths", 6); | ||
| student.addGrade("english", 3); | ||
| const avgMath = student.getAverageGrade("maths"); | ||
| const avg = student.getAverageGrade(); | ||
|
|
||
| console.log(student); | ||
| console.log(avgMath); | ||
| console.log(avg); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,12 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="pl"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <meta http-equiv="X-UA-Compatible" content="ie=edge" /> | ||
| <title>devmentor.pl - JS BASICS - #05</title> | ||
| </head> | ||
| <body> | ||
|
|
||
| </body> | ||
| </html> | ||
| </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.
👍