Skip to content

Commit

Permalink
Fix right answer not appearing in alternatives
Browse files Browse the repository at this point in the history
  • Loading branch information
bennekrouf committed Jan 7, 2024
1 parent 19f966a commit f5b41d3
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/domain/exercise/generate_one_exercise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,22 @@ pub fn generate_one_exercise(dbs: &Database, exercise: &mut ExerciseOutput, exer
if let Some(ref mut valid_verse) = exercise.verses.get_mut(valid_verse_index) {
valid_verse.verse.sourate = Some(sourate_name_from_verse(dbs, &valid_verse.verse));
}

let exclude_verse = Some(exercise.verses[valid_verse_index].verse.clone());

let extracted_values = extract_and_shuffle_options(&mut exercise.verses, exercise_type, &exclude_verse);
let mut alternatives: Vec<Alternative> = extracted_values.into_iter().map(|value| {

// Ensure the correct answer is added first
let valid_verse = exercise.verses.get(valid_verse_index).unwrap();
let mut alternatives = vec![Alternative { verse: Some(valid_verse.verse.clone()) }];

// Add random alternatives
alternatives.extend(extracted_values.into_iter().map(|value| {
match exercise_type {
ExerciseType::FindDiscriminant => {
Alternative {
verse: Some(VerseOutput {
chapter_no: 0,
verse_no: 0,
sourate: None,
sourate: value.1.sourate,
ungrouped_text: Some(UngroupedText {
discriminant: Some(value.0),
pre: None,
Expand All @@ -43,11 +47,11 @@ pub fn generate_one_exercise(dbs: &Database, exercise: &mut ExerciseOutput, exer
},
_ => unimplemented!(),
}
}).take(2).collect();
}).filter(|alt| alt.verse != Some(valid_verse.verse.clone())));

// Add the correct answer
let valid_verse = exercise.verses.get(valid_verse_index).unwrap();
alternatives.push(Alternative { verse: Some(valid_verse.verse.clone()) });
// let valid_verse = exercise.verses.get(valid_verse_index).unwrap();
// alternatives.push(Alternative { verse: Some(valid_verse.verse.clone()) });
alternatives.shuffle(&mut rand::thread_rng());

// Return None if there are not enough alternatives
Expand Down

0 comments on commit f5b41d3

Please sign in to comment.