Created
April 16, 2018 19:59
-
-
Save lastnamearya/acd7182bea6fdecda64a3ec6d0e7c120 to your computer and use it in GitHub Desktop.
Imperative Programming vs Declarative Programming
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
1. Write a function called double which takes in an array of numbers and returns a new array after doubling every item in that array. double([1,2,3]) -> [2,4,6] | |
function double (arr) { | |
let results = [] | |
for (let i = 0; i < arr.length; i++){ | |
results.push(arr[i] * 2) | |
} | |
return results | |
} | |
// Declarative Approach | |
function double (arr) { | |
return arr.map((item) => item * 2) | |
} | |
2. Write a function called add which takes in an array and returns the result of adding up every item in the array. add([1,2,3]) -> 6 | |
function add (arr) { | |
let result = 0 | |
for (let i = 0; i < arr.length; i++){ | |
result += arr[i] | |
} | |
return result | |
} | |
// Declarative Approach | |
function add (arr) { | |
return arr.reduce((prev, current) => prev + current, 0) | |
} | |
3. Using jQuery (or vanilla JavaScript), add a click event handler to the element which has an id of btn. When clicked, toggle (add or remove) the highlight class as well as change the text to Add Highlight or Remove Highlight depending on the current state of the element. | |
$("#btn").click(function() { | |
$(this).toggleClass("highlight") | |
$(this).text() === 'Add Highlight' | |
? $(this).text('Remove Highlight') | |
: $(this).text('Add Highlight') | |
}) | |
// Declarative Approach | |
<Btn | |
onToggleHighlight={this.handleToggleHighlight} | |
highlight={this.state.highlight}> | |
{this.state.buttonText} | |
</Btn> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment