Created
March 17, 2014 20:21
-
-
Save shinnn/9607524 to your computer and use it in GitHub Desktop.
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'; | |
function arrayWriteOut(arr, process) { | |
if (!process) { | |
process = function(str) { | |
return str; | |
}; | |
} | |
var result = ''; | |
for (var i=0; i < arr.length; i++) { | |
if (i > 0) { | |
if (i < arr.length - 1) { | |
result += ', '; | |
} else { | |
result += ' and '; | |
} | |
} | |
result += process.call(null, arr[i], i); | |
} | |
return result; | |
} | |
if (typeof module !== 'undefined' && module.exports) { | |
module.exports = arrayWriteOut; | |
} else { | |
window.arrayWriteOut = arrayWriteOut; | |
} | |
}()); |
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 doubleQuote(str) { | |
return '"' + str + '"'; | |
} | |
arrayWriteOut(['Apple', 'Orange', 'Blueberry'], doubleQuote); | |
//=> "Apple", "Orange" and "Blueberry" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment