-
Notifications
You must be signed in to change notification settings - Fork 10
Домашка #13
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
ManginAlexander
wants to merge
3
commits into
cripi-javascript:master
Choose a base branch
from
ManginAlexander: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
Домашка #13
Changes from 1 commit
Commits
Show all changes
3 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 |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| /*jslint nomen: true*/ | ||
| /*global alert: true*/ | ||
| /*global _testElAscending: true*/ | ||
| /*global _testElNotAscending: true*/ | ||
| /*global _testElAscendingAndEqualsElement: true*/ | ||
| /*global _runTestSortArray: true*/ | ||
| /*global _testElNotAscendingAndEqualsElement: true*/ | ||
| function bubbleSort(arr) { | ||
| "use strict"; | ||
| if (!((typeof (arr) === 'object') && (arr instanceof Array))) { | ||
|
||
| throw "You get me not array"; | ||
| } | ||
| var i = 0, j = 0, temp; | ||
| for (i = 0; i < arr.length; i += 1) { | ||
| for (j = 0; j < arr.length; j += 1) { | ||
| if (arr[i] < arr[j]) { | ||
| temp = arr[j]; | ||
| arr[j] = arr[i]; | ||
| arr[i] = temp; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| //Ниже функции для проверки сортировки | ||
| //За эталонную сортировку я взял Sort | ||
| //Да для этого лучше всего использовать библиотеку для Unit тестирования. | ||
| //Но мне было лениго в ней разбираться. ПОэтому написан небольшой свой велосипед | ||
|
|
||
| function runTestsSort() { | ||
| "use strict"; | ||
| var numberTest; | ||
| try { | ||
| numberTest = 1; | ||
| _testElAscending(); | ||
| numberTest += 1; | ||
| _testElNotAscending(); | ||
| numberTest += 1; | ||
| _testElAscendingAndEqualsElement(); | ||
| numberTest += 1; | ||
| _testElNotAscendingAndEqualsElement(); | ||
| numberTest += 1; | ||
| //_testNoArray(); | ||
| //numberTest++; | ||
| } catch (e) { | ||
| //Это конечно лучше писать в лог | ||
| alert("Тест " + numberTest + " упал"); | ||
| return; | ||
| } | ||
| //И это тоже | ||
| alert("Тесты пройдены успешно"); | ||
| } | ||
|
|
||
| function _testElAscending() { | ||
| "use strict"; | ||
| var array = [1, 2, 3, 4, 5]; | ||
| _runTestSortArray(array, bubbleSort); | ||
| } | ||
| function _testElNotAscending() { | ||
| "use strict"; | ||
| var array = [5, 4, 3, 2, 1]; | ||
| _runTestSortArray(array, bubbleSort); | ||
| } | ||
| function _testElAscendingAndEqualsElement() { | ||
| "use strict"; | ||
| var array = [1, 2, 3, 4, 4, 5]; | ||
| _runTestSortArray(array, bubbleSort); | ||
| } | ||
| function _testElNotAscendingAndEqualsElement() { | ||
| "use strict"; | ||
| var array = [5, 4, 3, 2, 2, 1]; | ||
| _runTestSortArray(array, bubbleSort); | ||
| } | ||
| function _testNoArray() { | ||
| "use strict"; | ||
| var array = 124; | ||
| _runTestSortArray(array, bubbleSort); | ||
| } | ||
| function _runTestSortArray(arr, algorithm) { | ||
| "use strict"; | ||
| var copy = [], i = 0; | ||
| for (i = 0; i < arr.length; i += 1) { | ||
| copy[i] = arr[i]; | ||
| } | ||
| copy.sort(); | ||
| algorithm(arr); | ||
| if (copy.length !== arr.length) { | ||
| throw "Your algorithm dont work!"; | ||
| } | ||
| for (i = 0; i < copy.length; i += 1) { | ||
| if (copy[i] !== arr[i]) { | ||
| throw "Your algorithm dont work!"; | ||
| } | ||
| } | ||
| } | ||
| } | ||
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,10 @@ | ||
| <html> | ||
| <head> | ||
| <script src="mysort.js" type="text/javascript"></script> | ||
|
|
||
| </head> | ||
| <body onload="runTestsSort();"> | ||
|
|
||
| </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.
вместо глобалов можно было сначало объявить функцию, а потом ее использовать те runTestsSort опустить в самый конец скрипта
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.
Но тогда код будет неопрятным. Так как в начале файла, модуля, класса должно быть самое важное. Ну это на мой взгляд.
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.
Код должен быть максимально самодокументированным. Используй тогда модуль заодно и от _ избавишься
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.
Писать комментарии? - мне кажется это избыточно в данной ситуации (так как не очевидного кода нет) - DRY
Писать тесты в которых описано как и что работает? - я это вроде бы написал в той или иной степени
Писать вики документацию? - когда я устраивался на стажировку мой код с вики документацией "обсмеяли" - DRY.
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.
самодокументированности кода - всмысле, что код должен быть понятен и без коментариев. Коментарии только для не очевидных штук (алгоритмы, датафлоу, пояснения к аргументам функции итп).
Совсем не посмотрел на имена функций - все они тесты. Оставь как есть. Не заморачивайся :)
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.
А можете выложить под тестовым пользователем, вашу версию данной задачи? хочется научиться чему, то хорошему.
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.
Вот что бы я сделал по максимуму:
@examplepackage.json(прописал бы все зависимости)test/*make testиnpm testmake docsnpm install && npm test(вобщем автоматом ставит и запускает базбраузерные тесты)Почти все это я сделал тут https://github.com/azproduction/lmd