Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Verse Key in Question/Answer Format Response #22

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ When the `options.value` is set to 1, it means the answer is correct, while when
{
"data": [
{
"question": " قُلْ هُوَ ٱللَّهُ أَحَدٌ",
"question": {
"text": " قُلْ هُوَ ٱللَّهُ أَحَدٌ",
"verseKey": "112:1"
},
"options": [
{
"text": "Al-Masad",
Expand Down
10 changes: 10 additions & 0 deletions src/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ export const getVerseTextById = (id: number) => {
return find.text_uthmani;
};

export const getVerseKeyById = (id: number) => {
const find = getQuran().find((item) => item.id === id);

if (!find) {
throw new Error("Failed to find verse");
}

return find.verse_key;
}

const chapterNames = [
"Al-Fatihah",
"Al-Baqarah",
Expand Down
11 changes: 9 additions & 2 deletions src/quiz/guessSurah.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
getManyVerseIdBySurahId,
getSurahBySurahId,
getSurahByVerseId,
getVerseKeyById,
getVerseTextById,
} from "~/data";
import { randomizeOptions } from "~/quiz/helpers";
Expand Down Expand Up @@ -48,7 +49,10 @@ const guessSurahBySurah = (props: GuessVerse) => {
options = options.sort(() => 0.5 - Math.random()).slice(0, 3);

return randomizeOptions({
question: getVerseTextById(questionVerseId),
question: {
text: getVerseTextById(questionVerseId),
verseKey: getVerseKeyById(questionVerseId),
},
options: [
...options.map((option) => {
return {
Expand Down Expand Up @@ -79,7 +83,10 @@ const guessSurahBySurah = (props: GuessVerse) => {
};

type CreateQuizOutput = {
question: string;
question: {
text: string;
verseKey: string;
};
options: {
text: string;
value: number;
Expand Down
8 changes: 7 additions & 1 deletion src/quiz/guessVerse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getManyVerseIdBySurahId,
getManyVerseIdByJuzId,
getVerseTextById,
getVerseKeyById,
} from "~/data";
import {
getIndexOfQuestionAnswerOptions,
Expand Down Expand Up @@ -126,17 +127,22 @@ const createGuessVerseQuiz = (props: CreateGuessVerseQuiz) => {
const { questionVerseId, answerVerseId, optionsVerseId } = props;

const result = {
question: getVerseTextById(questionVerseId),
question: {
text: getVerseTextById(questionVerseId),
verseKey: getVerseKeyById(questionVerseId),
},
options: [
...optionsVerseId.map((option) => {
return {
text: getVerseTextById(option),
value: 0,
verseKey: getVerseKeyById(option),
};
}),
{
text: getVerseTextById(answerVerseId),
value: 1,
verseKey: getVerseKeyById(answerVerseId),
},
],
};
Expand Down
4 changes: 2 additions & 2 deletions src/quiz/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ export const getIndexOfQuestionAnswerOptions = (
};

export const randomizeOptions = (result: {
question: string;
question: { text: string; verseKey: string };
options: { text: string; value: number }[];
}): {
question: string;
question: { text: string; verseKey: string };
options: { text: string; value: number }[];
} => {
const randomizedOptions = [...result.options];
Expand Down