Skip to content

Instantly share code, notes, and snippets.

@Shiggiddie
Last active August 29, 2015 14:20
Show Gist options
  • Save Shiggiddie/930320949623605c0ec3 to your computer and use it in GitHub Desktop.
Save Shiggiddie/930320949623605c0ec3 to your computer and use it in GitHub Desktop.
Bad array function use
// 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