Created
October 24, 2018 15:52
-
-
Save mmmaaatttttt/d762c6f06707a539979097f3ab08e49a to your computer and use it in GitHub Desktop.
Mastering JS Functions Examples
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
// map examples | |
[1, 2, 3, 4].map(function(num) { | |
return 2 * num; | |
}); | |
[1, 2, 3, 4].map(function(num) { | |
if (num % 2 === 0) return "even"; | |
else return "odd"; | |
}); | |
// example with named functions | |
function double(num) { return 2 * num; } | |
function triple(num) { return num * 3; } | |
function isEven(num) { return num % 2 === 0; } | |
[1,2,3,4].map(double); | |
[1, 2, 3, 4, 5, 6, 7].map(triple); | |
[1, 2, 3, 4, 5, 6, 7].map(isEven); | |
// filter examples | |
[1, 2, 3, 4].filter(function(num) { | |
return num < 3; | |
}); | |
[1, 2, 3, 4, 5, 6, 7].filter(isEven); |
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
// anonymous callback | |
setTimeout(function() { | |
console.log("hi!!!"); | |
}, 1000); | |
// named callback | |
function friendlyGreeting() { | |
console.log("OMG HI SO NICE TO SEE YOU!!!!!!!!!!!!!!"); | |
} | |
setTimeout(friendlyGreeting, 2000); |
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 add(x, y) { | |
return x + y; | |
} | |
function subtract(x, y) { | |
return x - y; | |
} | |
function doSomeMath(operation, x, y) { | |
return operation(x, y); | |
} | |
// doSomeMath is a higher order function | |
// it can take add or subtract as a callback. | |
doSomeMath(add, 1, 4); | |
doSomeMath(subtract, 4, 2); |
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 makeBankAccount(pin) { | |
var balance = 1000; | |
var publicMethods = { | |
deposit: function(pinGuess, amount) { | |
if (pinGuess !== pin) return "Invalid credentials"; | |
balance += amount; | |
return "Deposit successful! Your balance is: " + balance; | |
}, | |
withdraw: function(pinGuess, amount) { | |
if (pinGuess !== pin) return "Invalid credentials"; | |
if (amount > balance) return "You don't have enough $$$"; | |
balance -= amount; | |
return "Withdrawal successful! Your balance is: " + balance; | |
} | |
} | |
return publicMethods; | |
} | |
// Matt gets a bank account! | |
var mattAccount = makeBankAccount("1234"); | |
// Jimmy gets a bank account! | |
var jimmyAccount = makeBankAccount("1111"); |
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 guessingGame() { | |
var randomNumber = Math.floor(Math.random() * 1000); | |
return function guess(num) { | |
if (num < randomNumber) { | |
return "hahaha too low!"; | |
} else if (num > randomNumber) { | |
return "lolz too high!" | |
} else { | |
return "Oh no, you have found my secret number, and it is " + randomNumber + ". :(" | |
} | |
} | |
} | |
// newGame will have a randomNumber inside of it, | |
// but there's no way to deduce its value without | |
// playing (and winning) the game. | |
var newGame = guessingGame() |
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 sayMyNameLater() { | |
var firstName = "Matt"; | |
return function sayMyNameSayMyName() { | |
console.log(firstName); | |
} | |
} | |
var lolwut = sayMyNameLater(); | |
function sayMyNameSayMyName() { | |
console.log(firstName); | |
} | |
sayMyNameSayMyName(); // ReferenceError: firstName is not defined | |
lolwut(); // "Matt" |
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 matt() { | |
var firstName = "Matt"; | |
console.log(firstName); | |
} | |
matt(); // logs "Matt" | |
console.log(firstName); // ReferenceError: firstName is not defined | |
var x = 1; | |
function foo() { | |
var y = 2; | |
function bar() { | |
var x = 5; | |
var z = 3; | |
console.log(x + y + z) | |
} | |
bar(); | |
} | |
foo(); // 10 | |
function sayMyName() { | |
var firstName = "Matt"; | |
function sayMyNameSayMyName() { | |
console.log(firstName); | |
} | |
sayMyNameSayMyName(); | |
} | |
sayMyName(); // "Matt" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment