Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Client Script Forum > ช่วยดูโค้ดสุ่มข้อสอบและคำตอบมาแสดงหน่อยครับ เอามาจากเว็บไซต์ แล้วแก้ไม่ถูกครับ



 

ช่วยดูโค้ดสุ่มข้อสอบและคำตอบมาแสดงหน่อยครับ เอามาจากเว็บไซต์ แล้วแก้ไม่ถูกครับ

 



Topic : 086816



โพสกระทู้ ( 0 )
บทความ ( 0 )



สถานะออฟไลน์




เอามาจากเว็บไซต์ครับ เป็นโค้ดสุ่มคำถามและข้อสอบมาแสดง แต่ผมไม่เข้าใจการทำงานของมันว่ามันกำหนดคำตอบยังไง รู้ได้ไงว่าข้อ 1 ตอบอะไร เพราะเท่าที่ดูจากโค้ด มีคำถาม 20 ข้อ มีตัวเลือกเยอะ ซึ่งจะสุ่มมา แล้วคำตอบมี 20 ข้อ ซึ่งเขาบอกว่าคำตอบผิดอยู่ ต้องกำหนดใหม่ แต่ผมไม่รู้จะกำหนดยัง และอีกอย่าง พอมันสุ่มคำตอบมา มันไม่ดึงข้อมูลที่ตรงกับคำถามมาแสดง ต้องทำยังไงครับ ช่วยอธิบายแนวทาง หรืออธิบายโค้ดก็ได้ครับ ขอบคุณครับ

