var Test = function (question) {
  var body = $('<div id="test"><div>').appendTo($("body"));
  var buttonDiv = $(
    '<center><div id="testBtn"></div></center><br><br><br><br><br><br>'
  ).appendTo($("body"));
  var button = $('<input type="button" value="Проверить" id="btn" />').appendTo(
    buttonDiv
  );
  var countShowQuestion = 10;

  var countAnswer = [];

  var showQuestion = function () {
    countAnswer = [];
    body.html("");
    for (el = 0; el < countShowQuestion; el++) {
      j = Math.random();
      countAnswer.push(0);
      body.append(
        "<div class='row-fluid'><div class='span8' id='q" +
          el +
          "'>" +
          (el + 1) +
          " " +
          question[el].question +
          "<br></div><div class='span4' id='r" +
          el +
          "'></div></div>"
      );
      $(
        '<input type="radio" value="' +
          el +
          '" name="q' +
          el +
          '"/> ' +
          question[el].answer1 +
          "<br />"
      )
        .appendTo("#q" + el)
        .click(function () {
          if (question[this.value].answer == 1) countAnswer[this.value] = 1;
          else countAnswer[this.value] = 0;
        });
      $(
        '<input type="radio" value="' +
          el +
          '" name="q' +
          el +
          '"/> ' +
          question[el].answer2 +
          "<br />"
      )
        .appendTo("#q" + el)
        .click(function () {
          if (question[this.value].answer == 2) countAnswer[this.value] = 1;
          else countAnswer[this.value] = 0;
        });
      $(
        '<input type="radio" value="' +
          el +
          '" name="q' +
          el +
          '"/> ' +
          question[el].answer3 +
          "<br />"
      )
        .appendTo("#q" + el)
        .click(function () {
          if (question[this.value].answer == 3) countAnswer[this.value] = 1;
          else countAnswer[this.value] = 0;
        });
       
    }
  };

  showQuestion();

  button.click(function () {
    $("#btn").css("display", "none"); //Скрыть кнопку после нажатия
    var count = 0;
    for (i in countAnswer) {
      if (countAnswer[i] == 1) {
        count++;
        $("#r" + i).html("Верно!");
        $("#r" + i).css("text-align", "center");
        $("#r" + i).css("background-color", "#00bc0fa6");
      } else {
        $("#r" + i).html("Неверно!");
        $("#r" + i).css("background-color", "#f9000082");
        $("#r" + i).css("text-align", "center");
      }
    }
    var result = (count / countAnswer.length) * 100;
    if (result < 50) alert("Вы не готовы. Повторите пройденный материал.");

    if (result >= 50 && result < 69) alert("Удовлетворительно.");
    if (result >= 70 && result < 89) alert("Хорошо.");
    if (result >= 90) alert("Отлично!");
  });
};
