Create an attendance register with nodejs


Read student names, line by line, from a .txt file.

For each name, ask if the student is in class.

E.g: Is Michael in class? [yes/no]

Let the user input either yes or no.

Store each name and entry in another .txt file.


I've written this almost working code just having problem looping through readlineSync module to prompt the names one after the other.

var fs = require("fs");
var text = fs.readFileSync("./test.txt");
var nameByLine = text.toString().split("\n");
var readlineSync = require("readline-sync");
module.exports = function rollCall() {
  let attendance = [];

  nameByLine.forEach(name => {
    let userResp = readlineSync.question(`Is ${name} in class: `);
    if (userResp.toLowerCase() == "yes") {
      present = `${name}:\t\t?}`;
    } else {
      absent = `${name}:\t\t?}`;

  return fs.writeFileSync("./newtest.txt", attendance.join("\n"));

Expected output: - Is name[1] in class: yes - Is name[2] in class: no

Result" - Is name[1] name[2] in class: yes

Before running the code

Before running the code

After running the code

After running the code



You are splitting the file using newline(\n) but the file is in CSV.

So there are two solutions:

1) Put the names in single line. The code will work as is.
2) Split the line using comma.



Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.