diff --git a/public/advanced.json b/public/advanced.json new file mode 100644 index 0000000..10a13cf --- /dev/null +++ b/public/advanced.json @@ -0,0 +1,35 @@ +[ + { + "question": "What is the key difference between Python 2 and Python 3?", + "options": [ + "Python 2 is no longer supported by the Python Software Foundation.", + "Python 3 has a different syntax and introduces new features.", + "Python 2 is faster than Python 3.", + "Python 3 is not backward compatible with Python 2." + ], + "correct_answer": 1, + "explanation": "Python 2 and Python 3 have significant differences in syntax and features. Python 3 was introduced to address some design flaws in Python 2 and to improve the language overall." + }, + { + "question": "What is the key difference between Python 2 and Python 3?", + "options": [ + "Python 2 is no longer supported by the Python Software Foundation.", + "Python 3 has a different syntax and introduces new features.", + "Python 2 is faster than Python 3.", + "Python 3 is not backward compatible with Python 2." + ], + "correct_answer": 1, + "explanation": "Python 2 and Python 3 have significant differences in syntax and features. Python 3 was introduced to address some design flaws in Python 2 and to improve the language overall." + }, + { + "question": "What is the key difference between Python 2 and Python 3?", + "options": [ + "Python 2 is no longer supported by the Python Software Foundation.", + "Python 3 has a different syntax and introduces new features.", + "Python 2 is faster than Python 3.", + "Python 3 is not backward compatible with Python 2." + ], + "correct_answer": 1, + "explanation": "Python 2 and Python 3 have significant differences in syntax and features. Python 3 was introduced to address some design flaws in Python 2 and to improve the language overall." + } +] \ No newline at end of file diff --git a/public/expert.json b/public/expert.json new file mode 100644 index 0000000..70f7079 --- /dev/null +++ b/public/expert.json @@ -0,0 +1,30 @@ + +[ +{ + "question": "True or False: In Python, '==' and 'is' operators are used interchangeably to compare two objects.", + "options": [ + "True", + "False" + ], + "correct_answer": 1, + "explanation": "False. In Python, '==' is used to compare the values of two objects, while 'is' is used to check if two objects refer to the same memory location." +}, +{ + "question": "True or False: In Python, '==' and 'is' operators are used interchangeably to compare two objects.", + "options": [ + "True", + "False" + ], + "correct_answer": 1, + "explanation": "False. In Python, '==' is used to compare the values of two objects, while 'is' is used to check if two objects refer to the same memory location." +}, +{ + "question": "True or False: In Python, '==' and 'is' operators are used interchangeably to compare two objects.", + "options": [ + "True", + "False" + ], + "correct_answer": 1, + "explanation": "False. In Python, '==' is used to compare the values of two objects, while 'is' is used to check if two objects refer to the same memory location." +} +] \ No newline at end of file diff --git a/src/advanced.json b/src/advanced.json deleted file mode 100644 index e69de29..0000000 diff --git a/src/components/Difficulty.tsx b/src/components/Difficulty.tsx new file mode 100644 index 0000000..bc5141f --- /dev/null +++ b/src/components/Difficulty.tsx @@ -0,0 +1,27 @@ +// eslint-disable-next-line @typescript-eslint/no-unused-vars +import React, { useState } from 'react'; + +interface DifficultyProps { + onSelectDifficulty: (selectedDifficulty: string) => void; +} + +const Difficulty: React.FC = ({ onSelectDifficulty }) => { + const difficulties = ['Beginner', 'Advanced', 'Expert']; + + return ( +
+

Choose a difficulty level please:

+ +
+ ); +}; + +export default Difficulty; diff --git a/src/components/Language.tsx b/src/components/Language.tsx new file mode 100644 index 0000000..0ab8298 --- /dev/null +++ b/src/components/Language.tsx @@ -0,0 +1,35 @@ +// eslint-disable-next-line @typescript-eslint/no-unused-vars +import React, { useState } from 'react'; + +interface LanguageProps { + onSelectLanguage: (selectedLanguage: string) => void; +} + +const Language: React.FC = ({ onSelectLanguage }) => { + const languages = [ + 'Javascript', + 'Python', + 'PHP', + 'React', + 'Angular', + 'Vue', + 'Tailwind', + ]; + + return ( +
+

Choose a programming language to test yourself please:

+
    + {languages.map((language, index) => ( +
  • + +
  • + ))} +
+
+ ); +}; + +export default Language; diff --git a/src/components/Questions.tsx b/src/components/Questions.tsx new file mode 100644 index 0000000..fb8589f --- /dev/null +++ b/src/components/Questions.tsx @@ -0,0 +1,122 @@ +import React, { useState, useEffect } from 'react'; + +interface QuestionsProps { + apiUrl: string; // get data from backend +} + +interface Question { + question: string; + options: string[]; + correct_answer: number; + explanation: string; +} + +const Questions: React.FC = ({ apiUrl }) => { + const [questions, setQuestions] = useState([]); + const [currentQuestionIndex, setCurrentQuestionIndex] = useState(0); + const [userAnswers, setUserAnswers] = useState([]); + const [isTimeUp, setIsTimeUp] = useState(false); + const [isAnswered, setIsAnswered] = useState(false); + const [timer, setTimer] = useState(0); + + useEffect(() => { + fetch(apiUrl) + .then((response) => response.json()) + .then((data) => { + setQuestions(data); + // Time, depending on questions type + const questionType = + data[0].options.length > 2 ? 'multiple' : 'trueFalse'; + setTimer(questionType === 'multiple' ? 90 : 60); + }) + .catch((error) => { + console.error('can not communicate with the api:', error); + }); + }, [apiUrl]); + + useEffect(() => { + let interval: NodeJS.Timeout; + + if (timer > 0 && currentQuestionIndex < questions.length && !isTimeUp) { + interval = setInterval(() => { + if (timer > 0) { + setTimer(timer - 1); + } else { + clearInterval(interval); + setIsTimeUp(true); + // when time is up, next q + goToNextQuestion(); + } + }, 1000); + } + return () => { + clearInterval(interval); + }; + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [timer, currentQuestionIndex, questions, isTimeUp]); + const handleAnswerQuestion = (selectedOption: number) => { + const correctAnswer = questions[currentQuestionIndex].correct_answer; + setUserAnswers([...userAnswers, selectedOption === correctAnswer ? 1 : 0]); + setIsAnswered(true); + }; + + const goToNextQuestion = () => { + if (currentQuestionIndex < questions.length - 1) { + setCurrentQuestionIndex(currentQuestionIndex + 1); + setIsAnswered(false); + } else { + // Show result + } + }; + + return ( +
+ {timer > 0 && !isTimeUp &&

Timer: {timer} second

} + + {questions.length > 0 && currentQuestionIndex < questions.length ? ( +
+

Question {currentQuestionIndex + 1}

+

{questions[currentQuestionIndex].question}

+
    + {questions[currentQuestionIndex].options.map((option, index) => ( +
  • + +
  • + ))} +
+ {isAnswered && ( +
+

+ Correct:{' '} + { + questions[currentQuestionIndex].options[ + questions[currentQuestionIndex].correct_answer + ] + } +

+ +
+ )} +
+ ) : ( +
+

You have have answred all question

+

+ Correct answers:{' '} + {userAnswers.filter((answer) => answer === 1).length} +

+

+ Wrong answers: {userAnswers.filter((answer) => answer === 0).length} +

+
+ )} +
+ ); +}; + +export default Questions; diff --git a/src/beginner.json b/src/data/beginner.json similarity index 100% rename from src/beginner.json rename to src/data/beginner.json diff --git a/src/programmingLanguages.json b/src/data/programmingLanguages.json similarity index 100% rename from src/programmingLanguages.json rename to src/data/programmingLanguages.json diff --git a/src/expert.json b/src/expert.json deleted file mode 100644 index e69de29..0000000