Skip to content

Instantly share code, notes, and snippets.

@HananoshikaYomaru
Last active October 24, 2024 23:09
Show Gist options
  • Save HananoshikaYomaru/5bb12b590607e5cb8992a3a0e55732da to your computer and use it in GitHub Desktop.
Save HananoshikaYomaru/5bb12b590607e5cb8992a3a0e55732da to your computer and use it in GitHub Desktop.
addQuestionNumber.mjs
import fs from "fs-extra";
const filePath = "midterm.txt";
const fileContent = fs.readFileSync(filePath, "utf-8");
const lines = fileContent.split("\n");
let questionNumber = 1;
const updatedLines = lines.map((line) => {
const isQuestion = !(
(
line.trim() === "" ||
line.trim().startsWith("ANSWER:") ||
/^[A-Z]\.\s/.test(line.trim()) || // Matches A. B. C. ... Z.
/^[A-Z]\)\s/.test(line.trim())
) // Matches A) B) C) ... Z)
);
if (isQuestion) {
console.log(`Adding question number ${questionNumber}`);
line = `${questionNumber}. ${line.trim()}`;
questionNumber++;
}
return line;
});
const updatedContent = updatedLines.join("\n");
fs.writeFileSync(filePath, updatedContent, "utf-8");
console.log("Question numbers added successfully.");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment