Last active
August 24, 2017 00:19
-
-
Save luciomartinez/752744ec66ce684d8b242dde1ac05ce1 to your computer and use it in GitHub Desktop.
Functional vs imperative vs OOP in JavaScript 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
function() { | |
"use strict"; | |
var capify = function(str) { | |
return [str.charAt(0).toUpperCase(), str.substring(1)].join(""); | |
}; | |
var processWords = function(fn, str) { | |
return str.split(" ").map(fn).join(" "); | |
}; | |
document.getElementById("main_button").addEventListener("click", function(e) { | |
var something = prompt("Give me something to capitalize"); | |
alert(processWords(capify, 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 result; | |
function getText() { | |
var someText = prompt("Give me something to capitalize"); | |
capWords(someText); | |
alert(result.join(" ")); | |
}; | |
function capWords(input) { | |
var counter; | |
var inputArray = input.split(" "); | |
var transformed = ""; | |
result = []; | |
for (counter = 0; counter < inputArray.length; counter++) { | |
transformed = [ | |
inputArray[counter].charAt(0).toUpperCase(), | |
inputArray[counter].substring(1) | |
].join(""); | |
result.push(transformed); | |
} | |
}; | |
document.getElementById("main_button").onclick = getText; |
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() { | |
"use strict"; | |
var SomeText = function(text) { | |
this.text = text; | |
}; | |
SomeText.prototype.capify = function(str) { | |
var firstLetter = str.charAt(0); | |
var remainder = str.substring(1); | |
return [firstLetter.toUpperCase(), remainder].join(""); | |
}; | |
SomeText.prototype.capifyWords = function() { | |
var result = []; | |
var textArray = this.text.split(" "); | |
for (var counter = 0; counter < textArray.length; counter++) { | |
result.push(this.capify(textArray[counter])); | |
} | |
return result.join(" "); | |
}; | |
document.getElementById("main_button").addEventListener("click", function(e) { | |
var something = prompt("Give me something to capitalize"); | |
var newText = new SomeText(something); | |
alert(newText.capifyWords()); | |
}); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment