-
Notifications
You must be signed in to change notification settings - Fork 205
practice-js-basics done #166
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
Open
satoshi300
wants to merge
5
commits into
devmentor-pl:master
Choose a base branch
from
satoshi300:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| } | ||
| 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); | ||
|
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. 👍 |
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
|
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. Warto się zabezpieczyć czy nie dzielimy przez 0 :) |
||
|
|
||
| } | ||
|
|
||
| const avg = getAvg(largest); | ||
| console.log(avg); | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,21 @@ | ||
| <!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"> | ||
| <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> | ||
|
|
||
| <script src="./app.js"></script> | ||
| </body> | ||
|
|
||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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]; | ||
| // ``` | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,21 @@ | ||
| <!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"> | ||
| <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> | ||
|
|
||
| <script src='./app.js'></script> | ||
| </body> | ||
|
|
||
| </html> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
👍