Advertisement
sivancheva

02.ScoreToHTML

Sep 4th, 2018
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. function scoreToHTML(inputArr) {
  2.  
  3. let result = "<table>\n";
  4.  
  5. result += " <tr><th>name</th><th>score</th></tr>\n";
  6.  
  7. for (let line of inputArr) {
  8. result += ` <tr><td>${escapeHTML(line.name)}</td><td>${+line.score}</td></tr>\n`;
  9. }
  10. result += "</table>";
  11. console.log(result);
  12.  
  13. function escapeHTML(str) {
  14. "use strict";
  15. str = str.replace(/&/g, "&amp;")
  16. .replace(/>/g, "&gt;")
  17. .replace(/</g, "&lt;")
  18. .replace(/"/g, "&quot;")
  19. .replace(/'/g, "&#39;");
  20. return str;
  21. }
  22. }
  23.  
  24. //Uslovie
  25. // 2. Score to HTML
  26. // You are given a JSON string representing an array of objects, parse the JSON and create a table using the supplied objects. The table should have 2 columns "name" and "score", each object in the array will also have these keys.
  27. // Any text elements must also be escaped in order to ensure no dangerous code can be passed.
  28. // You can either write the HTML escape function yourself or use the one from the Strings and Regular Expressions Lab.
  29. // The input comes as a single string argument – the array of objects as a JSON.
  30. // The output should be printed on the console – a table with 2 columns - "name" and "score", containing the values from the objects as rows.
  31. // Input
  32. // '[{"name":"Pesho","score":479},{"name":"Gosho","score":205}]'
  33. // Output
  34. // <table>
  35. // <tr><th>name</th><th>score</th></tr>
  36. // <tr><td>Pesho</td><td>479</td></tr>
  37. // <tr><td>Gosho</td><td>205</td></tr>
  38. // </table>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement