Skip to content

Instantly share code, notes, and snippets.

View iCaspar's full-sized avatar

Caspar Green iCaspar

View GitHub Profile
@iCaspar
iCaspar / cashRegister.js
Created December 12, 2018 01:55
A cash register function
function checkCashRegister(price, cash, cid) {
const denominationVals = {
'ONE HUNDRED': 100,
'TWENTY': 20,
'TEN': 10,
'FIVE': 5,
'ONE': 1,
'QUARTER': 0.25,
'DIME': 0.1,
'NICKEL': 0.050,
@iCaspar
iCaspar / usTelephoneValidator.js
Created December 11, 2018 18:21
Function to validate a US phone number
function telephoneCheck(str) {
const usPhoneRegex = /^(?:1\s?)?(?:\d{3}|\(\d{3}\))[\s|-]?\d{3}[\s|-]?\d{4}$/
return usPhoneRegex.test(str);
}
@iCaspar
iCaspar / rot13.js
Created December 11, 2018 16:20
A function to decode a ROT13 string
function rot13 (str) { // LBH QVQ VG!
const allCapsRegex = /[A-Z]/
let decoded = ''
let char = 0
while (char < str.length) {
if (true === allCapsRegex.test(str[char])) {
decoded += String.fromCharCode(
str[char].charCodeAt(0) <= 77 ? str[char].charCodeAt(0) + 13 : str[char].charCodeAt(0) - 13
@iCaspar
iCaspar / toRomanNumeral.js
Created December 11, 2018 14:51
A function to convert an aribic (base 10) number to a Roman numeral
function convertToRoman(num) {
const romanSymbols = {
M: 1000,
CM: 900,
D: 500,
CD: 400,
C: 100,
XC: 90,
L: 50,
XL: 40,
@iCaspar
iCaspar / palindrome-checker.js
Created December 10, 2018 20:24
A function to check whether a string, stripped of non alphanumeric characters and spaces, is a palendrome
function palindrome(str) {
const lettersOnlyRegex = /[a-zA-Z0-9]/g
const letters = str.match(lettersOnlyRegex)
if (null === letters || 0 === letters.length) {
return false
}
const strippedString = str.match(lettersOnlyRegex).join('').toLowerCase()
@iCaspar
iCaspar / str-reverse.js
Created December 5, 2018 15:38
JS algorithm to reverse a string (without using conversion to array and back)
function strReverse(str) {
let reverse = ''
let i = str.length
while (i-- > 0) {
reverse += str[i]
}
return reverse
}
@iCaspar
iCaspar / default.conf
Created November 28, 2018 11:03
A default nginx config for php apps.
server {
listen 80 default_server;
root /var/www/html;
index index.html index.htm index.php;
server_name localhost;
charset utf-8;
location = /favicon.ico {
log_not_found off;
access_log off;
@iCaspar
iCaspar / no-fives.php
Created November 15, 2018 02:36
Has 5s recursive method to find numbers with 5s in them.
/**
* Does this number have 5s?
*
* @param $number Number to check.
*
* @since 1.0.0
*
* @return bool
*/
private function hasFive($number): bool
@iCaspar
iCaspar / arrayproduct.php
Created November 6, 2018 20:04
Array Product puzzle
<?php
/**
* Unit tests for Product of Array Items puzzle.
*
* @see https://www.codewars.com/kata/product-of-array-items/
*
* Requirements: Calculate the product of all elements in an array.
* If the array is NULL or empty, return NULL
*
* @author Caspar Green <[email protected]>
@iCaspar
iCaspar / guessBlue.php
Last active November 5, 2018 15:30
Guess the odds of drawing blue
<?php
/**
* Unit tests for Blue and Red Marbles game.
*
* @see https://www.codewars.com/kata/thinkful-number-drills-blue-and-red-marbles/
*
* You and a friend have decided to play a game to drill your statistical intuitions.
* The game works like this:
* You have a bunch of red and blue marbles.
* To start the game you grab a handful of marbles of each color and put them