Skip to content

Instantly share code, notes, and snippets.

View nickihastings's full-sized avatar
🏠
Working from home

Nicki Hastings nickihastings

🏠
Working from home
View GitHub Profile
@nickihastings
nickihastings / Positive-and-Negative-Lookahead.js
Created October 10, 2019 11:48
Use lookaheads in the pwRegex to match passwords that are greater than 5 characters long and have two consecutive digits.
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 / roman-numeral-converter.js
Last active August 22, 2023 14:29
Convert the given number into a roman numeral. All roman numerals answers should be provided in upper-case.
function convertToRoman(num) {
var singles = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"];
var tens = ["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"];
var hundreds = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"];
var thousands = ["", "M", "MM", "MMM", "MMMM"];
//turn the number to a string, split individual characters and then turn each one to a number.
var length = num.toString().length;
var numbers = num.toString().split("").map(Number);
var roman = '';
@nickihastings
nickihastings / search-and-replace.js
Created March 16, 2018 20:11
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 …
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 / pig-latin.js
Created March 20, 2018 08:18
Translate the provided string to pig latin. Pig Latin takes the first consonant (or consonant cluster) of an English word, moves it to the end of the word and suffixes an "ay". If a word begins with a vowel you just add "way" to the end. Input strings are guaranteed to be English words in all lowercase.
function translatePigLatin(str) {
var vowels = ['a','e','i','o','u'];
var strArr = str.split(''); //split the string into an array
//walk through the string array and test for vowels.
for(var i = 0; i<strArr.length; i++){
//if the letter is a vowel create the new string
if(vowels.includes(strArr[i])){
//if the first letter is a vowel just add 'way' to the end.
if(i == 0){
@nickihastings
nickihastings / missing-letters.js
Created March 21, 2018 20:46
Find the missing letter in the passed letter range and return it. If all letters are present in the range, return undefined.
function fearNotLetter(str) {
//in unicode A=65 and Z=90, a=97 and z=122
//get the starting letter's unicode, and then add one to check the next letter
var start = str.charCodeAt(0) + 1;
var missing = ''; //store missing letters
//loop over the string, get the letters unicode value and compare it to what the next letter's code should be:
for(var i = 1; i < str.length; i++){
//if the codes don't match append the letter to the variable
if(str.charCodeAt(i) !== start){
@nickihastings
nickihastings / symmetric-difference.js
Last active November 10, 2022 20:17
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 = {…
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 / exact-change.js
Last active November 27, 2021 15:20
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…
/* 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 March 22, 2018 20:39
Check if a value is classified as a boolean primitive. Return true or false. Boolean primitives are true and false.
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 / wp-hide-admin-menu.php
Last active August 8, 2021 17:35
How to hide an admin menu in WordPress based on a user role
<?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 April 12, 2021 20:54
Debug admin menu - print to screen
<?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>';
}
?>