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
| const str = "Строчка с 3 числами в ней... 42 и 88."; | |
| const numRe = /\b(\d+)\b/g; | |
| let match; | |
| while (match = numRe.exec(str)) { | |
| console.log("Нашёл ", match[1], " на ", match.index); | |
| } | |
| // Нашёл 3 на 10 | |
| // Нашёл 42 на 29 |
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
| let str = "multiline\nstring"; | |
| let lines = str.split(/\r?\n/); |
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
| function iterate(from, to, cb) { | |
| let diff = to > from ? 1 : -1; | |
| for (let i = from * diff; i <= to * diff; i++) { | |
| cb(i / diff); | |
| } | |
| } |
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
| Array.prototype.shuffle = function() { | |
| let arr = [...this]; | |
| let m = this.length; | |
| while(m) { | |
| const i = Math.floor(Math.random() * m--); | |
| [arr[m], arr[i]] = [arr[i], arr[m]]; | |
| } | |
| return arr; | |
| } |
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
| function isInside(node, target) { | |
| for (; node != null; node = node.parentNode) | |
| if (node == target) return true; | |
| } |
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
| var bar = document.querySelector(".progress div"); | |
| addEventListener("scroll", function() { | |
| var max = document.body.scrollHeight - innerHeight; | |
| var percent = (pageYOffset / max) * 100; | |
| bar.style.width = percent + "%"; | |
| }); |
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
| git config --global user.name "John Doe" | |
| git config --global user.email johndoe@example.com |
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
| let xhr = new XMLHttpRequest(); | |
| xhr.open('GET', '/my/url', true); | |
| xhr.send(); | |
| xhr.onreadystatechange = function() { | |
| if (this.readyState != 4) return; | |
| if (this.status != 200) { | |
| console.log( 'ошибка: ' + (this.status ? this.statusText : 'запрос не удался') ); | |
| return; | |
| } |
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
| let xhr = new XMLHttpRequest(); | |
| let body = JSON.stringify({}); | |
| xhr.open('POST', '/my/url', true); | |
| xhr.setRequestHeader('Content-type', 'application/json; charset=utf-8'); | |
| xhr.send(body); | |
| xhr.onreadystatechange = function() { | |
| if (this.readyState != 4) return; | |
| if (this.status != 200) { | |
| console.log( 'ошибка: ' + (this.status ? this.statusText : 'запрос не удался') ); | |
| return; |
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
| // Возвращает случайное целое число между min (включительно) и max (не включая max) | |
| // Использование метода Math.round() даст вам неравномерное распределение! | |
| function getRandomInt(min, max) { | |
| return Math.floor(Math.random() * (max - min)) + min; | |
| } |