fsb-k,
<!doctype html> <head> <meta charset="utf-8" /> <title>Тест</title> <style> body{color:#000;background-color:#fff;font-family:Georgia,'Bookman Old Style',Verdana;font-size:14px;} #question{position:absolute;padding:30px;border:2px outset #fff;border-radius:10px;background-color:#d8e2cf;} #question span{padding-left:15px;padding-right:15px;} #question i{font-weight:bold;color:Crimson;} #q{position:absolute;right:-35px;top:-10px;padding:10px;border:1px solid #ccc;border-radius:5px;background-color:#eef3e3;} input[type="radio"],input[type="button"]{cursor:pointer;} input[type="button"]{border:none;background-color:transparent;font-weight:bold;letter-spacing:1px;} h2,h3{text-align:center;} </style> <script> var questions=[ /* ['вопросы|разделенные',время на ответ, баллы за ответ на этот вопрос, номер правильного ответа, текст вопроса] */ ['Пауэрлифтинг (жим лежа)|Процесс получения растительного масла|Женские половые сиски|Ничего из перечисленного',10,2,3,'Что подразумевает народная мудрость <i>Что посмеешь - то и пожмёшь</i>?'], ['Восемь|Шесть|Йа натурал',5,10,2,'Сколько будет <i>Два плюс два умножить на два</i>?'], ['То же, что и котангенс|Дорожный просвет|Да как вы смеете?!',10,100,2,'Что такое <i>клиренс</i>?'], ['Лавкрафт|Гитлер|Освальд|Перумов',10,1000,1,'<i>Пх\'нглуи мглв\'нафх Ктулху Р\'льех вгах\'нагл фхтагн</i>?'] ], ranks=['тупой дебил','медуза','сцуко умный'], stor={}, to; function $(id){return document.getElementById(id);} function fc(obj){ obj=(typeof obj!='string')?obj:$(obj); obj.style.position='absolute'; obj.style.left=(screen.width-obj.offsetWidth)/2+'px'; obj.style.top=(screen.height-obj.offsetHeight)/3+'px'; } function Question(n){ this.n=n; this.name='q'+(n+1); this.ttl='Вопрос '+(n+1); this.qq=questions[n][0]; this.tt=questions[n][1]; this.bb=questions[n][2]; this.nn=questions[n][3]; this.txt=questions[n][4]; this.show(); } Question.prototype={ show:function(){ if($('question')==null){document.body.appendChild(document.createElement('div')).setAttribute('id','question');} $('question').innerHTML='<center><div id="q">'+this.ttl+'</div><div id="timer">timer</div><h3>'+this.txt+'</h3></center>'; var arr=this.qq.split('|'),i=0; for(i; i < arr.length; i++){ var val=(i+1==this.nn)?'1':'0'; $('question').innerHTML+='<input type="radio" data-points="'+this.bb+'" name="'+this.name+'" value="'+val+'" /><span>'+arr[i]+'</span><br />'; } $('question').innerHTML+='<br /><center><input id="next" data-next="'+(this.n+1)+'" data-stor="'+this.name+'" type="button" value="Следующий вопрос" /></center>'; fc('question'); var inps=document.getElementsByName(this.name); for(var k=0; k < inps.length; k++){ inps[k].onclick=function(){ stor[this.name]=this.value*this.getAttribute('data-points'); } } $('next').onclick=function(){ var nn=this.getAttribute('data-stor'), ind=Number(this.getAttribute('data-next')); if(to!='undefined'){clearTimeout(to);} if(stor[nn]===undefined){stor[nn]=0;} if(typeof questions[ind]=='object'){new Question(ind);} else{ var x=0,right=0,wrong=0,points=0, w_ones='',r_ones='', output='<h2>Опрос окончен</h2>'; for(var z in stor){ stor[z]!=0 ? (right++) : (wrong++); stor[z]!=0 ? (r_ones+=z.substr(1,z.length-1)+',') : (w_ones+=z.substr(1,z.length-1)+','); points+=stor[z]; x++; } output+='Всего вопросов <i>'+x+'</i><br />Правильных ответов: <i>'+right+'</i> '+(right>0 ? '(№ '+r_ones.substr(0,r_ones.length-1)+')':'' )+' '+Math.round((right/x)*100)+'%<br />'; output+='Неправильных ответов: <i>'+wrong+'</i> '+(wrong>0 ? '(№ '+w_ones.substr(0,w_ones.length-1)+')':'' )+' '+Math.round((wrong/x)*100)+'%<br />'; output+='Баллов: <i>'+points+'</i><br />'; output+='По итогам опроса вы - <b>'; var res=right-wrong, ri=1; if(res>0){ri=2;} if(res<0){ri=0;} output+=ranks[ri]+'<b><br />'; output+='<br /><br /><center><input type="button" value="Закрыть" onclick="document.body.removeChild($(\'question\'));stor={}" /></center>'; $('question').innerHTML=output; } fc('question'); } var t=function(){ var T=arguments[0]; if(T > 0){ $('timer').innerHTML='Осталось <b>'+T+'</b> секунд'; to=setTimeout(function(){t(T-1);},1000); } else{$('next').click();} } t(this.tt); } }; onload=function(){ fc('btStart'); $('btStart').onclick=function(){new Question(0);} } </script> </head> <body> <input type="button" value="ОПРОС" id="btStart" /> </body> </html> |
Добрый день! Ковыряю JS на codacademy.com. Дошел до задания сделать игру "Камень, ножницы, бумага". Все этапы успешно прошел и сервер защитывает все мои ответы, но в интерпритаторе выдается результат
типа: Computer: paper undefined Мой код (все по инструкции вроде как там советуют): var userChoice = prompt("Do you choose rock, paper or scissors?"); var computerChoice = Math.random(); if (computerChoice < 0.34) { computerChoice = "rock"; } else if(computerChoice <= 0.67) { computerChoice = "paper"; } else { computerChoice = "scissors"; } console.log("Computer: " + computerChoice); function compare(choice1, choice2) { if (choice1 === choice2) { return "The result is a tie!"; } else if (choice1 === "rock") { if (choice2 === "scissors") { return "rock wins" } else { return "paper wins"; } } else if (choice1 === "paper") { if (choice2 ==="rock") { return "paper wins"; } else { return "scissors wins"; } } else if (choice1 === "scissors") { if (choice2 === "rock") { return "rock wins"; } else { return "scissors wins"; } } } console.log (compare (userChoice, computerChoice)); |
Часовой пояс GMT +3, время: 09:30. |