Skip to content

Instantly share code, notes, and snippets.

@dohoonk
Created February 17, 2016 02:23
Show Gist options
  • Select an option

  • Save dohoonk/a0ab8dd5e19943d01293 to your computer and use it in GitHub Desktop.

Select an option

Save dohoonk/a0ab8dd5e19943d01293 to your computer and use it in GitHub Desktop.
codecore
#javascript
var count = function(string){
var newString = string.split(" ");
myObject = {};
for(var i = 0; i < newString.length; i++){
myObject[newString[i]] = newString[i].length
}
}
console.log(count("Hello Tony"))
var returnZero = function(x) {
return x - x;
};
console.log(returnZero(5));
var user = function(userObject) {
console.log( userObject.name + "is" + userObject.age + "years old." );
};
## typeof
typeof(123)
"number"
typeof({a: 1, b: 2});
"object"
Array.isArray([1,2,3,4,5]);
true
var car = {
speed: 200,
drive: function(){
console.log("vroomm")
}
}
car.speed;
200
car.drive();
Vroooom
var car = {
speed: 200,
drive: function(){
console.log("vroomm");
},
stop: function(){
console.log("screeetch");
}
}
car.stop()
## this
var car = {
speed: 200,
drive: function(){
console.log("vroomm" + this.speed + "km/hr");
},
stop: function(){
console.log("screeetch");
}
}
var car = {
speed: 200,
drive: function(){
console.log("vroomm");
},
stop: function(){
if(this.speed === "fast"){
console.log("Screeeeetch!!!");
}else if(this.speed === "medium"){
console.log("RRrch");
}else if(this.speed === "slow"){
console.log("sh");
}else if(typeof(this.speed) === "number") {
var e = []
for(var i = 0; i < this.speed; i++){
e.push("e")
}
e.join(" ")
console.log("Scr" + e + "tch!!");
}else {
console.log("I dont know");
}
}
}
car.stop()
#higherorder
var MyFunction = function(anotherFunction) {
console.log(antherFunction(10)
}
var addFive = function(x){
return x + 5;
}
myFunction(addFive);
---------------------------------------------------------------------------
var call = function(num, anotherFunction){
for(var i = 0; i < count; i++){
num = anotherFunction(num);
}
return anotherFunction(num);
};
var doubleIt = function(x) {
return 2 * x
}
console.log(call(5, doubleIt))
-------------------------------------------------------------------------
var map = function(array, anotherFunction){
for(var i = 0; i < array.length; i++ ){
array[i] = anotherFunction(array[i]);
} return array;
};
var addOne = function(x){
return x + 1;
};
console.log(map([1,2,3], addOne));
-----------------------------------------------------
## foreach
[1,2,3,4,5].forEach(function(x){
console.log(x);
});
var map = function(array, fn) {
}
-------------------------------------------------------
setTimeout(function(){
console.log("Hello, World!")
}, 3000);
console.log("loading...")
var x =function(x) {
console.log(".")
setTimeout(function(x){
},1000);
}
---------------------------------------------------------------------
var count = function(){
var odd = 1, even =2,
setInterval(function(){
console.log("Odd: " + odd + " Even: " + even);
odd += 2;
even += 2;
}, 1000);
}();
var num = 0;
setInterval(function(){
if(num % 2 == 0) {
console.log("counter 1: " + num);
}
num ++;
} ,1000)
-----------------------------------------------------------------------------------
1.
var ageCheck = function(age){
if(age > 50){
console.log("OLD");
} else{
console.log("YOUNG");
}
};
ageCheck(80)
2.
var forEach = function(array,anotherFunction){
for(var i = 0; i < array.length; i++){
array[i] = anotherFunction(array[i]);
}
return array;
};
var sum = function(x){
return x += 1
};
forEach([1,2,3,4],sum)
3.
var sayLater = function(string){
setTimeout(function(){
console.log(string)
},5000);
};
sayLater("HI")
-------------------------------------------------------------------------------------------------------------
1. Personable
var tony = {
name: "Tony",
age: 27,
compare: function(other){
if(other.age > this.age){
console.log("Hello, " + other.name +". I am "+ this.name + ". Please to meet you" )
}else {
console.log("Yo " + other.name + ". I'm totally " + this.name + ". Wsup!")
}
}
}
var mike = {
name: "Mike",
age: 25
}
tony.compare(mike)
2.
var isPositive = function(obj){
if(typeof(obj) === "number" && obj >= 0){
return false
}else if(typeof(obj) === "number"){
return true
}else{
return "!false"
}
}
isPositive("yo")
3.
var filter = function(array, af) {
newArray = []
for(var i = 0; i < array.length; i++){
newArray.push(af(array[i]))
}return newArray
}
var isPositive = function(obj){
if(typeof(obj) === "number" && obj >= 0){
return false
}else if(typeof(obj) === "number"){
return true
}else{
return "!false"
}
}
filter([1,2,-5,"toy"],isPositive)
4.
var count = 1;
var timer = 1000;
var counter = function(){
console.log(count);
interval;
if(count === 10){
clearInterval(interval);
}
};
var interval = setInterval(function(){
console.log(++count);
},timer += 1000);
counter();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment