d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Help Me Write A Javascript Grade Checker Program > (500 Fg)
Add Reply New Topic New Poll
Member
Posts: 14,925
Joined: Jan 3 2008
Gold: 135,879.75
Mar 4 2019 08:57am
Given: input.txt −−> Output: output.txt

Input Text File:

Answer Key:
d /science /vocab
a /science /vocab
b /science /inference
d /science /bigpicture
c /history /inference
c /history /vocab
b /history /vocab
c /english /misc
b /english /bigpicture
b /english /misc

Student 1:
a
d
c
d
a
b
b
c
b
d

Output Text File:

Student 1:
Raw Score: 3/10

Score Breakdown by Subject:
science: 1/4
history: 1/3
english: 1/3

Score Breakdown by Question:
vocab: 1/4
Inference: 0/2
bigpicture: 1/2
misc: 1/2

Additional Specifications:
The code must work for any number of students. In this example, the students = 1. It should work for student = n.
The code must work for any number of questions. In this example, there were 10 questions.
The code must work for any given tag name. In this example, the tag names are "science" "history" "english" "vocab" "inference" "bigpicture" "misc."

This post was edited by kdog3682 on Mar 4 2019 08:57am
Member
Posts: 1,039
Joined: Jul 8 2008
Gold: 1,939.50
Mar 5 2019 11:48am
When do you need this by? Is this for school? Because if it is you should probably do your work if you intend to go into the field and program.
Member
Posts: 1,039
Joined: Jul 8 2008
Gold: 1,939.50
Mar 5 2019 12:20pm
Wrote this using node 8 to give myself a little refresher. I assume you're using node because you're using file in/out in JavaScript?

Code

var fs = require('fs');

fs.readFile('input.txt', 'utf8', function(err, data) {
if (err) throw err;
var student;
var readingAnswer = false;
var answers = [];
var students = new Map();
data.split('\r\n').forEach(line => {
if (line == '') {
return;
}
if (line.startsWith('Answer Key:')) {
readingAnswer = true;
return;
}
if (line.startsWith('Student')) {
readingAnswer = false;
student = line.replace('Student', '').replace(':' ,'').replace(' ', '');
return;
}
if (readingAnswer) {
answer = line.split(' /');
answers.push(answer);
return;
}
if (student) {
if (students.has(student)) {
students.set(student, students.get(student) + line);
} else {
students.set(student, line);
}
}
});

students.forEach((value, key, map) => {
var score = 0;
var subjects = new Map();
var questions = new Map();
value.split('').forEach((answer, index) => {
var correctAnswer = answers[index][0];
var subjectKey = answers[index][1];
var questionKey = answers[index][2];
var correct = answer == correctAnswer ? 1 : 0;
score += correct;

var subject = subjects.has(subjectKey) ? subjects.get(subjectKey) : { total : 0, correct : 0 };
subject.total++;
subject.correct += correct;

var question = questions.has(questionKey) ? questions.get(questionKey) : { total : 0, correct : 0 };
question.total++;
question.correct += correct;

questions.set(questionKey, question);
subjects.set(subjectKey, subject);
});

var contents = `Student ${key}:\n`;
contents += `Raw Score ${score}/${value.length}\n`;
contents += `\n`;
contents += `Score Breakdown by Subject:\n`;
subjects.forEach((value, key) => {
contents += `${key}: ${value.correct}/${value.total}\n`;
})
contents += `\n`;
contents += `Score Breakdown by Question:\n`;
questions.forEach((value, key) => {
contents += `${key}: ${value.correct}/${value.total}\n`;
})
contents += `\n`;

fs.appendFile('output.txt', contents);
});
});
Member
Posts: 14,925
Joined: Jan 3 2008
Gold: 135,879.75
Mar 5 2019 01:57pm
No, this isn't for school. I'm just trying to make grading easier for myself. Do I need to download node js in order for this code to work?
Member
Posts: 1,039
Joined: Jul 8 2008
Gold: 1,939.50
Mar 6 2019 07:56pm
Download node v.8.x and then copy this code into a file named grade.js with a file named input.txt beside it. Run the command
Code
node grade.js
and it will process the input.txt file.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll