Using template literals with loop structures allows for efficient generation of formatted text content. The backtick syntax enables multi-line strings and variable interpolation through the ${} syntax.
const teacherList = [
"Chen*ming (Mathematics)",
"Li*wei (Physics)",
"Wang*fang (Chemistry)",
"Zhao*qiang (Biology)",
"...",
"Zhou*lin (Economics)"
];
for (const teacher of teacherList) {
console.log(`${teacher}\nVery Satisfied\nSatisfied\nNeutral\nDissatisfied\n\n${teacher} - Reason for Dissatisfaction [Multi-select]\nA. Discrimination or inappropriate punishment of students;\nB. Organizing paid tutoring sessions;\nC. Working at external training institutions;\nD. Soliciting gifts from students or parents;\nE. Poor teaching quality;\nF. Inadequate classroom management;\nG. Disrespectful behavior toward parents;\nH. Accepting gifts or money from parents;\nI. Other reasons.\n\n`);
}
Rendering Content in a New Window
For better presentation, content can be rendreed in a separate browser window. This approach accumulates the text into a variable first, then opens a new document stream for display.
let outputContent = "";
const staffMembers = [
"Chen*ming (Mathematics)",
"Li*wei (Physics)",
"Wang*fang (Chemistry)",
"Zhao*qiang (Biology)",
"...",
"Zhou*lin (Economics)"
];
staffMembers.forEach(member => {
outputContent += `${member}\nVery Satisfied\nSatisfied\nNeutral\nDissatisfied\n\n${member} - Dissatisfaction Reasons [Multi-select]\nA. Discrimination or inappropriate punishment;\nB. Paid tutoring activities;\nC. External institution involvement;\nD. Soliciting gifts;\nE. Poor teaching performance;\nF. Classroom management issues;\nG. Unprofessional parent interactions;\nH. Accepting monetary gifts;\nI. Other concerns.\n\n`;
});
const popup = window.open();
popup.document.open();
popup.document.write(`${outputContent}`);
popup.document.close();
Triggering File Download
For longer content, triggering a file download provides a better user experience. This method creates an invisible anchor element with a data URI containing the text content.
function triggerTextDownload(content, filename = 'output.txt') {
const link = document.createElement('a');
const dataUri = 'data:text/plain;charset=utf-8,' + encodeURIComponent(content);
link.setAttribute('href', dataUri);
link.setAttribute('download', filename);
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
const generatedText = "Generated content string...";
triggerTextDownload(generatedText, 'survey_results.txt');
Combined Display and Download Soltuion
A comprehensive approach renders the content in a new window while simultaneously triggering a file download, providing users with both viewing and saving opsions.
function displayAndDownload(content, downloadName = 'document.txt') {
// Display in new window
const viewer = window.open();
viewer.document.open();
viewer.document.write(`${content}`);
viewer.document.close();
// Trigger download
const downloadLink = document.createElement('a');
downloadLink.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(content));
downloadLink.setAttribute('download', downloadName);
downloadLink.style.display = 'none';
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
}
// Usage
let surveyOutput = "";
const respondents = [
"Chen*ming (Mathematics)",
"Li*wei (Physics)",
"Wang*fang (Chemistry)",
"Zhao*qiang (Biology)",
"...",
"Zhou*lin (Economics)"
];
respondents.forEach(person => {
surveyOutput += `${person}\nVery Satisfied\nSatisfied\nNeutral\nDissatisfied\n\n`;
});
displayAndDownload(surveyOutput, 'teacher_survey.txt');
Note: Browser popup blockers may intercept the new window. Users should allow popups for the page, or consider alternative approaches such as rendering directly within the current page using textareas for input configuration and a preview area for output display.