Last active
August 29, 2015 13:56
-
-
Save harshai/9144336 to your computer and use it in GitHub Desktop.
Demonstrating functional aspects of JavaScript
This file contains hidden or 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 numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | |
| var sumOfEvenSquares = numbers.filter(function(num){ | |
| return num % 2 == 0; | |
| }).map(function(evenNum){ | |
| return evenNum * evenNum; | |
| }).reduce(function(prev, next){ | |
| return prev + next; | |
| }, 0); |
This file contains hidden or 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 tempC = [-4, 102, 100, -8 ,6, 0, -40]; | |
| function freezingTemps(arr){ | |
| var subZero = [] | |
| for(var i = 0, length = arr.length; i<length; i++){ | |
| if(arr[i]<0){ | |
| subZero.push(arr[i]) | |
| } | |
| } | |
| } | |
| function filterFreezing(arr){ | |
| return arr.filter(function(temp){ | |
| return temp < 0; | |
| }) | |
| } |
This file contains hidden or 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 tempC = [-4, 102, 100, -8 ,6,]; | |
| function forLoop(arr){ | |
| for(var i = 0, length = arr.length; i< length; i++){ | |
| console.log(arr[i] + " °C"); | |
| } | |
| } | |
| function forEachLoop(arr){ | |
| arr.forEach(function(temp){ | |
| console.log(temp + " °C") | |
| }) | |
| } |
This file contains hidden or 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 tempC = [-4, 102, 100, -8 ,6, 0, -40]; | |
| function converToFar(arr){ | |
| var convertedArr = []; | |
| for(var i = 0, length = arr.length; i<length; i++){ | |
| var convertedTemp = arr[i] * 9/5 + 32; | |
| convertedArr.push(convertedTemp) | |
| } | |
| return convertedArr; | |
| } | |
| function mapToFar(arr){ | |
| return arr.map(function(temp){ | |
| return temp * 9/5 +32; | |
| } | |
| } |
This file contains hidden or 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 tempC = [-4, 102, 100, -8 ,6, 0, -40]; | |
| function sumOfTemps(arr){ | |
| var sum = 0; | |
| for(var i = 0, length = arr.length; i<length; i++){ | |
| sum += arr[i]; | |
| } | |
| return sum; | |
| } | |
| function reduceSum(arr){ | |
| return arr.reduce(function(prev, next){ | |
| return prev + next; | |
| },0) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment