Skip to content

Instantly share code, notes, and snippets.

@leonel-ai
leonel-ai / testPrototypes.js
Created January 9, 2019 20:26
OOP exercise using 'this' and 'prototypes'
// props I don't want shared for each obj
// constructor function
function Vehicle(make, model, year){
this.make = make;
this.model = model;
this.year = year;
this.isRunning = false;
}
// functions I want each obj to have
@leonel-ai
leonel-ai / practiceRoutes.js
Created January 1, 2019 23:11
Practicing Express routing with params!
var express = require("express");
var app = express();
app.get("/", function(req, res){
res.send("Hi there, welcome to my practice run!");
});
app.get("/speak/:animal", function(req, res){
var sounds = {
pig: "Oink",
@leonel-ai
leonel-ai / wp-rawjs.js
Created September 10, 2018 18:06
Raw JS file from WordPress site. Seeking to modify highlightNav().
<script type="text/javascript"/> // added slash outside of WP to recognize all below
// Left Navigation - Selectors
var header = document.getElementById('hb-header');
var headerInner = document.getElementById('header-inner');
var pageHeader = document.getElementById('hb-page-title');
var dataSpecContainer = document.querySelector('.data-spec');
var leftNavCol = document.getElementById('nav-column');
var navTrees = document.querySelectorAll('.nav-tree');
var navSubTrees = jQuery('.nav-tree__sub-nav a');
var navContainer = jQuery('.nav-tree__container');
@leonel-ai
leonel-ai / s3PhotoUpload.js
Created July 20, 2017 18:41
Code written to upload a photo from local file to S3 (re: node, express, sequelize)
var TARGET_PATH = path.resolve(`./documents/tempdocs/yourSetFolder`);
var IMAGE_TYPES = ['image/jpeg', 'image/png'];
var yourPhotoUpload = {
index: (req, res, next) => {
res.render('index');
},
upload: (req, res, next) => {
// UPLOAD LOCAL TEMP
var is, os, targetPath, targetName;
@leonel-ai
leonel-ai / palindromeEfficiency.js
Created June 18, 2017 18:26
Palindrome function with minimal cyclomatic complexity
//this solution performs at minimum 7x better, at maximum infinitely better.
function palindrome(str) {
//assign a front and a back pointer
let front = 0
let back = str.length - 1
//back and front pointers won't always meet in the middle, so use (back > front)
while (back > front) {
//increments front pointer if current character doesn't meet criteria
if ( str[front].match(/[\W_]/) ) {
@leonel-ai
leonel-ai / fcc-largestNumbersInArray
Created May 11, 2017 19:55
freeCodeCamp Basic Algorithm Scripting: Challenge 06
function largestOfFour(arr) {
// array to hold results of 4 sub-arrays
var largestNum = [0,0,0,0];
//nested for loops to iterate through array and sub-array
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr[i].length; j++) {
if (arr[i][j] > largestNum[i]) {
largestNum[i] = arr[i][j];
}
@leonel-ai
leonel-ai / fcc-longestWord
Last active April 27, 2017 22:01
freeCodeCamp Basic Algorithm Scripting: Challenge 04
// written out by hand - first in pseudocode, then actual code, and then tested in browser
function findLongestWord(str) {
var arrElem = str.split(" "); // str to array with space delimiter in bw
var maxElem = arrElem[0]; // default longest element to first index of array
for (var i = 0; i < arrElem.length; i++) {
if (arrElem[i].length >= maxElem.length) { // compares length of each to previous maxElem
maxElem = arrElem[i]; // sets new maxElem if greater than
}
@leonel-ai
leonel-ai / fcc-palindromes
Created April 27, 2017 21:31
freeCodeCamp Basic Algorithm Scripting: Challenge 03
// learned to clean up code and execute multiple functions in a single line
// learned to use regular expressions and the replace function
function palindrome(str) {
var re = /[^A-Za-z0-9]/g; // regular expression removes all non-alphanumeric char
var string = str.toLowerCase().replace(re,''); // lower case, replace char w/no char in bw
var revArray = string.split('').reverse().join(''); // str to array, reverse, array to str
return string === revArray; // returns true if equal, otherwise false
}
@leonel-ai
leonel-ai / fcc-factorialize-a-number
Created April 27, 2017 20:40
freeCodeCamp Basic Algorithm Scripting: Challenge 02 - Recursion
// example of recursive algorithm
function factorialize(num) {
if (num < 0) {
return -1; // error msg
}
else if (num === 0) {
return 1; // base case
}
else {
@leonel-ai
leonel-ai / FCC-reverse-a-string
Created April 27, 2017 20:10
freeCodeCamp Basic Algorithm Scripting: Challenge 01
function reverseString(str) {
var string = str;
var array = [];
array = string.split(''); // turns string into an array of char
var revArray = array.reverse(); // reverses elements of array
var newStr = revArray.join(''); // joins reversed elements of array into a string w/ no char in between
return newStr; // returns reversed string