Last active
October 24, 2024 23:09
-
-
Save HananoshikaYomaru/5bb12b590607e5cb8992a3a0e55732da to your computer and use it in GitHub Desktop.
addQuestionNumber.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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