Advertisement
ralitsa_d

Scores to HTML

Oct 5th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function scoreToHtml(scoreStr) {
  2.     let scoreArr = JSON.parse(scoreStr);
  3.  
  4.     let html = '<table>\n';
  5.  
  6.     html += `\t<tr><th>name</th><th>score</th>\n`;
  7.  
  8.     function htmlEscape(text) {
  9.         text = text.toString();
  10.         let map = {'"': '&quot;', '&': '&amp;', "'": '&#39;', '<': '&lt;', '>':'&gt;'};
  11.         return text.replace(/[\"&'<>"]/g, ch => map[ch]);
  12.     }
  13.  
  14.     for (let score of scoreArr){
  15.         html += '\t<tr>';
  16.         html += `<td>${htmlEscape(score['name'])}</td><td>${htmlEscape(score['score'])}</td>`;
  17.         html +=  `</tr>\n`;
  18.     }
  19.  
  20.     html += '</table>';
  21.     return html;
  22.  
  23. }
  24.  
  25. console.log(scoreToHtml(['[{"name":"<div>Pesho","score":479},{"name":"Gosho","score":205}]']));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement