Skip to content

Instantly share code, notes, and snippets.

View JaymesKat's full-sized avatar

James Katarikawe JaymesKat

View GitHub Profile
@JaymesKat
JaymesKat / steam-roller-algorithm.js
Created December 27, 2019 03:24
Flatten a nested array, accounting for varying levels of nesting. Ref: freecodecamp
function steamrollArray(arr) {
let flatArr = [];
for(let item of arr){
if(Array.isArray(item)){
flatArr.push(...item.map(i => getValFromNestedArr(i)))
}
else {
flatArr.push(item)
}
}
@JaymesKat
JaymesKat / cash-register.js
Created December 27, 2019 02:33
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 k…
const CLOSED = "CLOSED";
const INSUFFICIENT_FUNDS = "INSUFFICIENT_FUNDS";
const OPEN = "OPEN";
const PENNY = 0;
const NICKEL = 1;
const DIME = 2;
const QUARTER = 3;
const ONE = 4;
const FIVE = 5;
@JaymesKat
JaymesKat / telephone-number-validator.js
Last active December 27, 2019 02:30
A function that returns true if the passed string looks like a valid US phone number.
function telephoneCheck(str) {
if(!parenthesesBalanced(str)){
return false;
}
const regex = /^1?\s*(\(?\d{3}\)?[-]?\s*){2}\d{4}$/
return regex.test(str);
}
function parenthesesBalanced(str){
const openBracketRegex = /\(/g
@JaymesKat
JaymesKat / caesar-cipher.js
Last active December 27, 2019 02:26
One of the simplest and most widely known ciphers is a Caesar cipher, also known as a shift cipher. In a shift cipher the meanings of the letters are shifted by some set amount. A common modern use is the ROT13 cipher, where the values of the letters are shifted by 13 places. Thus 'A' ↔ 'N', 'B' ↔ 'O' and so on. Write a function which takes a RO…
function createAlphabetArr(){
const first = 'A'.charCodeAt();
const second = 'Z'.charCodeAt();
let alphabet = [];
for(let code = first; code <= second; code++) {
alphabet.push(String.fromCharCode(code))
}
return alphabet;
}
@JaymesKat
JaymesKat / roman-numeral-converter.js
Created December 27, 2019 02:23
Convert the given number into a roman numeral.
const symbols = {
'1': 'I',
'5' : 'V',
'10' : 'X',
'50' : 'L',
'100' : 'C',
'500' : 'D',
'1000' : 'M'
};
@JaymesKat
JaymesKat / palindrome-checker.js
Created December 27, 2019 02:21
A palindrome is a word or sentence that's spelled the same way both forward and backward, ignoring punctuation, case, and spacing.
function palindrome(str) {
let text = str.toLowerCase().replace(/[^a-z0-9]/g, "");
const revText = text.split("").reverse().join("");
return text === revText;
}
@JaymesKat
JaymesKat / index.html
Created October 12, 2018 07:46
Scrolling Bootstrap Card Carousel
<div class="container-fluid">
<h1 class="text-center mb-3">Bootstrap Multi-Card Carousel</h1>
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<div class="carousel-inner row w-100 mx-auto">
<div class="carousel-item col-md-4 active">
<div class="card">
<img class="card-img-top img-fluid" src="http://placehold.it/800x600/f44242/fff" alt="Card image cap">
<div class="card-body">
<h4 class="card-title">Card 1</h4>
<p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
@JaymesKat
JaymesKat / silly-story-generator-assessment-solution.js
Created December 12, 2017 20:50
Solution to First Assessment Exercise in Mozilla JavaScript course
var customName = document.getElementById('customname');
var randomize = document.querySelector('.randomize');
var story = document.querySelector('.story');
function randomValueFromArray(array){
return array[Math.floor(Math.random()*array.length)];
}
var storyText = "It was 94 farenheit outside, so :insertx: went for a walk. When they got to :inserty:, they stared in horror for a few moments, then :insertz:. Bob saw the whole thing, but he was not surprised — :insertx: weighs 300 pounds, and it was a hot day.";