Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Nicki Hastings nickihastings

🏠
Working from home
View GitHub Profile
@nickihastings
nickihastings / Positive-and-Negative-Lookahead.js
Created Oct 10, 2019
Use lookaheads in the pwRegex to match passwords that are greater than 5 characters long and have two consecutive digits.
View Positive-and-Negative-Lookahead.js
let sampleWord = "astronaut";
let pwRegex = /(?=\w{5,})(?=\w+\d{2})/; // Change this line
let result = pwRegex.test(sampleWord);
// Your regex should use two positive lookaheads.
// Your regex should not match "astronaut"
// Your regex should not match "airplanes"
// Your regex should not match "banan1"
// Your regex should match "bana12"
// Your regex should match "abc123"
@nickihastings
nickihastings / search-and-replace.js
Created Mar 16, 2018
Perform a search and replace on the sentence using the arguments provided and return the new sentence. First argument is the sentence to perform the search and replace on. Second argument is the word that you will be replacing (before). Third argument is what you will be replacing the second argument with (after). NOTE: Preserve the case of the …
View search-and-replace.js
function myReplace(str, before, after) {
//search for the before string and then use a function to check for capitalisation before replacing.
str = str.replace(before, function(x){
//check whether the first letter of before is a capital
//replace after with a capitalised first letter and then
//the rest of the string, cannot just replace first letter as its read only.
if(before[0] == before[0].toUpperCase()){
after = after[0].toUpperCase() + after.slice(1);
return after;
}
@nickihastings
nickihastings / exact-change.js
Last active Nov 27, 2021
Design a cash register drawer function checkCashRegister() that accepts purchase price as the first argument (price), payment as the second argument (cash), and cash-in-drawer (cid) as the third argument. cid is a 2D array listing available currency. Return the string "Insufficient Funds" if cash-in-drawer is less than the change due. Return the…
View exact-change.js
/* UPDATED NOVEMBER 2021 to work with the latest FreeCodeCamp tests
Design a cash register drawer function checkCashRegister() that accepts purchase price as the first argument (price),
payment as the second argument (cash), and cash-in-drawer (cid) as the third argument.
cid is a 2D array listing available currency.
The checkCashRegister() function should always return an object with a status key and a change key.
Return {status: "INSUFFICIENT_FUNDS", change: []} if cash-in-drawer is less than the change due, or if you cannot return
the exact change.
Return {status: "CLOSED", change: [...]} with cash-in-drawer as the value for the key change if it is equal to the change due.
Otherwise, return {status: "OPEN", change: [...]}, with the change due in coins and bills, sorted in highest to lowest order,
as the value of the change key.
@nickihastings
nickihastings / boo-who.js
Created Mar 22, 2018
Check if a value is classified as a boolean primitive. Return true or false. Boolean primitives are true and false.
View boo-who.js
function booWho(bool) {
// What is the new fad diet for ghost developers? The Boolean.
//check if the value 'bool' is strictly true or false, this means the value is a Boolean Primitive and should return true.
//Everything else should return false.
if(bool === true || bool === false){
return true;
}
else {
return false;
}
@nickihastings
nickihastings / symmetric-difference.js
Last active Nov 1, 2021
Create a function that takes two or more arrays and returns an array of the symmetric difference (△ or ⊕) of the provided arrays. Given two sets (for example set A = {1, 2, 3} and set B = {2, 3, 4}), the mathematical term "symmetric difference" of two sets is the set of elements which are in either of the two sets, but not in both (A △ B = C = {…
View symmetric-difference.js
function sym(args) {
//Put the arguments into an array
var arrArgs = Array.prototype.slice.call(arguments);
//Function to compare two arrays by reducing each one in
//comparison with the other. Check if the item in first array
//is also in the second array, if it isn't push it to the accumulator.
//then repeat the other way around.
function compareArrays(arr1, arr2){
@nickihastings
nickihastings / wp-hide-admin-menu.php
Last active Aug 8, 2021
How to hide an admin menu in WordPress based on a user role
View wp-hide-admin-menu.php
<?php
/* check if user is administrator - only show downloads menu if is admin */
add_action( 'admin_init', 'nh_remove_menu_pages' );
function nh_remove_menu_pages() {
global $user_ID;
//if the user is NOT an administrator remove the menu for downloads
if ( !current_user_can( 'administrator' ) ) { //change role or capability here
remove_menu_page( 'edit.php?post_type=wpdmpro' ); //change menu item here
}
@nickihastings
nickihastings / print_admin_menu.php
Last active Apr 12, 2021
Debug admin menu - print to screen
View print_admin_menu.php
<?php
//this will print the admin menu in array form so you can determine slug names from value [2] for use with remove_menu_page()
add_action( 'admin_init', 'print_admin_menu' );
function print_admin_menu() {
echo '<pre>' . print_r( $GLOBALS[ 'menu' ], TRUE) . '</pre>';
}
?>
@nickihastings
nickihastings / wpml-formidable-duplicate-post-langs.php
Created Feb 18, 2020
Duplicate a post created with Formidable Forms frontend post submission, so that it is available in all languages. Needs WPML.
View wpml-formidable-duplicate-post-langs.php
<?php
add_action('frm_after_create_entry', 'add_translation', 30, 2);
function add_translation($entry_id, $form_id) {
$forms = array( 64 ); // <---- change or add form ids here, comma separated, e.g. (24, 53, 64)
// check if form id is in array, if so run code
if( in_array($form_id, $forms) ) {
@nickihastings
nickihastings / pairwise.js
Created Apr 17, 2018
Given an array arr, find element pairs whose sum equal the second argument arg and return the sum of their indices. If multiple pairs are possible that have the same numeric elements but different indices, return the smallest sum of indices. Once an element has been used, it cannot be reused to pair with another. For example pairwise([7, 9, 11, …
View pairwise.js
function pairwise(arr, arg) {
//if its an empty array return 0
if(arr.length === 0){
return 0;
}
//reduce the array by looping over the array and checking if the
//sum of the current element and another element = the argument.
//if they match and the indexes of both are different, add the indexes to
//the output array.
var pairs = arr.reduce(function(accum, curr, index, array){
@nickihastings
nickihastings / aux-functions.php
Last active Feb 18, 2020 — forked from fredrikwoll/aux-functions.php
Programmatically duplicating a WordPress post
View aux-functions.php
<?php
/**
* Duplicates a post & its meta and it returns the new duplicated Post ID
* @param [int] $post_id The Post you want to clone
* @return [int] The duplicated Post ID
*/
function duplicate($post_id) {
$title = get_the_title($post_id);
$oldpost = get_post($post_id);