Last active
August 29, 2015 14:20
-
-
Save Shiggiddie/930320949623605c0ec3 to your computer and use it in GitHub Desktop.
Bad array function use
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
// Problem: given an array of elements, create the array function to turn that array into | |
// a string that will represent an html table containing the array elements, one element per row, | |
// with each element appended with "foo" | |
// E.g. The following array... | |
var arr = ["hi", "bye", "todo", "fly"]; | |
// ...becomes: | |
// <html> | |
// <body> | |
// <table> | |
// <tbody> | |
// <tr> | |
// hifoo | |
// </tr> | |
// ... | |
// </tbody> | |
// </table> | |
// </body> | |
// </html> | |
// (newlines and indentation is provided above for readability only) | |
// Bad solution: | |
var html = "<html><body><table><tbody>"; | |
var addFoo = function(s) { | |
return s + "foo"; | |
} | |
html += "<tr>" + arr.map( | |
function(el) { | |
return addFoo(el); | |
} | |
).join('</tr><tr>') + '</tr></tbody></table></body></html>'; | |
console.log(html) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment