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
/** | |
* Функция, возвращающая случайный элемемент массива | |
* https://learn.javascript.ru/array | |
* @param {array} array | |
* @return {*} | |
*/ | |
function getRandomElement(array) { | |
let randomIndex = Math.floor(Math.random() * array.length); | |
return array[randomIndex]; | |
} |
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, включая min, max как возможные значения | |
* https://learn.javascript.ru/task/random-int-min-max | |
* @param {number} min | |
* @param {number} max | |
* @return {number} | |
*/ | |
function getRandomInRange(min, max) { | |
let rand = min + Math.random() * (max + 1 - min); | |
rand = Math.floor(rand); |
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
/** | |
* Функция возвращает массив со случайно перемешанными элементами | |
* https://habr.com/ru/post/358094/ | |
* @param {array} arr | |
* @return {array} | |
*/ | |
function shuffle(arr){ | |
let j, temp; | |
for(let i = arr.length - 1; i > 0; i--){ | |
j = Math.floor(Math.random()*(i + 1)); |
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
/** | |
* Функция возвращает массив с уникальными элементами | |
* Только в ES6 https://webformyself.com/kak-proizvesti-udalenie-dublej-massiva-v-es6/ | |
* @param {array} arr | |
* @return {any[]} | |
*/ | |
function returnUniqueArray(arr) { | |
return Array.from(new Set(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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Title</title> | |
</head> | |
<body> | |
<button> | |
Click me! → And see console! | |
</button> |
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
/** | |
* Возвращает ключ объекта по его значению. Нет функционала в js ? | |
* Вернет только первый ключ, если одинаковых значений несколько? | |
* @param {object} obj | |
* @param {any} value | |
* @return {string | undefined} | |
*/ | |
const getKeyByValue = (obj, value) => | |
Object.keys(obj).find((key) => obj[key] === value); |
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
/** | |
* Вызывает переданную ей функцюи с задержкой | |
* https://learn.javascript.ru/task/delay | |
* @param f | |
* @param ms | |
* @return {Function} | |
*/ | |
export const delay = function (f, ms) { | |
return function () { |
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
/** | |
* Запускать функцию с рендомным интервалом, в заданном диапазоне | |
* https://stackoverflow.com/questions/34656758/javascript-setinterval-with-random-time?rq=1 | |
*/ | |
function myFunction() { | |
const min = 5; | |
const max = 10; | |
let rand = Math.floor(Math.random() * (max - min + 1) + min); // Generate Random number between 5 - 10 | |
alert(`Wait for ` + rand + ` seconds`); | |
setTimeout(myFunction, rand * 1000); |
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
/** | |
* Запускать функцию с рендомным интервалом, в заданном диапазоне - упрощенный вариант | |
* https://stackoverflow.com/questions/34656758/javascript-setinterval-with-random-time?rq=1 | |
*/ | |
function myFunction() { | |
alert(`oo`); | |
setTimeout(myFunction, Math.random() * 5000); | |
} | |
myFunction(); |
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 delay = ms => new Promise(resolve => setTimeout(resolve, ms)); | |
async function something() { | |
console.log("this might take some time...."); | |
await delay(5000); | |
console.log("done!") | |
} | |
something(); |
OlderNewer