Encode string with HTML tags at given characters | JavaScript Interview Question - 59

preview_player
Показать описание
JavaScript Interview Question - 59 | In this video, we will see how to solve a medium-difficulty problem asked in a frontend engineer interview to SDE1, SDE2, and SDE3.

Given a string and a style array render HTML pretty much like a rich text editor.
For example: 'Hello, world', [[0, 2, 'i'], [4, 9, 'b'], [7, 10, 'u']]

You can expect this frontend coding question in Rippling, Uber, Flipkart, Atlassian, Meta, Google, Microsoft, Dropbox, TCS, Infosys, Wipro, Cognizant, Capgemini, Accenture, and other product-based organizations' interviews.

Loved the question? I have 120+ solved problems in my ebook "JavaScript Interview Guide". If you are preparing for Interviews and looking for a solutions book, Get my Ebook.

Social links
Рекомендации по теме
Комментарии
Автор

Yes, this addresses the problem that I'm facing at the moment.

mountains
Автор

I thin you have over complicated it ?
We can easilt do it via array
function encodeStringWithStyle(str, styles) {
let result = '';

styles.forEach(style => {
const [start, end, tag] = style;

// Concatenate the part of the string before the styled section
result += str.substring(0, start);

// Add the styled section with the specified HTML tag
result += `<${tag}>${str.substring(start, end + 1)}</${tag}>`;

// Update the string to exclude the processed part
str = str.substring(end + 1);
});

// Add the remaining part of the string after the last styled section
result += str;

return result;
}

jagjitsingh-mkev