Skip to content

Instantly share code, notes, and snippets.

@AlexDz27
AlexDz27 / index.php
Created September 5, 2023 11:41
Pdo in php (connect to db)
$host = 'localhost';
$db = 'test';
$user = 'root';
$pass = 'root';
$charset = 'utf8mb4';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
@AlexDz27
AlexDz27 / main.js
Created January 19, 2022 09:22
Even though setTimeout is faster, it still goes after slow operation
setTimeout(() => {
console.log('hello from timeout!')
})
let sum = 0
for (let i = 1; i <= 1000000000; i++) {
sum++
}
console.log(sum)
function getArrayFromLinkedList(linkedList) {
const arr = []
while (linkedList !== null) {
const value = linkedList.value
arr.push(value)
linkedList = linkedList.next
}
return arr
@AlexDz27
AlexDz27 / prototype.js
Created December 29, 2021 14:57
OttoFeller problem - prototype.js
/** Slightly changed problem description **/
// Какой вариант лучше и почему? Может оба неудачные?
// 1
function DumbConstructorA() { }
DumbConstructorA.prototype.dumbMethod = function() { return 123 }
// 2
function DumbConstructorB() {
this.dumbMethod = function() { return 123 }
}
@AlexDz27
AlexDz27 / poly.js
Last active December 30, 2021 09:54
OttoFeller problem - poly.js
// names может быть как массивом, так и строкой. Как избавиться от любых условий при выводе списка names?
function namesFunc(names) {
if(names instanceof Array)
console.log(names.join(', '));
else
console.log(names);
}
/*** Solution ***/
@AlexDz27
AlexDz27 / map.js
Created December 29, 2021 14:55
OttoFeller problem - map.js
// Как избавиться от переменной that в этом примере? То есть не сохранять явно контекст родителя в переменную.
function parent() {
var that = this;
that.multiplier = 3;
return [33, 77, 99, 81, 55].map(function(I) { return I * that.multiplier});
}
@AlexDz27
AlexDz27 / hasArrayAllSpecifiedKeys.php
Created December 13, 2021 11:52
Checks if array has all specified keys
/**
* Checks if array has all specified keys
*
* Returns false if some keys are missing and true if all keys are present
*
* @param $array
* @param $keys
* @return bool
*/
function hasArrayAllSpecifiedKeys($array, $keys) {
function delay(ms) {
const promise = new Promise((resolve) => {
setTimeout(() => resolve(), ms);
});
return promise;
}
delay(1000).then(() => console.log('asdsad'));
@AlexDz27
AlexDz27 / getMatchesCountOnStr.js
Created June 10, 2021 11:44
Get regexp pattern matches count on string
function getMatchesCountOnStr(str, pattern) {
// Find all occurrences, case-insensitive
const regexp = new RegExp(pattern, 'ig');
return str.match(regexp)?.length;
}
// Matches all HTML elements, even if they have attributes
let regexp1 = /<\w+\s?.*?>.*?<\/\w+>/g;
let str1 = '<span class="qweqwe"></span> <h1>asd</h1>';
console.log( str1.match(regexp1) ); // (2) ["<span class="qweqwe"></span>", "<h1>asd</h1>"]