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
let numbers = [4,6,3,8]; | |
let max = Math.max(...numbers); | |
console.log(max); |
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
# Object oriented programming | |
```js | |
var answer = { | |
get: function fn1() { | |
return this.val; | |
}, | |
val: 42 | |
}; | |
``` |
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
var counter = (function() { | |
var i = 0; | |
return { | |
get: function() { | |
return i; | |
}, | |
set: function(val) { | |
i = val; | |
}, |
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
Write a function, funcCaller, that takes a func (a function) and an arg (any data type). The function returns the func called with arg(as an argument). | |
var funcCaller = function(func, arg) { | |
return func(arg); | |
} | |
Write a function, firstVal, that takes an array, arr, and a function, func, and calls func with the first index of the arr, the index # and the whole array. | |
var firstVal = function(arr, func){ | |
return func(arr[0], 0, arr); | |
} | |
Change firstVal to work not only with arrays but also objects. Since objects are not ordered, you can use any key-value pair on the object. |
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
var Toaster = function(){ | |
//some private methods and properties | |
var maxTemp = 500, | |
temp: 0; | |
return { | |
//some public methods and properties, etc | |
setTemp: function(newTemp) { | |
if (newTemp > maxTemp) { | |
console.log("That temp is too high!"); | |
} else { |
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
var increment = function(n){ return n + 1; }; | |
var square = function(n){ return n*n; }; | |
var doMath = function(n, func){ return func(n); }; | |
var squareAnswer = doMath(5, square); | |
var plusOne = doMath(4, increment); | |
console.log(squareAnswer); | |
console.log(plusOne); |
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
var ifElse = function(condition, isTrue, isFalse){ | |
if(condition){ | |
isTrue(); | |
} else { | |
isFalse(); | |
} | |
}; | |
var logTrue = function(){ console.log(true); }; | |
var logFalse = function(){ console.log(false); }; |
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
var Module = function(){ | |
var privateProperty = 'foo'; | |
function privateMethod(args){ | |
// do something | |
}; | |
return { | |
publicProperty: "", | |
publicMethod: function(args){ | |
// do something |
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
var nonsense = function(string){ | |
var blab = function(){ | |
alert(string); | |
}; | |
setTimeout(blab, 2000); | |
} | |
nonsense('blah blah'); |
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
function counter() { | |
var n = 0; | |
return { | |
count: function() {return ++n; }, | |
reset: function() {n = 0;} | |
} | |
}; | |
var myCounter = counter(), num; | |
myCounter.count(); |
NewerOlder