Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@npgenx
npgenx / mean_mode.js
Last active April 30, 2022 00:38
js: Get mean & mode
const statsFinder = (array) => {
const total = array.reduce((nextitem, previtem) => (nextitem + previtem),0);
const mean = total / array.length;
const getMode = (array) => {
const modeObject = {};
array.forEach(number => {
modeObject[number] = (!modeObject[number]) ? 1 : modeObject[number]+1;
});
return +Object.keys(modeObject).reduce((a, b) => modeObject[a] > modeObject[b] ? a : b);
}
@npgenx
npgenx / JS Snippets
Last active April 30, 2022 00:41
js: JS Snippets
// Copy to Clipboard
// Easily copy any text to clipboard using navigator.clipboard.writeText.
const copyToClipboard = (text) => navigator.clipboard.writeText(text);
copyToClipboard("Hello World");
// Check if Date is Valid
// Use the following snippet to check if a given date is valid or not.
const isDateValid = (...val) => !Number.isNaN(new Date(...val).valueOf());
isDateValid("December 17, 1995 03:24:00");
// Result: true
@npgenx
npgenx / sort_objects.js
Last active April 30, 2022 00:40
js: sort objects using an index array
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
const nums = [2, 7, 11, 15, 20], target = 9;
for (const [idx, testnum] of nums.entries()) {
console.log(`id:(${idx}): ${testnum}`);
@npgenx
npgenx / getMethods.js
Created October 10, 2019 03:16
js: Get methods of a javascript object
getMethods = (obj) => Object.getOwnPropertyNames(obj).filter(item => typeof obj[item] === 'function')
@npgenx
npgenx / SumTest.js
Created March 17, 2018 02:34
js: Sum Test
const isThereAMaxMatch = (list) => {
let thereIsASum = false;
let sortedList = list.sort((a, b) => { return b - a }); // hi to low
const maxNumber = sortedList[0];
let numCheck = (list) => {
let accumulatedSum = list[0];
for (i = 1; i < list.length; i++) {
@npgenx
npgenx / trace.js
Created February 5, 2016 05:55
js: Trace ( Console.log) Debugger
var trace = {
output : false,
queue : [],
push: function(data){
trace.queue.push(data);
if(trace.output){
trace.post();
}
return
@npgenx
npgenx / .htaccess
Last active April 30, 2022 00:44
apache: Allow Facebook and twitter crawlers behind a password protected site
AuthUserFile #Location of htpasswd file#
AuthType Basic
AuthName #<Site Name>#
Require valid-user
Order deny,allow
Deny from all
# Update the fb and twitter crawler IP lists by running the following commands, then copy & replace the IPs below.
#
# Facebook
//Making jQuery Google API
function modify_jquery() {
if (!is_admin()) {
wp_deregister_script('jquery');
wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js', false, '1.6.4');
wp_enqueue_script('jquery');
}
}
add_action('init', 'modify_jquery');