Skip to content

Instantly share code, notes, and snippets.

@cmcnealy
cmcnealy / quotes.json
Created July 29, 2020 17:27 — forked from camperbot/quotes.json
Quotes List in JSON Format
{
"quotes": [
{
"quote":"Life isn’t about getting and having, it’s about giving and being.","author":"Kevin Kruse"},
{
"quote":"Whatever the mind of man can conceive and believe, it can achieve.","author":"Napoleon Hill"},
{
"quote":"Strive not to be a success, but rather to be of value.","author":"Albert Einstein"},
{
@cmcnealy
cmcnealy / Project-Telephone-Number-Validator.js
Created June 28, 2020 01:26
freeCodeCamp | JavaScript Algorithms and Data Structures Projects: Telephone Number Validator: Return true if the passed string looks like a valid US phone number. The user may fill out the form field any way they choose as long as it has the format of a valid US number. The following are examples of valid formats for US numbers (refer to the te…
function telephoneCheck(str) {
// regex that matches a phone number with no spaces,
// with spaces, with dashes, and with or without country code
let regex = /^[1]*[\s|-]*\d{3}[\s|-]*\d{3}[\s|-]*\d{4}$/;
// regex that matches parentheses
// with no spaces, with spaces, with dashes,
// and with or without country code
let regexParentheses = /^[1]*[\s]*[(]\d{3}[)][\s]*\d{3}[\s|-]*\d{4}$/;
@cmcnealy
cmcnealy / Project-Caesar-Cipher.js
Last active June 27, 2020 23:41
freeCodeCamp | JavaScript Algorithms and Data Structures Projects: Caesars Cipher: 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 b…
function rot13(str) {
let codes = [];
for (let i = 0; i < str.length; i++) {
codes.push(str.charCodeAt(i));
}
console.log(codes);
let newCodes = codes.map(code => {
@cmcnealy
cmcnealy / Project-Roman-Numeral-Converter.js
Created June 27, 2020 20:13
freeCodeCamp | JavaScript Algorithms and Data Structures Projects: Roman Numeral Converter Convert the given number into a roman numeral. All roman numerals answers should be provided in upper-case.
function convertToRoman(num) {
// first split the number into decimal places
let str = num.toString(10);
let array = str.split("");
// unshift string elements to an array
// index[3] is always thousands
// index[2] is always hundreds
// index[1] is always tens
// index[0] is always ones
@cmcnealy
cmcnealy / Project-Palindrome-Checker.js
Created June 27, 2020 17:38
freeCodeCamp | JavaScript Algorithms and Data Structures Projects: Palindrome Checker
function palindrome(str) {
// turn everything into the same case
let lowerStr = str.toLowerCase();
// split the array into characters
let arr = lowerStr.split("");
// remove all non-alphanumeric characters
let letters = arr.filter(character => {
let regex = /[^\W|_]/;
return regex.test(character);
@cmcnealy
cmcnealy / Map-The-Debris.js
Created June 27, 2020 16:51
freeCodeCamp | Intermediate Algorithm Scripting: Map the Debris: Return a new array that transforms the elements' average altitude into their orbital periods (in seconds). The array will contain objects in the format {name: 'name', avgAlt: avgAlt}. You can read about orbital periods on Wikipedia. The values should be rounded to the nearest whole…
function orbitalPeriod(arr) {
let newArr = [];
const GM = 398600.4418;
const earthRadius = 6367.4447;
let getOrbitalPeriod = function(avgAlt) {
let a = earthRadius + avgAlt;
let T = 2 * Math.PI * Math.sqrt(Math.pow(a,3)/(GM));
return Math.round(T);
}
@cmcnealy
cmcnealy / Make-A-Person.js
Created June 27, 2020 16:10
freeCodeCamp | Intermediate Algorithm Scripting: Make a Person Fill in the object constructor with the following methods below: getFirstName() getLastName() getFullName() setFirstName(first) setLastName(last) setFullName(firstAndLast) Run the tests to see the expected output for each method. The methods that take an argument must accept only one…
var Person = function(firstAndLast) {
let fullName = firstAndLast;
this.getFullName = function() {
return fullName;
};
this.getFirstName = function() {
return fullName.split(" ")[0];
return
}
@cmcnealy
cmcnealy / Binary-Converter.js
Created June 27, 2020 00:25
freeCodeCamp | Intermediate Algorithm Scripting: Binary Agents: Return an English translated sentence of the passed binary string. The binary string will be space separated.
function binaryAgent(str) {
// first split the binary string on spaces
let array = str.split(" ");
// initialize empty array to push to
let charcodes = [];
// use forEach to iterate over the array of binary codes
array.forEach(binstring => {
// parseInt the binary to get a charCode
let parsed = parseInt(binstring, 2);
@cmcnealy
cmcnealy / Steamroller.js
Created June 26, 2020 22:55
freeCodeCamp | Intermediate Algorithm Scripting: Steamroller: Flatten a nested array. You must account for varying levels of nesting.
function steamrollArray(arr) {
let stack = arr.slice();
let newArr = [];
while (stack.length > 0) {
// take the last member of the parent array
let next = stack.pop();
if (Array.isArray(next)) {
// if it's an array,
// spread it and push it back onto the stack
@cmcnealy
cmcnealy / Drop-It.js
Created June 26, 2020 19:51
freeCodeCamp | Intermediate Algorithm Scripting: Drop it: Given the array arr, iterate through and remove each element starting from the first element (the 0 index) until the function func returns true when the iterated element is passed through it. Then return the rest of the array once the condition is satisfied, otherwise, arr should be retur…
function dropElements(arr, func) {
// copy array to new instance with slice
let newArr = arr.slice();
// iterate over array and try the function
// if function is true, break and return what's left
// else remove the matching newArr index
// which will be 0 if some have already been removed
for (let i = 0; i < arr.length; i++) {
if (func(arr[i])) {