Code (JavaScript)
<html><head>
<script type="text/JavaScript">
function Questions(divname) {
   this.QText   = new Array(20); // The questions
   this.QAnswer = new Array(20); // The correct answers
   this.QChoice = new Array(80); // The possible multi-choice values
   this.thisAns = new Array(10); // The answers for this page
   this.Answer  = new Array(10); // The user's answers
   this.anchor = document.getElementById(divname);

   // OK, set the questions
   this.QText[0] = "What is the capital of Norway";
   this.QText[1] = "What is the capital of Hungary";
   this.QText[2] = "What is the capital of Cyprus";
   this.QText[3] = "What is the capital of Libya";
   this.QText[4] = "What is the capital of Kenya";
   this.QText[5] = "What is the capital of Nigeria";
   this.QText[6] = "What is the capital of Columbia";
   this.QText[7] = "What is the capital of Barbados";
   this.QText[8] = "What is the capital of Australia";
   this.QText[9] = "What is the capital of Pakistan";
   this.QText[10] = "What is the capital of Spain";
   this.QText[11] = "What is the capital of England";
   this.QText[12] = "What is the capital of Ireland";
   this.QText[13] = "What is the capital of Sweden";
   this.QText[14] = "What is the capital of Denmark";
   this.QText[15] = "What is the capital of Poland";
   this.QText[16] = "What is the capital of Italy";
   this.QText[17] = "What is the capital of India";
   this.QText[18] = "What is the capital of Canada";
   this.QText[19] = "What is the capital of Portugal";

   // Now, set the possible choices
   this.QChoice[0] = "Bergen";
   this.QChoice[1] = "Trondheim";
   this.QChoice[2] = "Oslo";
   this.QChoice[3] = "Haugesund";
   this.QChoice[4] = "Warsaw";
   this.QChoice[5] = "Budapest";
   this.QChoice[6] = "Bucharest";
   this.QChoice[7] = "Vienna";
   this.QChoice[8] = "Crete";
   this.QChoice[9] = "Nicosia";
   this.QChoice[10] = "Athens";
   this.QChoice[11] = "Saloniki";
   this.QChoice[12] = "Tehran";
   this.QChoice[13] = "Cairo";
   this.QChoice[14] = "Tripoli";
   this.QChoice[15] = "Baghdad";
   this.QChoice[16] = "Nairobi";
   this.QChoice[17] = "Mombasa";
   this.QChoice[18] = "Zanzibar";
   this.QChoice[19] = "Kampala";
   this.QChoice[20] = "Laos";
   this.QChoice[21] = "Harare";
   this.QChoice[22] = "Douala";
   this.QChoice[23] = "Lagos";
   this.QChoice[24] = "Bogota";
   this.QChoice[25] = "Cali";
   this.QChoice[26] = "Medellin";
   this.QChoice[27] = "Panama";
   this.QChoice[28] = "Havana";
   this.QChoice[29] = "Bridgetown";
   this.QChoice[30] = "Kingston";
   this.QChoice[31] = "Montego Bay";
   this.QChoice[32] = "Sydney";
   this.QChoice[33] = "Brisbane";
   this.QChoice[34] = "Melbourne";
   this.QChoice[35] = "Canberra";
   this.QChoice[36] = "Kabul";
   this.QChoice[37] = "Karachi";
   this.QChoice[38] = "Islamabad";
   this.QChoice[39] = "New Delhi";
   this.QChoice[40] = "Bergen";
   this.QChoice[41] = "Madrid";
   this.QChoice[42] = "Oslo";
   this.QChoice[43] = "Haugesund";
   this.QChoice[44] = "London";
   this.QChoice[45] = "Budapest";
   this.QChoice[46] = "Bucharest";
   this.QChoice[47] = "Vienna";
   this.QChoice[48] = "Crete";
   this.QChoice[49] = "Dublin";
   this.QChoice[50] = "Athens";
   this.QChoice[51] = "Saloniki";
   this.QChoice[52] = "Tehran";
   this.QChoice[53] = "Stockholm";
   this.QChoice[54] = "Tripoli";
   this.QChoice[55] = "Baghdad";
   this.QChoice[56] = "Nairobi";
   this.QChoice[57] = "Mombasa";
   this.QChoice[58] = "Copenhagen";
   this.QChoice[59] = "Kampala";
   this.QChoice[60] = "Warsaw";
   this.QChoice[61] = "Harare";
   this.QChoice[62] = "Douala";
   this.QChoice[63] = "Lagos";
   this.QChoice[64] = "Bogota";
   this.QChoice[65] = "Cali";
   this.QChoice[66] = "Medellin";
   this.QChoice[67] = "Rome";
   this.QChoice[68] = "New Delhi";
   this.QChoice[69] = "Bridgetown";
   this.QChoice[70] = "Kingston";
   this.QChoice[71] = "Montego Bay";
   this.QChoice[72] = "Sydney";
   this.QChoice[73] = "Brisbane";
   this.QChoice[74] = "Ottawa";
   this.QChoice[75] = "Canberra";
   this.QChoice[76] = "Kabul";
   this.QChoice[77] = "Karachi";
   this.QChoice[78] = "Islamabad";
   this.QChoice[79] = "Lisbon";

   // Set the correct answers
   this.QAnswer[0] = 3;
   this.QAnswer[1] = 2;
   this.QAnswer[2] = 2;
   this.QAnswer[3] = 3;
   this.QAnswer[4] = 1;
   this.QAnswer[5] = 4;
   this.QAnswer[6] = 1;
   this.QAnswer[7] = 2;
   this.QAnswer[8] = 4;
   this.QAnswer[9] = 3;
   this.QAnswer[10] = 3; // these answers are wrong - check them yourself!
   this.QAnswer[11] = 2;
   this.QAnswer[12] = 2;
   this.QAnswer[13] = 3;
   this.QAnswer[14] = 1;
   this.QAnswer[15] = 4;
   this.QAnswer[16] = 1;
   this.QAnswer[17] = 2;
   this.QAnswer[18] = 4;
   this.QAnswer[19] = 3;

}

Questions.prototype.generateQuiz = function() {
   var QCount = 0;
   var QNumber = 0;

   // Initialise the "questions available" table
   var QUsed = new Array(20);
   for (QCount=0; QCount<20; QCount++) {
      QUsed[QCount] = 1; // Marked as available
   }

   // Delete any existing questions
   //this.nukeExistingQuiz();
   this.anchor.innerHTML = "";

   var HTMLBlob = "<table>";

   // Build up the questions
   for (QCount=0; QCount<10; ) {
      QNumber = Math.floor(20 * Math.random());
      if (1 == QUsed[QNumber]) { // Still available?
	 HTMLBlob += this.AddQuestion(QNumber, QCount);
	 QCount++;
         QUsed[QNumber] = 0; // Marked as unavailable
      }
   }
   HTMLBlob += "</table>";
   this.anchor.innerHTML = HTMLBlob;
}

// This removes any existing quiz on the page
Questions.prototype.nukeExistingQuiz = function() {
   if (null != this.anchor && null != this.anchor.childCount) {
      while (this.anchor.childCount > 0) {
         this.anchor.removeChild(this.anchor.childNodes[0]);
      }
   }
}

// Add this to the DOM
Questions.prototype.AddQuestion = function(QNum, EntryNum) {
   // This is really naughty: you should use DOM stuff, and not
   // non-standard innerHtml ...
   var Ix;
   var HTMLBlob = "<tr><td><input type=\"checkbox\" id=\"check" + EntryNum + "\" checked=\"checked\"></td><td>"
                       + "Question #" + QNum + ":</td><td>"
                       + "<strong>" + this.QText[QNum] + "</strong></td><td><select id=\"answer" + EntryNum + "\" size=\"1\">"
                       + "<option selected=\"selected\" value=\"0\">-- Select an answer --</option>"
   for (Ix=0; Ix<4; Ix++) {
      HTMLBlob = HTMLBlob + "<option value=\"" + Ix + "\">" + this.QChoice[QNum*4+Ix] + "</option>";
   }
   HTMLBlob = HTMLBlob + "</select></td></tr>";
   
   this.thisAns[EntryNum] = this.QAnswer[QNum];
   return HTMLBlob;
}


// Verify that all questions have been answered
Questions.prototype.AreQuestionsAnswered = function() {
   var unanswered = 0;
   for (var Ix=0; Ix<10; Ix++) {
      this.Answer[Ix] = document.getElementById("answer" + Ix).selectedIndex;
      if (this.Answer[Ix] == 0) {
         unanswered++;
      }
   }
   return unanswered;
}

// Score the results
Questions.prototype.ScoreIt = function() {
   var count = this.AreQuestionsAnswered();
   if (count > 0) {
      alert("You didn't answer " + count + " questions. Please try again.");
      count = 0;
   } else {
      for (var Ix=0; Ix<10; Ix++) {
         if (this.Answer[Ix] == this.thisAns[Ix]) {
            document.getElementById("check" + Ix).checked = false;
            count++;
         } else {
            document.getElementById("check" + Ix).checked = true;
         }
      }
      alert("You got " + count + " correct.");
   }
   return count;
}
</script>
</head><body>

   <div id="quizblock"></div>

   <script type="text/JavaScript">
      var MyQuiz = new Questions("quizblock");
      MyQuiz.generateQuiz();
   </script>

   <input type="button" onclick="MyQuiz.ScoreIt();" value="Done">

</body></html>




Tag : - - - -







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2012-11-16 13:00:13 By : papelocho View : 1652 Reply : 2
 

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

ใช้ JavaScript แล้วจะเพิ่มข้อสอบยังไงครับ ในกระทู้เก่า ๆ ผมเคยทำ php เป็นตัวอย่างไว้ครับ






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-11-18 08:11:35 By : mr.win
 


 

No. 2



โพสกระทู้ ( 0 )
บทความ ( 0 )



สถานะออฟไลน์


ลองใช้แล้วครับ คือไม่ต้องการเพิ่มอะไรมากมายครับ แค่เอาข้อสอบ 10 ข้อ มาให้สุ่มเฉย ๆ ครับ รบกวนแล้วด้วยครับ

สำหรับ php ผมทดลองแล้ว แต่ติดปัญหาตรง มันอ่านไทยไม่ได้ เลยไม่เอาครับ รบกวนด้วยครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-11-19 11:09:57 By : papelocho
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : ช่วยดูโค้ดสุ่มข้อสอบและคำตอบมาแสดงหน่อยครับ เอามาจากเว็บไซต์ แล้วแก้ไม่ถูกครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 04
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่