A Pen by Stephen Huh on CodePen.
Created
July 23, 2016 14:07
-
-
Save stephenhuh/7afb389d9157120c3f096b84948d6ca0 to your computer and use it in GitHub Desktop.
JS Scratchpad - 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form> | |
<input id="target" type="text" value="Hello there"> | |
</form> | |
<div id="other"> | |
Trigger the handler | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$( "#target" ).keyup(function(e) { | |
console.log(e); | |
alert( "Handler for .keyup() called." ); | |
}); | |
var arr = [1,2,3,4]; | |
var entries = arr.entries; | |
console.log(entries); | |
console.log(entries.next().value); | |
/* | |
//#dot notation and bracket notation and objects | |
var variablechange = 2; | |
//when heretoo is undefined the box object becomes undefined | |
var box = { | |
"materialstring": "cardboardstring", | |
material: "cardboard", | |
nonvar: "yolo", | |
"materialstring2": variablechange, | |
//box[0] -> "meow", box["0"] -> "meow", box.0 -> error | |
0: 'meow', | |
'^&*': "testing 123" | |
}; | |
//#for in loops and scope | |
var obj = { | |
a: 1, | |
b: 2, | |
c: 3 | |
}; | |
for (var prop in obj) { | |
//console.log("obj." + prop + " = " + obj[prop]); | |
} | |
//console.log("the prop still exists outside of the function due to function scope:" + prop); | |
//Frontendmasters fundamentals-functional - Objects | |
let animal = {}; | |
animal.username = "sexy-pig"; | |
animal["tagline"] = "you wanna oink"; | |
let noises = []; | |
animal["noises"] = noises; | |
let counter = 0; | |
for (let key in animal) { | |
counter++; | |
//if (key === 'username') {console.log('Hi my name is ' + animal[key]);} | |
//if (key === 'tagline') {console.log ('I like to say ' + animal[key]);} | |
} | |
//Frontendmasters fundamentals-functional - Arrays | |
var arr = []; | |
arr.push(2, 3); | |
arr["hello"] = "yolo"; | |
arr["dude"] = "this is fun"; | |
//arr.hello -> "yolo", arr["hello] -> yolo | |
for (let key in arr) { | |
//console.log("the key is : " + key); | |
//console.log("and the value is : " + arr[key]); | |
//console.log(arr.key) -----> error - remember dot notation doesnt work with variables | |
} | |
//so there's a property called length on arrays... | |
arr.length; //do these work? | |
arr[length]; //what do these track? | |
let arr2 = [1, 2, 3]; | |
arr2[5] = "Hello"; | |
arr2.length // --> 6 | |
//so interestingly arr2 just puts out the highest index, and doesnt count the actual indices. | |
//arr2 fills in the rest of the indices as undefined --> [1,2,3, undefined, undefined, "Hello"] | |
//or rather those values are exactly as it says, undefined. remember the difference between undefined and null | |
let noiseArray = ["Loud Noise"]; | |
noiseArray.unshift("quiet noise"); | |
noiseArray.push("super loud"); | |
noiseArray[3] = "loudest thus far"; | |
animal.noises = noiseArray; | |
//Come up with two ways you can add an element to the end of an array, without knowing the exact length of the array. | |
noiseArray.push("at the end!!"); | |
noiseArray[noiseArray.length] = "i fuckin got this"; | |
let animals = []; | |
animals[0] = animal; | |
let quackers = { | |
username: 'DaffyDuck', | |
tagline: 'Yippeee!', | |
noises: ['quack', 'honk', 'sneeze', 'growl'] | |
} | |
animals.push(quackers); | |
let dog = {}; | |
dog["username"] = "dope canine"; | |
dog["tagline"] = "im pretty happy to see people"; | |
dog.noises = []; | |
dog.noises.push("ruff"); | |
animals.push(dog); | |
let cat = {}; | |
cat["username"] = "yolo"; | |
//note you 1000% need the quotes around when using bracket notation unless its a variable or expression | |
cat["tagline"] = "meow"; | |
cat.noises = ['who u tryna fuck w'] | |
cat.noises[cat.noises.length] = "last one?"; | |
//#codewars | |
//array.prototype.concat() | |
//notice that you cannot do var arr[a,b,c] as a declaration because a is not defined | |
let arrz = [1,2,3]; | |
let arry = [4,5,6]; | |
let arrx = [7,8,9]; | |
//filter | |
let arra = [3,5,7,9]; | |
let filtered = arra.filter(num => num <= 5); | |
function length(head){ | |
head ? 1 + length(head.next) : 0; | |
} | |
function add (a){ | |
a++ | |
return a; | |
} | |
function add(arr){ | |
arr.pop(); | |
return arr; | |
} | |
//#this notes | |
function subtract(x){ | |
let val = 5; | |
return this.val - x; | |
} | |
function checkThisOut ( b ) { | |
var x = b; | |
this.x = b; | |
return 2; | |
} | |
function myFunc(){ | |
console.log(this); | |
}; | |
var objectLiteral = { | |
myFunc: function () {console.log(this);} | |
} | |
//closures | |
var closureAlert = function(){ | |
var alerter = function(){ | |
var x = 0; | |
console.log(++x); | |
} | |
return alerter(); | |
} | |
var funcCalled = closureAlert; | |
var funcCall = closureAlert(); | |
var paramFunc = function(x,y){ | |
var x = x + y; | |
console.log(x); | |
} | |
//FEM - DS & ALGOS | |
//Create a unique constructor that makes a building of your choice using the pseudoclassical pattern. | |
//Don't forget to add methods and practice creating instances. | |
//do i need it as an expression? or as a declaration | |
var Building = function(floors, color, style, costPerMonth, size){ | |
this.floors = floors; | |
this.color = color; | |
this.style = style; | |
this.costPerMonth = costPerMonth; | |
this.size = size; | |
} | |
//methods | |
Building.prototype.countFloors = function(){ | |
console.log(this.floors); | |
return this.floors; | |
} | |
Building.prototype.determineValue = function(){ | |
this.cost > 5000 ? "NOT WORTH IT" : "WORTH IT" | |
}; | |
var modernHouse = new Building(2, "all black", "modern"); | |
var emptyHouse = new Building(); | |
//Quizlet Review | |
var arr = "1234"; | |
var arr2 = Array.from(arr); | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment