Skip to content

Instantly share code, notes, and snippets.

@jbburf
Created June 14, 2018 15:29
Show Gist options
  • Save jbburf/c4884da36fc153f2a6ba5255fa2e0fd4 to your computer and use it in GitHub Desktop.
Save jbburf/c4884da36fc153f2a6ba5255fa2e0fd4 to your computer and use it in GitHub Desktop.
JS Bin Serlock and Anagrams // source https://jsbin.com/yagecih
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Serlock and Anagrams">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
// -------------------------------------
// Name: Ben Burford
// -------------------------------------
// -------------------------------------
// Exercise: Property Path Evaluation
// -------------------------------------
const testJSBinBugs = "Very true again...";
const eval1Obj = {
a: 1,
b: {
c: 2,
d: 3,
e: {
f: 4,
g: 5 }
},
h: 6
};
const eval1Tests = {
0: ["a"],
1: ["b"],
2: ["b","c"],
3: ["b","h"],
4: ["b","e"],
5: ["b","z"],
6: ["b","e","g"],
7: ["b","e","y"],
8: ["h"],
9: ["h","p"]
};
function propertyValAt(object,objProp){
let propLevels = objProp.length;
// Given time refactor to iterate **propLevels** times
if(object.hasOwnProperty(objProp[0])){ // level 1
if(object[objProp[0]].hasOwnProperty(objProp[1])){ // level 2
if(object[objProp[0]][objProp[1]].hasOwnProperty(objProp[2])){ // level 3
// Dirty extension added here to go deeper than 3 levels
return object[objProp[0]][objProp[1]][objProp[2]];
}
return object[objProp[0]][objProp[1]];
}
return object[objProp];
}
else { return undefined; }
}
// -------------------------------------
// Exercise: Sum Nested Array
// -------------------------------------
const eval2Obj = [1, 1, 1, [3, 4, [8]], [5]];
const eval2Tests = {
0: [],
1: [1, 1, 1, [3, 4, [8, 5,4]], [5]],
2: [1, 1, 1, [3, 4, [8]], [5]]
// add if time allows
};
function sumNested(inputArr){
let sumCount = 0;
// refactor using recurion if time allows... QWERTY is killing me...
for(let i = 0; i < inputArr.length; i++){
if(typeof inputArr[i] == "number"){ sumCount += inputArr[i]; }
else if(typeof inputArr[i] == "object"){
for(let j = 0; j < inputArr[i].length; j++){
if(typeof inputArr[i][j] == "number"){ sumCount += inputArr[i][j]; }
else if(typeof inputArr[i] == "object"){
for(let k = 0; k < inputArr[i][j].length; k++){
if(typeof inputArr[i][j][k] == "number"){ sumCount += inputArr[i][j][k]; }
}
}
}
}
}
return sumCount;
}
const eval3Obj = [1, 1, 1, [3, 4, [8]], [5]];
const eval3Tests = {
0: "kajsf skf sf dkfsdkj sfdkj",
1: "jsf sfdk dff kdkdd spffs QWERTY",
2: "",
3: "2324 fskjdsfk !@3sfkd.",
4: "This is a short sentence!",
5: "ThisIsA$ReallyLongWord"
// add if time allows
};
// -------------------------------------
// Exercise: Word Count
// -------------------------------------
function wordCount(sentence = "default"){
let wordArr = sentence.split(" ");
// check the spaces*n seperator case
for(let x in wordArr){
if(wordArr[x] == undefined || wordArr[x] == ""){ wordArr.splice(x, 1); }
}
return wordArr.length;
}
// -------------------------------------
// Exercise: Anagram Tester
// -------------------------------------
const eval4Obj = {};
const eval4Tests = {
0: ["abc","bca"],
1: ["abc","cde"],
2: ["dkd","dkk"],
3: ["dkd","dkk"],
4: "",
5: "",
6: "",
7: ""
};
function sumFactorial(num){
console.loc("New state 3.");
if(num < 1){ return 0; }
else if(num == 1){ return 1; }
else{ return num + sumFactorial(num - 1); }
}
function areTheseAnagrams(string1,string2){
let strArr1 = string1.split("").sort();
let strArr2 = string2.split("").sort();
if(strArr1.toString() === strArr2.toString()){ return true; }
else{ return false; }
}
// -------------------------------------
// Exercise: Analyze Prices
// -------------------------------------
const eval5Obj = {};
/*
const eval5Tests = {
0: {"test": [1,2,3,4,5,6,7,8,9,0],
"expected": {
"buyIndex": 0,
"sellIndex": 8 },
1: {"test":[8,4,3,1,5,5,2,8,2,3],
"expected": {
"buyIndex": 0,
"sellIndex": 8 }},
2: {"test": [5,2,5,4,5,6,3,8,1,8],
"expected": {
"buyIndex": 8,
"sellIndex": 9 }},
3: {"test": [1,1,1,1,1],
"expected": {
"buyIndex": null,
"sellIndex": null }},
4: "",
5: "",
6: "",
7: ""
};
*/
function analyzePrices(pricesArr){
let result = {
"buyIndex": null,
"sellIndex": null };
}
// -------------------------------------
// Exercise: Fizz Buzz
// -------------------------------------
const eval6Obj = {};
const eval6Tests = {
0: "",
1: "",
2: "",
3: "",
4: "",
5: "",
6: "",
7: ""
};
function fizzBuzz(n){
let result = "";
// mod behavior is weird, cases are executing that shouldn't pass switch(true) pattern... QWERTY...TIME...
switch(true){
case (n <= 0 ):
result = "";
break;
case (n > 0):
result = n;
case (n % 3 === 0):
result += "fizz";
case (n % 5 === 0):
result += "buzz";
break;
case (typeof n !== "number"):
return "Give me integer...\"" + n + "\", does not work...";
break;
}
return result;
}
// -------------------------------------
// Exercise: Object Oriented Programming - Car
// -------------------------------------
// rewrite using closures to encapsulate private properties and methods... or write a normal class in TS and compile to JS...
class Car {
constructor(make = undefined, model = undefined){
this.make = make;
this.model = model;
this.speed = 0;
this.accel = 0;
this.position = [0,0,0];
}
getInfo(){
let message = "The " + this.make + " " + this.model + " is currently traveling at " + this.speed + " mph...";
if(this.speed > 100){ message += " Whoah there, better slow it down!"; }
return message;
}
getSpeed(){
return this.speed;
}
setSpeed(newSpeed ){
if(this.speed >= 0){ this.speed = newSpeed; }
// else{ this.speed = 0; }
}
stop(){
this.speed = 0;
}
}
function carOOPHelper(){
let nissanGTR = new Car("Nissan","GT-R");
console.log(nissanGTR.getSpeed());
console.log(nissanGTR.getInfo());
nissanGTR.setSpeed(75);
console.log(nissanGTR.getSpeed());
nissanGTR.setSpeed(194);
console.log(nissanGTR.getInfo());
nissanGTR.stop();
console.log(nissanGTR.getInfo());
}
/*
-------------- BONUS SECTION -------------- !!!
*/
// -------------------------------------
// Exercise: Calculate Bowling Score
// -------------------------------------
const eval8Obj = {};
const eval8Tests = {
0: "",
1: "",
2: "",
3: "",
4: "",
5: "",
6: "",
7: ""
};
function calculateBowlingScore(scoreStr){
let score = 0;
let tempStr = scoreStr.split("");
let frameScore = Array(10);
for(let x = 1; x < 11; x++){
/*
1. test first frame of string
2. send to str -> value function
3. remove frame from string
4. set frame score to frameScore array in the proper index
*/
}
// score = sum of frameScore using compare function: (a, b) => (a + b);
return score;
}
// -------------------------------------
// Exercise: Memoization Wrapper
// -------------------------------------
const eval9Obj = {};
const eval9Tests = {
0: "",
1: "",
2: "",
3: "",
4: "",
5: "",
6: "",
7: ""
};
function memoizationWrapper(){
//I want to try this one!
}
</script>
<script id="jsbin-source-javascript" type="text/javascript">// -------------------------------------
// Name: Ben Burford
// -------------------------------------
// -------------------------------------
// Exercise: Property Path Evaluation
// -------------------------------------
const testJSBinBugs = "Very true again...";
const eval1Obj = {
a: 1,
b: {
c: 2,
d: 3,
e: {
f: 4,
g: 5 }
},
h: 6
};
const eval1Tests = {
0: ["a"],
1: ["b"],
2: ["b","c"],
3: ["b","h"],
4: ["b","e"],
5: ["b","z"],
6: ["b","e","g"],
7: ["b","e","y"],
8: ["h"],
9: ["h","p"]
};
function propertyValAt(object,objProp){
let propLevels = objProp.length;
// Given time refactor to iterate **propLevels** times
if(object.hasOwnProperty(objProp[0])){ // level 1
if(object[objProp[0]].hasOwnProperty(objProp[1])){ // level 2
if(object[objProp[0]][objProp[1]].hasOwnProperty(objProp[2])){ // level 3
// Dirty extension added here to go deeper than 3 levels
return object[objProp[0]][objProp[1]][objProp[2]];
}
return object[objProp[0]][objProp[1]];
}
return object[objProp];
}
else { return undefined; }
}
// -------------------------------------
// Exercise: Sum Nested Array
// -------------------------------------
const eval2Obj = [1, 1, 1, [3, 4, [8]], [5]];
const eval2Tests = {
0: [],
1: [1, 1, 1, [3, 4, [8, 5,4]], [5]],
2: [1, 1, 1, [3, 4, [8]], [5]]
// add if time allows
};
function sumNested(inputArr){
let sumCount = 0;
// refactor using recurion if time allows... QWERTY is killing me...
for(let i = 0; i < inputArr.length; i++){
if(typeof inputArr[i] == "number"){ sumCount += inputArr[i]; }
else if(typeof inputArr[i] == "object"){
for(let j = 0; j < inputArr[i].length; j++){
if(typeof inputArr[i][j] == "number"){ sumCount += inputArr[i][j]; }
else if(typeof inputArr[i] == "object"){
for(let k = 0; k < inputArr[i][j].length; k++){
if(typeof inputArr[i][j][k] == "number"){ sumCount += inputArr[i][j][k]; }
}
}
}
}
}
return sumCount;
}
const eval3Obj = [1, 1, 1, [3, 4, [8]], [5]];
const eval3Tests = {
0: "kajsf skf sf dkfsdkj sfdkj",
1: "jsf sfdk dff kdkdd spffs QWERTY",
2: "",
3: "2324 fskjdsfk !@3sfkd.",
4: "This is a short sentence!",
5: "ThisIsA$ReallyLongWord"
// add if time allows
};
// -------------------------------------
// Exercise: Word Count
// -------------------------------------
function wordCount(sentence = "default"){
let wordArr = sentence.split(" ");
// check the spaces*n seperator case
for(let x in wordArr){
if(wordArr[x] == undefined || wordArr[x] == ""){ wordArr.splice(x, 1); }
}
return wordArr.length;
}
// -------------------------------------
// Exercise: Anagram Tester
// -------------------------------------
const eval4Obj = {};
const eval4Tests = {
0: ["abc","bca"],
1: ["abc","cde"],
2: ["dkd","dkk"],
3: ["dkd","dkk"],
4: "",
5: "",
6: "",
7: ""
};
function sumFactorial(num){
console.loc("New state 3.");
if(num < 1){ return 0; }
else if(num == 1){ return 1; }
else{ return num + sumFactorial(num - 1); }
}
function areTheseAnagrams(string1,string2){
let strArr1 = string1.split("").sort();
let strArr2 = string2.split("").sort();
if(strArr1.toString() === strArr2.toString()){ return true; }
else{ return false; }
}
// -------------------------------------
// Exercise: Analyze Prices
// -------------------------------------
const eval5Obj = {};
/*
const eval5Tests = {
0: {"test": [1,2,3,4,5,6,7,8,9,0],
"expected": {
"buyIndex": 0,
"sellIndex": 8 },
1: {"test":[8,4,3,1,5,5,2,8,2,3],
"expected": {
"buyIndex": 0,
"sellIndex": 8 }},
2: {"test": [5,2,5,4,5,6,3,8,1,8],
"expected": {
"buyIndex": 8,
"sellIndex": 9 }},
3: {"test": [1,1,1,1,1],
"expected": {
"buyIndex": null,
"sellIndex": null }},
4: "",
5: "",
6: "",
7: ""
};
*/
function analyzePrices(pricesArr){
let result = {
"buyIndex": null,
"sellIndex": null };
}
// -------------------------------------
// Exercise: Fizz Buzz
// -------------------------------------
const eval6Obj = {};
const eval6Tests = {
0: "",
1: "",
2: "",
3: "",
4: "",
5: "",
6: "",
7: ""
};
function fizzBuzz(n){
let result = "";
// mod behavior is weird, cases are executing that shouldn't pass switch(true) pattern... QWERTY...TIME...
switch(true){
case (n <= 0 ):
result = "";
break;
case (n > 0):
result = n;
case (n % 3 === 0):
result += "fizz";
case (n % 5 === 0):
result += "buzz";
break;
case (typeof n !== "number"):
return "Give me integer...\"" + n + "\", does not work...";
break;
}
return result;
}
// -------------------------------------
// Exercise: Object Oriented Programming - Car
// -------------------------------------
// rewrite using closures to encapsulate private properties and methods... or write a normal class in TS and compile to JS...
class Car {
constructor(make = undefined, model = undefined){
this.make = make;
this.model = model;
this.speed = 0;
this.accel = 0;
this.position = [0,0,0];
}
getInfo(){
let message = "The " + this.make + " " + this.model + " is currently traveling at " + this.speed + " mph...";
if(this.speed > 100){ message += " Whoah there, better slow it down!"; }
return message;
}
getSpeed(){
return this.speed;
}
setSpeed(newSpeed ){
if(this.speed >= 0){ this.speed = newSpeed; }
// else{ this.speed = 0; }
}
stop(){
this.speed = 0;
}
}
function carOOPHelper(){
let nissanGTR = new Car("Nissan","GT-R");
console.log(nissanGTR.getSpeed());
console.log(nissanGTR.getInfo());
nissanGTR.setSpeed(75);
console.log(nissanGTR.getSpeed());
nissanGTR.setSpeed(194);
console.log(nissanGTR.getInfo());
nissanGTR.stop();
console.log(nissanGTR.getInfo());
}
/*
-------------- BONUS SECTION -------------- !!!
*/
// -------------------------------------
// Exercise: Calculate Bowling Score
// -------------------------------------
const eval8Obj = {};
const eval8Tests = {
0: "",
1: "",
2: "",
3: "",
4: "",
5: "",
6: "",
7: ""
};
function calculateBowlingScore(scoreStr){
let score = 0;
let tempStr = scoreStr.split("");
let frameScore = Array(10);
for(let x = 1; x < 11; x++){
/*
1. test first frame of string
2. send to str -> value function
3. remove frame from string
4. set frame score to frameScore array in the proper index
*/
}
// score = sum of frameScore using compare function: (a, b) => (a + b);
return score;
}
// -------------------------------------
// Exercise: Memoization Wrapper
// -------------------------------------
const eval9Obj = {};
const eval9Tests = {
0: "",
1: "",
2: "",
3: "",
4: "",
5: "",
6: "",
7: ""
};
function memoizationWrapper(){
//I want to try this one!
}</script></body>
</html>
// -------------------------------------
// Name: Ben Burford
// -------------------------------------
// -------------------------------------
// Exercise: Property Path Evaluation
// -------------------------------------
const testJSBinBugs = "Very true again...";
const eval1Obj = {
a: 1,
b: {
c: 2,
d: 3,
e: {
f: 4,
g: 5 }
},
h: 6
};
const eval1Tests = {
0: ["a"],
1: ["b"],
2: ["b","c"],
3: ["b","h"],
4: ["b","e"],
5: ["b","z"],
6: ["b","e","g"],
7: ["b","e","y"],
8: ["h"],
9: ["h","p"]
};
function propertyValAt(object,objProp){
let propLevels = objProp.length;
// Given time refactor to iterate **propLevels** times
if(object.hasOwnProperty(objProp[0])){ // level 1
if(object[objProp[0]].hasOwnProperty(objProp[1])){ // level 2
if(object[objProp[0]][objProp[1]].hasOwnProperty(objProp[2])){ // level 3
// Dirty extension added here to go deeper than 3 levels
return object[objProp[0]][objProp[1]][objProp[2]];
}
return object[objProp[0]][objProp[1]];
}
return object[objProp];
}
else { return undefined; }
}
// -------------------------------------
// Exercise: Sum Nested Array
// -------------------------------------
const eval2Obj = [1, 1, 1, [3, 4, [8]], [5]];
const eval2Tests = {
0: [],
1: [1, 1, 1, [3, 4, [8, 5,4]], [5]],
2: [1, 1, 1, [3, 4, [8]], [5]]
// add if time allows
};
function sumNested(inputArr){
let sumCount = 0;
// refactor using recurion if time allows... QWERTY is killing me...
for(let i = 0; i < inputArr.length; i++){
if(typeof inputArr[i] == "number"){ sumCount += inputArr[i]; }
else if(typeof inputArr[i] == "object"){
for(let j = 0; j < inputArr[i].length; j++){
if(typeof inputArr[i][j] == "number"){ sumCount += inputArr[i][j]; }
else if(typeof inputArr[i] == "object"){
for(let k = 0; k < inputArr[i][j].length; k++){
if(typeof inputArr[i][j][k] == "number"){ sumCount += inputArr[i][j][k]; }
}
}
}
}
}
return sumCount;
}
const eval3Obj = [1, 1, 1, [3, 4, [8]], [5]];
const eval3Tests = {
0: "kajsf skf sf dkfsdkj sfdkj",
1: "jsf sfdk dff kdkdd spffs QWERTY",
2: "",
3: "2324 fskjdsfk !@3sfkd.",
4: "This is a short sentence!",
5: "ThisIsA$ReallyLongWord"
// add if time allows
};
// -------------------------------------
// Exercise: Word Count
// -------------------------------------
function wordCount(sentence = "default"){
let wordArr = sentence.split(" ");
// check the spaces*n seperator case
for(let x in wordArr){
if(wordArr[x] == undefined || wordArr[x] == ""){ wordArr.splice(x, 1); }
}
return wordArr.length;
}
// -------------------------------------
// Exercise: Anagram Tester
// -------------------------------------
const eval4Obj = {};
const eval4Tests = {
0: ["abc","bca"],
1: ["abc","cde"],
2: ["dkd","dkk"],
3: ["dkd","dkk"],
4: "",
5: "",
6: "",
7: ""
};
function sumFactorial(num){
console.loc("New state 3.");
if(num < 1){ return 0; }
else if(num == 1){ return 1; }
else{ return num + sumFactorial(num - 1); }
}
function areTheseAnagrams(string1,string2){
let strArr1 = string1.split("").sort();
let strArr2 = string2.split("").sort();
if(strArr1.toString() === strArr2.toString()){ return true; }
else{ return false; }
}
// -------------------------------------
// Exercise: Analyze Prices
// -------------------------------------
const eval5Obj = {};
/*
const eval5Tests = {
0: {"test": [1,2,3,4,5,6,7,8,9,0],
"expected": {
"buyIndex": 0,
"sellIndex": 8 },
1: {"test":[8,4,3,1,5,5,2,8,2,3],
"expected": {
"buyIndex": 0,
"sellIndex": 8 }},
2: {"test": [5,2,5,4,5,6,3,8,1,8],
"expected": {
"buyIndex": 8,
"sellIndex": 9 }},
3: {"test": [1,1,1,1,1],
"expected": {
"buyIndex": null,
"sellIndex": null }},
4: "",
5: "",
6: "",
7: ""
};
*/
function analyzePrices(pricesArr){
let result = {
"buyIndex": null,
"sellIndex": null };
}
// -------------------------------------
// Exercise: Fizz Buzz
// -------------------------------------
const eval6Obj = {};
const eval6Tests = {
0: "",
1: "",
2: "",
3: "",
4: "",
5: "",
6: "",
7: ""
};
function fizzBuzz(n){
let result = "";
// mod behavior is weird, cases are executing that shouldn't pass switch(true) pattern... QWERTY...TIME...
switch(true){
case (n <= 0 ):
result = "";
break;
case (n > 0):
result = n;
case (n % 3 === 0):
result += "fizz";
case (n % 5 === 0):
result += "buzz";
break;
case (typeof n !== "number"):
return "Give me integer...\"" + n + "\", does not work...";
break;
}
return result;
}
// -------------------------------------
// Exercise: Object Oriented Programming - Car
// -------------------------------------
// rewrite using closures to encapsulate private properties and methods... or write a normal class in TS and compile to JS...
class Car {
constructor(make = undefined, model = undefined){
this.make = make;
this.model = model;
this.speed = 0;
this.accel = 0;
this.position = [0,0,0];
}
getInfo(){
let message = "The " + this.make + " " + this.model + " is currently traveling at " + this.speed + " mph...";
if(this.speed > 100){ message += " Whoah there, better slow it down!"; }
return message;
}
getSpeed(){
return this.speed;
}
setSpeed(newSpeed ){
if(this.speed >= 0){ this.speed = newSpeed; }
// else{ this.speed = 0; }
}
stop(){
this.speed = 0;
}
}
function carOOPHelper(){
let nissanGTR = new Car("Nissan","GT-R");
console.log(nissanGTR.getSpeed());
console.log(nissanGTR.getInfo());
nissanGTR.setSpeed(75);
console.log(nissanGTR.getSpeed());
nissanGTR.setSpeed(194);
console.log(nissanGTR.getInfo());
nissanGTR.stop();
console.log(nissanGTR.getInfo());
}
/*
-------------- BONUS SECTION -------------- !!!
*/
// -------------------------------------
// Exercise: Calculate Bowling Score
// -------------------------------------
const eval8Obj = {};
const eval8Tests = {
0: "",
1: "",
2: "",
3: "",
4: "",
5: "",
6: "",
7: ""
};
function calculateBowlingScore(scoreStr){
let score = 0;
let tempStr = scoreStr.split("");
let frameScore = Array(10);
for(let x = 1; x < 11; x++){
/*
1. test first frame of string
2. send to str -> value function
3. remove frame from string
4. set frame score to frameScore array in the proper index
*/
}
// score = sum of frameScore using compare function: (a, b) => (a + b);
return score;
}
// -------------------------------------
// Exercise: Memoization Wrapper
// -------------------------------------
const eval9Obj = {};
const eval9Tests = {
0: "",
1: "",
2: "",
3: "",
4: "",
5: "",
6: "",
7: ""
};
function memoizationWrapper(){
//I want to try this one!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment