Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions javascript/processingFunctions/processSnippetHtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ export const processSnippetHtml = (node, writeTo, split) => {
const jsPromptSnippet = node.getElementsByTagName("JAVASCRIPT_PROMPT")[0];

if (jsPromptSnippet) {
writeTo.push("<pre class='prettyprintoutput'>");
writeTo.push("<pre class='prettyprint'>");
writeTo.push(jsPromptSnippet.firstChild.nodeValue.trimRight());
writeTo.push("</pre>");
}

const jsLonelySnippet = node.getElementsByTagName("JAVASCRIPT_LONELY")[0];

if (jsLonelySnippet) {
writeTo.push("<pre class='prettyprintoutput'>");
writeTo.push("<pre class='prettyprint'>");
writeTo.push(jsLonelySnippet.firstChild.nodeValue.trimRight());
writeTo.push("</pre>");
}
Comment on lines 64 to 78
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While your change is functionally correct, there's an opportunity to refactor the code to reduce duplication. The logic for handling JAVASCRIPT_PROMPT and JAVASCRIPT_LONELY is identical. You can combine them into a loop to make the code more concise and maintainable.

Suggested change
const jsPromptSnippet = node.getElementsByTagName("JAVASCRIPT_PROMPT")[0];
if (jsPromptSnippet) {
writeTo.push("<pre class='prettyprintoutput'>");
writeTo.push("<pre class='prettyprint'>");
writeTo.push(jsPromptSnippet.firstChild.nodeValue.trimRight());
writeTo.push("</pre>");
}
const jsLonelySnippet = node.getElementsByTagName("JAVASCRIPT_LONELY")[0];
if (jsLonelySnippet) {
writeTo.push("<pre class='prettyprintoutput'>");
writeTo.push("<pre class='prettyprint'>");
writeTo.push(jsLonelySnippet.firstChild.nodeValue.trimRight());
writeTo.push("</pre>");
}
["JAVASCRIPT_PROMPT", "JAVASCRIPT_LONELY"].forEach(tagName => {
const snippet = node.getElementsByTagName(tagName)[0];
if (snippet) {
writeTo.push("<pre class='prettyprint'>");
writeTo.push(snippet.firstChild.nodeValue.trimRight());
writeTo.push("</pre>");
}
});

Expand Down