let difficulty_selection = $(".difficulty-selection"); let btn_previous_question = $("#btn-previous-question"); let btn_next_question = $("#btn-next-question"); let control_btns = $(".control-btn"); let current_difficulty = 1; difficulty_selection.on("click", change_difficulty); btn_next_question.on("click", next_question); btn_previous_question.on("click", previous_question); update_question(); function get_new_question(difficulty) { control_btns.attr("disabled", true); $.ajax(next_question_url, { method: "GET", cache: false, data: {difficulty: difficulty}, dataType: "json", success: (data, textStatus, jqXHR) => { control_btns.attr("disabled", false); question_received(data); }, }) } function change_difficulty(event) { difficulty_selection.removeClass("active"); let target = $(event.target); target.addClass("active"); if (target.prop("innerText") === "Easy") { current_difficulty = 1; } else if (target.prop("innerText") === "Medium") { current_difficulty = 2; } else { current_difficulty = 3; } update_question(); } function next_question() { let difficulty = questions[current_difficulty-1] let question_index = difficulty["current_question"]+1; if (difficulty["questions"].length === question_index) { get_new_question(current_difficulty); } else { difficulty["current_question"] += 1; update_question(); } } function previous_question() { questions[current_difficulty-1]["current_question"] -= 1; update_question(); } function question_received(data) { let difficulty = questions[current_difficulty-1]; if (data) { difficulty["questions"].push(data); difficulty["current_question"] += 1; update_question(); } else { difficulty["last_index"] = difficulty["current_question"]; update_question(); } }