Skip to content

Instantly share code, notes, and snippets.

@mtHuberty
Last active January 11, 2018 16:27
Show Gist options
  • Save mtHuberty/bf21770316978c513974a678a3c7444a to your computer and use it in GitHub Desktop.
Save mtHuberty/bf21770316978c513974a678a3c7444a to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/nosokiv
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
//---------------------------------
// Name: Matt Huberty
//---------------------------------
//---------------------------------
// Exercise: Property Path Evaluation
//---------------------------------
function propertyValueAt(obj, arr){
let path = arr.join(".");
".".concat(path);
let ans = "obj".concat(".").concat(path)
//I have read warnings about using eval(), would prefer to refactor this to exlude it
return eval(ans);
}
let myObj = {
a: 1,
b: {
c: 2,
d: 3
}
}
console.log(propertyValueAt(myObj, ["b","c"]))
console.log(propertyValueAt(myObj, ["b"]))
console.log(propertyValueAt(myObj, ["z"]))
//---------------------------------
// Name: Matt Huberty
//---------------------------------
//---------------------------------
// Exercise: Sum Nested Arrays
//---------------------------------
function sumNested(arr){
//Input validation
if(!Array.isArray(arr) || arr.length === 0){
return 0;
}
//Flatten any input array
function flattenArr(nested){
const notNested = nested.reduce((prev,next)=>{
//console.log("reducing")
//console.log(Array.isArray(prev));
if(Array.isArray(prev)){
return prev.concat((Array.isArray(next) ? flattenArr(next) : next));
} else{
const startArr = [];
startArr.push(prev);
return startArr.concat((Array.isArray(next) ? flattenArr(next) : next));
}
})
return notNested;
}
const flatArr = flattenArr(arr);
const sum = flatArr.reduce((prev, next)=>{
return prev + next;
})
return sum;
}
console.log(sumNested([1,1,1,[3,4,[8]],[5]]));
console.log(sumNested([]));
console.log(sumNested("im not an array"));
//---------------------------------
// Name: Matt Huberty
//---------------------------------
//---------------------------------
// Exercise: Word Count
//---------------------------------
function wordCount(sentence){
let filtered = sentence.match(/[ a-z0-9.,\/#!$%\^&\*;:{}=\-_`~()]/gi).join("");
console.log(filtered);
return filtered.split(" ").length;
}
console.log(wordCount("Hey the*%*%* I am @##$ 9239393 ...,"))
console.log(wordCount("I want TO BREAK, This. Thing."))
console.log(wordCount("$$$ $$$ %%% ^^^"))
// ---------------------------------
// Name: Matt Huberty
// ---------------------------------
// ---------------------------------
// Exercise: Anagram Tester
// ---------------------------------
function areTheseAnagrams(str1, str2){
//Input validation
if(typeof(str1) !== "string" || typeof(str2) !== "string" || arguments.length !== 2){
return "Please input two strings to test";
}
//Ignore cases
const arr1 = str1.toLowerCase().split("");
const arr2 = str2.toLowerCase().split("");
//Test all letters
return arr1.every(function(el){
return arr2.indexOf(el) >= 0 ? true : false;
})
}
console.log(areTheseAnagrams("abc", "bca"));
console.log(areTheseAnagrams("def", "hgh"));
console.log(areTheseAnagrams("def", "DEF"));
console.log(areTheseAnagrams("def"));
console.log(areTheseAnagrams("def", 2));
//---------------------------------
// Name: Matt Huberty
//---------------------------------
//---------------------------------
// Exercise: Analyze Prices
//---------------------------------
function analyzePrices(arr){
//We need to buy low and sell high
let largestProfit = 0;
let buyInd = null;
let sellInd = null;
//Logic for finding the greatest increase from left to right
arr.forEach((el, ind)=>{
for(let i=ind+1; i<arr.length; i++){
if(arr[i] - el > largestProfit){
largestProfit = arr[i] - el;
buyInd = ind;
sellInd = i;
}
}
})
//Create an object with restults from the calculations above
let analysis = {
buyIndex: buyInd,
sellIndex: sellInd
}
return analysis;
//Because this function loops through the array, and adds an additional loop for each element,
//this would not be efficient with sufficiently large data sets. It has a runtime complexity
//of O(n^2)
}
console.log(analyzePrices([1,2,3,4,5,6,7,8,9]));
console.log(analyzePrices([9,8,7,6,5,4]));
console.log(analyzePrices([1,8,2,20,1,70,0,100]));
//---------------------------------
// Name: Matt Huberty
//---------------------------------
//---------------------------------
// Exercise: Fizz Buzz
//---------------------------------
function fizzBuzz(n){
//Input Validation
if(n <= 0 || typeof(n) !== "number"){
return "";
}
let outputString = "";
for(let i=1; i<=n; i++){
outputString += i;
if(i % 3 === 0 && i % 5 === 0){
outputString += "fizzbuzz";
} else if(i % 3 === 0){
outputString += "fizz ";
} else if(i % 5 === 0){
outputString += "buzz";
}
if(i<n) outputString += ",";
}
return outputString;
}
console.log(fizzBuzz(0));
console.log(fizzBuzz(15));
console.log(fizzBuzz(30));
console.log(fizzBuzz(-1));
//---------------------------------
// Name: Matt Huberty
//---------------------------------
//---------------------------------
// Exercise: Object Oriented Programming - Car
//---------------------------------
function Car(speed){
this.speed = 0;
this.getSpeed = ()=>{
return this.speed;
}
this.setSpeed = (newSpeed)=>{
this.speed = newSpeed;
}
this.stop = ()=>{
this.speed = 0;
}
}
let car = new Car();
console.log(car.getSpeed());
car.setSpeed(10);
console.log(car.getSpeed());
car.stop();
console.log(car.getSpeed());
car.setSpeed(100);
console.log(car.getSpeed());
//Bonus
//---------------------------------
// Name: Matt Huberty
//---------------------------------
//---------------------------------
// Exercise: Calculate Bowling Score
//---------------------------------
//Running out of time!...giving this a shot anyway
function calculateBowlingScore(str){
//Function to convert X, /, and - to point values
function toPointValue(symb){
if(symb === "X"){
return 10;
} else if(symb === "/"){
return 10;
} else if(symb === "-"){
return 0;
} else{
return symb;
}
return "whoops";
}
const scoreArr = str.split("");
//Make new array that converts each item in old array to a score
const pointValues = scoreArr.map((el, ind)=>{
if(el === "X"){
return 10 + toPointValue(scoreArr[ind+1] + toPointValue(scoreArr[ind+2]));
} else if(el === "/"){
return 10 + toPointValue(scoreArr[ind+1]);
} else if(el === "-"){
return 0;
} else{
//log something to console
}
})
return pointValues;
}
console.log(calculateBowlingScore("XX2345/234-2"))
</script>
<script id="jsbin-source-javascript" type="text/javascript">//---------------------------------
// Name: Matt Huberty
//---------------------------------
//---------------------------------
// Exercise: Property Path Evaluation
//---------------------------------
function propertyValueAt(obj, arr){
let path = arr.join(".");
".".concat(path);
let ans = "obj".concat(".").concat(path)
//I have read warnings about using eval(), would prefer to refactor this to exlude it
return eval(ans);
}
let myObj = {
a: 1,
b: {
c: 2,
d: 3
}
}
console.log(propertyValueAt(myObj, ["b","c"]))
console.log(propertyValueAt(myObj, ["b"]))
console.log(propertyValueAt(myObj, ["z"]))
//---------------------------------
// Name: Matt Huberty
//---------------------------------
//---------------------------------
// Exercise: Sum Nested Arrays
//---------------------------------
function sumNested(arr){
//Input validation
if(!Array.isArray(arr) || arr.length === 0){
return 0;
}
//Flatten any input array
function flattenArr(nested){
const notNested = nested.reduce((prev,next)=>{
//console.log("reducing")
//console.log(Array.isArray(prev));
if(Array.isArray(prev)){
return prev.concat((Array.isArray(next) ? flattenArr(next) : next));
} else{
const startArr = [];
startArr.push(prev);
return startArr.concat((Array.isArray(next) ? flattenArr(next) : next));
}
})
return notNested;
}
const flatArr = flattenArr(arr);
const sum = flatArr.reduce((prev, next)=>{
return prev + next;
})
return sum;
}
console.log(sumNested([1,1,1,[3,4,[8]],[5]]));
console.log(sumNested([]));
console.log(sumNested("im not an array"));
//---------------------------------
// Name: Matt Huberty
//---------------------------------
//---------------------------------
// Exercise: Word Count
//---------------------------------
function wordCount(sentence){
let filtered = sentence.match(/[ a-z0-9.,\/#!$%\^&\*;:{}=\-_`~()]/gi).join("");
console.log(filtered);
return filtered.split(" ").length;
}
console.log(wordCount("Hey the*%*%* I am @##$ 9239393 ...,"))
console.log(wordCount("I want TO BREAK, This. Thing."))
console.log(wordCount("$$$ $$$ %%% ^^^"))
// ---------------------------------
// Name: Matt Huberty
// ---------------------------------
// ---------------------------------
// Exercise: Anagram Tester
// ---------------------------------
function areTheseAnagrams(str1, str2){
//Input validation
if(typeof(str1) !== "string" || typeof(str2) !== "string" || arguments.length !== 2){
return "Please input two strings to test";
}
//Ignore cases
const arr1 = str1.toLowerCase().split("");
const arr2 = str2.toLowerCase().split("");
//Test all letters
return arr1.every(function(el){
return arr2.indexOf(el) >= 0 ? true : false;
})
}
console.log(areTheseAnagrams("abc", "bca"));
console.log(areTheseAnagrams("def", "hgh"));
console.log(areTheseAnagrams("def", "DEF"));
console.log(areTheseAnagrams("def"));
console.log(areTheseAnagrams("def", 2));
//---------------------------------
// Name: Matt Huberty
//---------------------------------
//---------------------------------
// Exercise: Analyze Prices
//---------------------------------
function analyzePrices(arr){
//We need to buy low and sell high
let largestProfit = 0;
let buyInd = null;
let sellInd = null;
//Logic for finding the greatest increase from left to right
arr.forEach((el, ind)=>{
for(let i=ind+1; i<arr.length; i++){
if(arr[i] - el > largestProfit){
largestProfit = arr[i] - el;
buyInd = ind;
sellInd = i;
}
}
})
//Create an object with restults from the calculations above
let analysis = {
buyIndex: buyInd,
sellIndex: sellInd
}
return analysis;
//Because this function loops through the array, and adds an additional loop for each element,
//this would not be efficient with sufficiently large data sets. It has a runtime complexity
//of O(n^2)
}
console.log(analyzePrices([1,2,3,4,5,6,7,8,9]));
console.log(analyzePrices([9,8,7,6,5,4]));
console.log(analyzePrices([1,8,2,20,1,70,0,100]));
//---------------------------------
// Name: Matt Huberty
//---------------------------------
//---------------------------------
// Exercise: Fizz Buzz
//---------------------------------
function fizzBuzz(n){
//Input Validation
if(n <= 0 || typeof(n) !== "number"){
return "";
}
let outputString = "";
for(let i=1; i<=n; i++){
outputString += i;
if(i % 3 === 0 && i % 5 === 0){
outputString += "fizzbuzz";
} else if(i % 3 === 0){
outputString += "fizz ";
} else if(i % 5 === 0){
outputString += "buzz";
}
if(i<n) outputString += ",";
}
return outputString;
}
console.log(fizzBuzz(0));
console.log(fizzBuzz(15));
console.log(fizzBuzz(30));
console.log(fizzBuzz(-1));
//---------------------------------
// Name: Matt Huberty
//---------------------------------
//---------------------------------
// Exercise: Object Oriented Programming - Car
//---------------------------------
function Car(speed){
this.speed = 0;
this.getSpeed = ()=>{
return this.speed;
}
this.setSpeed = (newSpeed)=>{
this.speed = newSpeed;
}
this.stop = ()=>{
this.speed = 0;
}
}
let car = new Car();
console.log(car.getSpeed());
car.setSpeed(10);
console.log(car.getSpeed());
car.stop();
console.log(car.getSpeed());
car.setSpeed(100);
console.log(car.getSpeed());
//Bonus
//---------------------------------
// Name: Matt Huberty
//---------------------------------
//---------------------------------
// Exercise: Calculate Bowling Score
//---------------------------------
//Running out of time!...giving this a shot anyway
function calculateBowlingScore(str){
//Function to convert X, /, and - to point values
function toPointValue(symb){
if(symb === "X"){
return 10;
} else if(symb === "/"){
return 10;
} else if(symb === "-"){
return 0;
} else{
return symb;
}
return "whoops";
}
const scoreArr = str.split("");
//Make new array that converts each item in old array to a score
const pointValues = scoreArr.map((el, ind)=>{
if(el === "X"){
return 10 + toPointValue(scoreArr[ind+1] + toPointValue(scoreArr[ind+2]));
} else if(el === "/"){
return 10 + toPointValue(scoreArr[ind+1]);
} else if(el === "-"){
return 0;
} else{
//log something to console
}
})
return pointValues;
}
console.log(calculateBowlingScore("XX2345/234-2"))
</script></body>
</html>
//---------------------------------
// Name: Matt Huberty
//---------------------------------
//---------------------------------
// Exercise: Property Path Evaluation
//---------------------------------
function propertyValueAt(obj, arr){
let path = arr.join(".");
".".concat(path);
let ans = "obj".concat(".").concat(path)
//I have read warnings about using eval(), would prefer to refactor this to exlude it
return eval(ans);
}
let myObj = {
a: 1,
b: {
c: 2,
d: 3
}
}
console.log(propertyValueAt(myObj, ["b","c"]))
console.log(propertyValueAt(myObj, ["b"]))
console.log(propertyValueAt(myObj, ["z"]))
//---------------------------------
// Name: Matt Huberty
//---------------------------------
//---------------------------------
// Exercise: Sum Nested Arrays
//---------------------------------
function sumNested(arr){
//Input validation
if(!Array.isArray(arr) || arr.length === 0){
return 0;
}
//Flatten any input array
function flattenArr(nested){
const notNested = nested.reduce((prev,next)=>{
//console.log("reducing")
//console.log(Array.isArray(prev));
if(Array.isArray(prev)){
return prev.concat((Array.isArray(next) ? flattenArr(next) : next));
} else{
const startArr = [];
startArr.push(prev);
return startArr.concat((Array.isArray(next) ? flattenArr(next) : next));
}
})
return notNested;
}
const flatArr = flattenArr(arr);
const sum = flatArr.reduce((prev, next)=>{
return prev + next;
})
return sum;
}
console.log(sumNested([1,1,1,[3,4,[8]],[5]]));
console.log(sumNested([]));
console.log(sumNested("im not an array"));
//---------------------------------
// Name: Matt Huberty
//---------------------------------
//---------------------------------
// Exercise: Word Count
//---------------------------------
function wordCount(sentence){
let filtered = sentence.match(/[ a-z0-9.,\/#!$%\^&\*;:{}=\-_`~()]/gi).join("");
console.log(filtered);
return filtered.split(" ").length;
}
console.log(wordCount("Hey the*%*%* I am @##$ 9239393 ...,"))
console.log(wordCount("I want TO BREAK, This. Thing."))
console.log(wordCount("$$$ $$$ %%% ^^^"))
// ---------------------------------
// Name: Matt Huberty
// ---------------------------------
// ---------------------------------
// Exercise: Anagram Tester
// ---------------------------------
function areTheseAnagrams(str1, str2){
//Input validation
if(typeof(str1) !== "string" || typeof(str2) !== "string" || arguments.length !== 2){
return "Please input two strings to test";
}
//Ignore cases
const arr1 = str1.toLowerCase().split("");
const arr2 = str2.toLowerCase().split("");
//Test all letters
return arr1.every(function(el){
return arr2.indexOf(el) >= 0 ? true : false;
})
}
console.log(areTheseAnagrams("abc", "bca"));
console.log(areTheseAnagrams("def", "hgh"));
console.log(areTheseAnagrams("def", "DEF"));
console.log(areTheseAnagrams("def"));
console.log(areTheseAnagrams("def", 2));
//---------------------------------
// Name: Matt Huberty
//---------------------------------
//---------------------------------
// Exercise: Analyze Prices
//---------------------------------
function analyzePrices(arr){
//We need to buy low and sell high
let largestProfit = 0;
let buyInd = null;
let sellInd = null;
//Logic for finding the greatest increase from left to right
arr.forEach((el, ind)=>{
for(let i=ind+1; i<arr.length; i++){
if(arr[i] - el > largestProfit){
largestProfit = arr[i] - el;
buyInd = ind;
sellInd = i;
}
}
})
//Create an object with restults from the calculations above
let analysis = {
buyIndex: buyInd,
sellIndex: sellInd
}
return analysis;
//Because this function loops through the array, and adds an additional loop for each element,
//this would not be efficient with sufficiently large data sets. It has a runtime complexity
//of O(n^2)
}
console.log(analyzePrices([1,2,3,4,5,6,7,8,9]));
console.log(analyzePrices([9,8,7,6,5,4]));
console.log(analyzePrices([1,8,2,20,1,70,0,100]));
//---------------------------------
// Name: Matt Huberty
//---------------------------------
//---------------------------------
// Exercise: Fizz Buzz
//---------------------------------
function fizzBuzz(n){
//Input Validation
if(n <= 0 || typeof(n) !== "number"){
return "";
}
let outputString = "";
for(let i=1; i<=n; i++){
outputString += i;
if(i % 3 === 0 && i % 5 === 0){
outputString += "fizzbuzz";
} else if(i % 3 === 0){
outputString += "fizz ";
} else if(i % 5 === 0){
outputString += "buzz";
}
if(i<n) outputString += ",";
}
return outputString;
}
console.log(fizzBuzz(0));
console.log(fizzBuzz(15));
console.log(fizzBuzz(30));
console.log(fizzBuzz(-1));
//---------------------------------
// Name: Matt Huberty
//---------------------------------
//---------------------------------
// Exercise: Object Oriented Programming - Car
//---------------------------------
function Car(speed){
this.speed = 0;
this.getSpeed = ()=>{
return this.speed;
}
this.setSpeed = (newSpeed)=>{
this.speed = newSpeed;
}
this.stop = ()=>{
this.speed = 0;
}
}
let car = new Car();
console.log(car.getSpeed());
car.setSpeed(10);
console.log(car.getSpeed());
car.stop();
console.log(car.getSpeed());
car.setSpeed(100);
console.log(car.getSpeed());
//Bonus
//---------------------------------
// Name: Matt Huberty
//---------------------------------
//---------------------------------
// Exercise: Calculate Bowling Score
//---------------------------------
//Running out of time!...giving this a shot anyway
function calculateBowlingScore(str){
//Function to convert X, /, and - to point values
function toPointValue(symb){
if(symb === "X"){
return 10;
} else if(symb === "/"){
return 10;
} else if(symb === "-"){
return 0;
} else{
return symb;
}
return "whoops";
}
const scoreArr = str.split("");
//Make new array that converts each item in old array to a score
const pointValues = scoreArr.map((el, ind)=>{
if(el === "X"){
return 10 + toPointValue(scoreArr[ind+1] + toPointValue(scoreArr[ind+2]));
} else if(el === "/"){
return 10 + toPointValue(scoreArr[ind+1]);
} else if(el === "-"){
return 0;
} else{
//log something to console
}
})
return pointValues;
}
console.log(calculateBowlingScore("XX2345/234-2"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment