generated from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
70 additions
and
25 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const question4AnswersSchema = new mongoose.Schema({ | ||
question: { | ||
type: String, | ||
required: true, | ||
}, | ||
correctAnswer: { | ||
type: String, | ||
required: true, | ||
}, | ||
incorrectAnswer1: { | ||
type: String, | ||
required: true, | ||
}, | ||
incorrectAnswer2: { | ||
type: String, | ||
required: true, | ||
}, | ||
incorrectAnswer3: { | ||
type: String, | ||
required: true, | ||
}, | ||
}); | ||
|
||
const Question4Answers = mongoose.model('Question4Answers', question4AnswersSchema); | ||
|
||
module.exports = Question4Answers; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,42 @@ | ||
// questionGenerator.js | ||
const Question4Answers = require('./Question4Answers'); | ||
|
||
function generateQuestionPopulation(cityPopulationMap) { | ||
const cityPopulationArray = Array.from(cityPopulationMap); | ||
|
||
const randomIndex = Math.floor(Math.random() * cityPopulationArray.length); | ||
const [city, population] = cityPopulationArray[randomIndex]; | ||
|
||
const incorrectAnswers = []; | ||
while (incorrectAnswers.length < 3) { | ||
const randomCity = cityPopulationArray[Math.floor(Math.random() * cityPopulationArray.length)]; | ||
const [randomCityName, randomCityPopulation] = randomCity; | ||
if (randomCityName !== city && !incorrectAnswers.includes(randomCityPopulation)) { | ||
incorrectAnswers.push(randomCityPopulation); | ||
} | ||
const cityPopulationArray = Array.from(cityPopulationMap); | ||
|
||
const randomIndex = Math.floor(Math.random() * cityPopulationArray.length); | ||
const [city, population] = cityPopulationArray[randomIndex]; | ||
|
||
const incorrectAnswers = []; | ||
while (incorrectAnswers.length < 3) { | ||
const randomCity = cityPopulationArray[Math.floor(Math.random() * cityPopulationArray.length)]; | ||
const [randomCityName, randomCityPopulation] = randomCity; | ||
if (randomCityName !== city && !incorrectAnswers.includes(randomCityPopulation)) { | ||
incorrectAnswers.push(randomCityPopulation); | ||
} | ||
|
||
const question = { | ||
question: `What is the population of ${city}?`, | ||
correctAnswer: population, | ||
incorrectAnswers, | ||
}; | ||
|
||
return question; | ||
} | ||
|
||
module.exports = { | ||
generateQuestionPopulation, | ||
|
||
// Create the question object | ||
const question = { | ||
question: `What is the population of ${city}?`, | ||
correctAnswer: population.toString(), | ||
incorrectAnswer1: incorrectAnswers[0].toString(), | ||
incorrectAnswer2: incorrectAnswers[1].toString(), | ||
incorrectAnswer3: incorrectAnswers[2].toString(), | ||
}; | ||
|
||
|
||
// Save the question to MongoDB | ||
const newQuestion = new Question4Answers(question); | ||
newQuestion.save() | ||
.then(savedQuestion => { | ||
console.log('Question saved to MongoDB:', savedQuestion); | ||
}) | ||
.catch(error => { | ||
console.error('Error saving question to MongoDB:', error.message); | ||
}); | ||
|
||
return question; | ||
} | ||
|
||
module.exports = { | ||
generateQuestionPopulation, | ||
}; |