Advertisement
kstoyanov

02. Score to HTML

Sep 22nd, 2020
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function scoreToHTML(input) {
  2.   const inputArr = JSON.parse(input);
  3.  
  4.   const escapeHTML = (arg) => {
  5.     const str = arg.replace(/&/g, '&')
  6.       .replace(/>/g, '>')
  7.       .replace(/</g, '&lt;')
  8.       .replace(/"/g, '&quot;')
  9.       .replace(/'/g, '&#39;');
  10.     return str;
  11.   };
  12.  
  13.   let result = '<table>\n';
  14.  
  15.   result += '  <tr><th>name</th><th>score</th></tr>\n';
  16.  
  17.   inputArr.forEach((line) => {
  18.     result += `  <tr><td>${escapeHTML(line.name)}</td><td>${+line.score}</td></tr>\n`;
  19.   });
  20.   result += '</table>';
  21.   console.log(result);
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement