Assignment Brief
Design and implement a Question Paper Generator application
The application must store a number of questions in a Question Store. A question can have the following attributes {question, subject, topic, difficulty, marks}
The Question paper Generator should look for questions in the Question Store and then generate the question paper based on the total marks and the distribution of marks based on Difficulty
Example
Assume the below requirement for a question paper:
(100 marks, Difficulty, {20% Easy, 50% Medium, 30% Hard })
The problem statement here is that you need to generate a question paper of 100 marks total of which 20% (ie, 20 marks) worth of questions should have the Difficulty=Easy, 50% having Difficulty=Medium and 30% having Difficulty=Hard
Expectations
To design and implement the Question Paper Generator application in Node.js, you can use the following structure and steps.
Step 1: Setting up the Environment
Step 2: Initializing the Project
npm init -y
to initialize a new Node.js project.Step 3: Installing Required Packages
npm install express mongoose
Step 4: Creating Question Schema and Model
models
) to store the schema and models for questions using MongoDB and Mongoose.// models/question.js const mongoose = require('mongoose'); const Schema = mongoose.Schema; const questionSchema = new Schema({ question: { type: String, required: true }, subject: { type: String, required: true }, topic: { type: String, required: true }, difficulty: { type: String, required: true }, marks: { type: Number, required: true // You may add validation rules as needed } }); const Question = mongoose.model('Question', questionSchema); module.exports = Question;
Step 5: Setting up Express Server
app.js
) to set up an Express server and handle routes for the Question Paper Generator.// app.js const express = require('express'); const mongoose = require('mongoose'); const Question = require('./models/question'); // Import the Question model const app = express(); const PORT = process.env.PORT || 3000; // Set the port as needed // Connect to MongoDB using Mongoose mongoose.connect('mongodb://localhost:27017/questionStore', { useNewUrlParser: true, useUnifiedTopology: true }) .then(() => { console.log('Connected to MongoDB'); }) .catch((err) => { console.error('Failed to connect to MongoDB:', err); }); // Define routes and implement the logic for generating the question paper // Start the server app.listen(PORT, () => { console.log(`Server running on port ${PORT}`); });
Step 6: Generating Question Paper
Step 7: Additional Requirements
Step 8: Running the Application
node app.js
This setup provides a basic foundation for building the Question Paper Generator application in Node.js using Express, MongoDB, and Mongoose. It can be extended to accommodate future requirements and handle edge cases effectively.
Generating Question Paper as a above description in express and nodejs
Let's design a simple solution for a Question Paper Generator application in Express and Node.js. Note that due to the complexity and scale of the project, the solution is a basic version.
Let's approach it in different steps:
Step 1: Create and set up a new Node.js project If Node.js and npm are not installed, you need to install them first.
Create a new directory, initialize a new Node.js project and install necessary packages:
mkdir question-generator cd question-generator npm init -y npm install express mongoose
Step 2: Connect to the MongoDB database using Mongoose
Create a file database.js
with the following code.
const mongoose = require('mongoose'); const connectDB = async () => { try { await mongoose.connect('mongodb://127.0.0.1:27017/question-generator', { useNewUrlParser: true, useUnifiedTopology: true }); console.log('MongoDB connected...'); } catch(err) { console.error(err.message); process.exit(1); } } module.exports = connectDB``` **Step 3: Define the Model** Create a new file `models/Question.js` and define the Question schema as per the application's requirements. ```javascript const mongoose = require('mongoose'); const QuestionSchema = new mongoose.Schema({ question: { : String, required: true }, subject: { type: String, required: true topic: { type: String, required: true }, difficulty: { type: String, required: true }, marks: { type: Number, required: true } }); module.exports = Question = mongoose.model('question', QuestionSchema); ` **Step 4: Define Routes and Logic** In the `app.js` file, define the logic and the endpoint to generate the question paper. ```javascript const express = require('express'); const connectDB = require('./database'); const Question = require('./models/Question'); const app = express(); connectDB(); app.get('/generate-paper', async (req, res) => { try { let paper = []; let totalMarks = 0; const difficultyLevels = ['Easy', 'Medium', 'Hard']; for(let i = 0; i < difficultyLevels.length; i++) { let questions = await Question.find({ difficulty: difficultyLevels[i] }); questions.forEach(question => { if(totalMarks + question.marks <= 100) { totalMarks += question.marks; paper.push(question); } }); if (totalMarks >= 100) break; } res.json(paper); catch(err) { console.error(err.message); res.status(500).send('Server Error'); } });
This code fetches the questions based on the difficulty level and constructs the question paper until the total marks reach 100.
Step 5: Start the server
The last step is to the server.
const PORT = process.env.PORT || 5000; app.listen(PORT, () => console.log(`Server started on port ${PORT}`));
Run the application using the command node app.js
.
This approach is a simple starter to a question paper generator. This solution does not include the distribution of marks based on the difficulty level but gives a foundation to expand upon. It can be modified to include more complex functionalities.
To handle the edge cases, you can program more conditions in the code to accommodate situations such as when the total marks exceed 100.
en.wikipedia.org
infosys.com
developer.mozilla.org