with javascript 2c can html markup be added in a text string 3f

Solutions on MaxInterview for with javascript 2c can html markup be added in a text string 3f by the best coders in the world

showing results for - "with javascript 2c can html markup be added in a text string 3f"
Laura
10 Oct 2020
1const text = 'This is a test to see whether nested style spans work properly.'
2const styling = [
3  {start: 22, end: 54, type: "strong"},
4  {start: 30, end: 36, type: "a"},
5  {start: 37, end: 48, type: "em"},
6  {start: 43, end: 48, type: "a"}
7];
8
9const result = [...text].reduce((a, v, i) => {
10  styling.filter(s => s.start === i).forEach(s => a += `<${s.type}>`);
11  styling.filter(s => s.end === i).forEach(s => a += `</${s.type}>`);
12  return a + v;
13}, '');
14
15document.body.innerHTML = result;
16
17// EXMAPLE FROM STACKOVERFLOW.COM //
18// OUTPUT: 
19// This is a test to see <strong>whether <a>nested</a> <em>style <a>spans</em></a> work </strong>properly.