Create an attendance register with nodejs

Question:

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.

Solution:

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?}`;
      attendance.push(present);
    } else {
      absent = `${name}:\t\t?}`;
      attendance.push(absent);
    }
  });

  attendance;
  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

Answers:

Answer

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.

nameByLine.split(',').map((e)=>e.trim()).forEach(...)

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.