Created
January 3, 2015 22:14
-
-
Save greggnakamura/f8eaf88411c7dd9e0b4a to your computer and use it in GitHub Desktop.
Javascript: function example
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
/* functions */ | |
/* passing a function as a parameter */ | |
var calculate = function (number, paramTwo, fn) { | |
number = number + 3; | |
number = number + 1; | |
number = number * 8; | |
return fn(number, paramTwo); | |
}; | |
/* function parameter for 'fooResult' */ | |
function sum (number, paramTwo) { | |
return number + paramTwo; | |
} | |
/* function parameter for 'barResult' */ | |
function product (number, paramTwo) { | |
return number * paramTwo; | |
} | |
var foo = 2, | |
bar = 3; | |
var fooResult = calculate (foo, 2, sum); | |
var barResult = calculate (bar, 2, product); | |
console.log('foo: ' + fooResult); | |
console.log('bar: ' + barResult); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment