Advertisement
Guest User

Untitled

a guest
May 24th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.74 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width,initial-scale=1">
  6. <title>JS Bin</title>
  7.  
  8. <style>
  9. html{
  10. background:#333;
  11. background: url(https://78.media.tumblr.com/6cb0ae44278156aa660d95d55df340de/tumblr_nz14o7t0Z61skcd7fo1_500.gif);
  12. background-attachment: fixed;
  13. background-size: 100% 100%;
  14. color:antiquewhite;
  15. font-family:Arial;
  16. text-align: center;
  17. margin:0 auto;
  18. max-width:500px;
  19. }
  20. .flex{
  21.  
  22. }
  23.  
  24.  
  25.  
  26. li{
  27. display: flex;
  28. }
  29. button{
  30. margin:20px;
  31. padding:15px 40px;
  32. text-transform: uppercase;
  33. background:0;
  34. border:0;
  35. border-radius:10px;
  36. background:#4DD0E1;
  37. transition:0.3s;
  38. color:white;
  39. font-weight: bold;
  40. box-shadow: 0px 0px 25px 3px rgba(0,0,0,0.75);
  41. font-size:1.5rem
  42. }
  43. button:hover{
  44. background: #80DEEA;
  45. }
  46. button:focus{
  47. background:#00BCD4;
  48.  
  49. }
  50. button:active{
  51. transform:translate(0,5px);
  52. }
  53.  
  54. .podp{
  55. padding:5px;
  56. background:rgba(35, 35, 35, 0.82);
  57. margin-bottom:20px;
  58. display: flex;
  59. justify-content: center
  60. }
  61. .podp img{
  62. padding:5px;
  63. height:100px;
  64. width:100px;
  65.  
  66. }
  67. .enemy{
  68. transform: rotateY(180deg);
  69. }
  70. </style>
  71. </head>
  72. <body>
  73. <p id="wynik">Kliknij przycisk aby rozpocząć grę!</p>
  74. <p class="podp">Wygrane: <strong id="wygrane">0</strong> / Przegrane: <strong id="przegrane">0</strong></p>
  75. <div id="quiz">
  76.  
  77. </div>
  78. <p class="podp">
  79. <img src="http://3.bp.blogspot.com/-Q-haT3Z9XVI/Vpp6romxfqI/AAAAAAAAAoI/hSJQFVMw5d8/s1600/kamien_papier_nozyce.png" id="ob1">
  80. <img src="http://3.bp.blogspot.com/-Q-haT3Z9XVI/Vpp6romxfqI/AAAAAAAAAoI/hSJQFVMw5d8/s1600/kamien_papier_nozyce.png" class="enemy" id="ob2">
  81. </p>
  82.  
  83. <div class="flex">
  84. <button id="kamien">Wybierz kamień</button>
  85. <button id="papier">Wybierz papier</button>
  86. <button id="nozyce">Wybierz nożyce</button>
  87. </div>
  88.  
  89.  
  90. <script>
  91.  
  92. function shuffleArray(array) {
  93. for (let i = array.length - 1; i > 0; i--) {
  94. const j = Math.floor(Math.random() * (i + 1));
  95. [array[i], array[j]] = [array[j], array[i]];
  96. }
  97. }
  98.  
  99. class Quiz{
  100. constructor(element,questions){
  101. this.container = element;
  102. this.questions = questions;
  103. this.documentFragment = document.createDocumentFragment();
  104.  
  105. this.questionsToNodes();
  106. this.container.appendChild(this.documentFragment);
  107. }
  108. calculateCorrectAnswers(){
  109. console.log(this.container)
  110.  
  111. /*
  112.  
  113. if(xd === correctAnswer)
  114. */
  115.  
  116. }
  117. questionsToNodes(){
  118. this.questions.forEach(question=>{
  119. const title = document.createElement("h2");
  120. title.textContent = question.question;
  121.  
  122. this.documentFragment.appendChild(title);
  123.  
  124. const correctAnswer = question.answers[question.correctAnswer];
  125.  
  126. shuffleArray(question.answers);
  127.  
  128. const list = document.createElement("ul");
  129.  
  130. question.answers.forEach(answer=>{
  131. const li = document.createElement("li");
  132. const input = document.createElement("input");
  133. const p = document.createElement("p");
  134. input.name = question.question;
  135. input.type = "radio";
  136.  
  137. li.appendChild(input);
  138. li.appendChild(p);
  139. p.textContent = answer;
  140. list.appendChild(li);
  141. });
  142.  
  143. this.documentFragment.appendChild(list);
  144. });
  145.  
  146. const btn = document.createElement("button");
  147. const p = document.createElement("p");
  148. this.calculateCorrectAnswers()
  149. btn.addEventListener("click",()=>{
  150. p.textContent = ``
  151. });
  152. }
  153. }
  154.  
  155. const questions = [{
  156. question:"Jak prawidłowo zmienić zawartość elementu HTML?",
  157. answers:[`document.getElementByName("p").innerHTML = "Hello World!";`,
  158. `document.getElementById("demo").innerHTML = "Hello World!";`,
  159. `document.getElement("p").innerHTML = "Hello World!";`,
  160. `#demo.innerHTML = "Hello World!";`],
  161. correctAnswer:1
  162.  
  163. },
  164. {
  165. question:"W jaki element HTML wkładamy skrypty JavaScript?",
  166. answers:[`<javascript>`,`<script>`,`<js>`,`<scripting>`],
  167. correctAnswer:1
  168.  
  169. },
  170. {
  171. question:"Jaka jest prawidłowa składnia podłączenia zewnętrznego pliku .js do tagu <script>?",
  172. answers:[`<script href="xxx.js">`,`<script src="xxx.js">`,`<script name="xxx.js">`],
  173. correctAnswer:1
  174.  
  175. },
  176. {
  177. question:"Czy zewnętrzny skrypt musi posiadać atrybut src?",
  178. answers:[`Tak`,`Nie`],
  179. correctAnswer:0
  180.  
  181. },
  182. {
  183. question:"Jak wyświetlić napis w oknie dialogowym?",
  184. answers:[`msgBox("Hello World");`,`alert("Hello World");`,`msg("Hello World");`,`alertBox("Hello World");`],
  185. correctAnswer:1
  186.  
  187. },
  188. {
  189. question:"Jaka jest prawidłowa składnia pętli while?",
  190. answers:[`while (i <= 10; i++)`,`while i = 1 to 10`,`while (i <= 10)`],
  191. correctAnswer:2
  192.  
  193. },
  194. {
  195. question:"Jak dodać komentarz w JavaScript?",
  196. answers:[`'This is a comment`,`//This is a comment`,`<!--This is a comment-->`],
  197. correctAnswer:1
  198.  
  199. },
  200. {
  201. question:"Jak prawidłowo napisać tablicę w języku JavaScript?",
  202. answers:[`var colors = 1 = ("red"), 2 = ("green"), 3 = ("blue")`,`var colors = "red", "green", "blue"`,`var colors = (1:"red", 2:"green", 3:"blue")`,`var colors = ["red", "green", "blue"]`],
  203. correctAnswer:3
  204.  
  205. },
  206. {
  207. question:"Jak poprawnie otworzyć nowe okno?",
  208. answers:[`window.open("http://www.w3schools.com");`,`window.new("http://www.w3schools.com");`],
  209. correctAnswer:0
  210.  
  211. },
  212. {
  213. question:"Co zwróci Boolean(10 > 9) ?",
  214. answers:[`true`,`false`,`NaN`],
  215. correctAnswer:0
  216.  
  217. }];
  218.  
  219. const beka = new Quiz(document.getElementById("quiz"),questions);
  220.  
  221.  
  222.  
  223. const wynik = document.getElementById("wynik"),
  224. kamien = document.getElementById("kamien"),
  225. papier = document.getElementById("papier"),
  226. nozyce = document.getElementById("nozyce"),
  227. wygrane = document.getElementById("wygrane"),
  228. ob1 = document.getElementById("ob1"),
  229. ob2 = document.getElementById("ob2"),
  230. przegrane = document.getElementById("przegrane");
  231.  
  232. const mozliwosci = ["Kamień","Papier","Nożyce"];
  233.  
  234. const xd = [{
  235. wybor: mozliwosci[0],
  236. kontra: mozliwosci[2],
  237. img: "kamien1.png"
  238. },
  239. {
  240. wybor: mozliwosci[1],
  241. kontra: mozliwosci[0],
  242. img: "papier1.png"
  243. },
  244. {
  245. wybor: mozliwosci[2],
  246. kontra: mozliwosci[1],
  247. img:'noz1.png'
  248. }];
  249.  
  250. const losuj = ()=>{
  251. const numer = Math.floor(Math.random() * 3);
  252. return mozliwosci[numer];
  253. };
  254.  
  255.  
  256. const gra = (enemy,gracz)=>{
  257. xd.forEach((komputer)=>{
  258.  
  259. if(komputer.wybor === enemy ){
  260. ob1.src = komputer.img;
  261. console.log(gracz, ob2);
  262. ob2.src = gracz.img;
  263.  
  264. if(komputer.wybor === gracz.wybor){
  265. wynik.textContent += ". Remis!";
  266. }
  267. else if(komputer.kontra === gracz.wybor){
  268. wynik.textContent += ". Komputer wygrał.";
  269. przegrane.textContent = przegrane.textContent *1+1;
  270. }
  271. else{
  272. wynik.textContent += ". Wygrałeś!";
  273. wygrane.textContent = wygrane.textContent *1+1;
  274. }
  275. }
  276.  
  277. });
  278. };
  279.  
  280. kamien.onclick = ()=>{
  281. const enemy = losuj();
  282. const gracz = xd[0];
  283. wynik.textContent = "Komputer wylosował " + enemy + ", a ty masz " + gracz.wybor;
  284.  
  285. gra(enemy,gracz);
  286. };
  287.  
  288. papier.onclick = ()=>{
  289. const enemy = losuj();
  290. const gracz = xd[1];
  291. wynik.textContent = "Komputer wylosował " + enemy + ", a ty masz " + gracz.wybor;
  292.  
  293. gra(enemy,gracz);
  294. };
  295.  
  296. nozyce.onclick = ()=>{
  297. const enemy = losuj();
  298. const gracz = xd[2];
  299. wynik.textContent = "Komputer wylosował " + enemy + ", a ty masz " + gracz.wybor;
  300.  
  301. gra(enemy,gracz);
  302. };
  303.  
  304. </script>
  305.  
  306.  
  307. </body>
  308. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement