Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//<pre>// Script to pose study questions on a given topic//// To use this script, add "importScript('User:Proteins/studyquestions.js');" to your monobook.js subpage // under your user page, as you can see at User:Proteins/monobook.js//******************// The Main Function//******************functionposeStudyQuestions(){varalert_string="";varbackup_string="";varsummary_string="";varquestion_string="";varallow_questions_to_be_repeated=true;varbody_content;varspan_nodes;vartemp_span_node;varnum_span_nodes=0;varspan_node_index=0;varrandom_index=0;varmax_random_index=0;varquestion_index=0;varquestion_counter=0;varstudy_question_index=0;varnum_study_questions=0;varnum_questions_posed=0;varnum_questions_desired=0;varnum_questions_selected=0;varnum_questions_remaining=0;varnum_answers=0;varanswer_index=0;varanswer_counter=0;varnum_answers_selected=0;varnum_answers_remaining=0;varnum_answers_printed_with_question=5;varnum_correct_answers=0;varnum_incorrect_answers=0;varpercentage_of_correct_answers=0;vartemp_study_question;varstudy_question_text="";varanswer_list=newArray();varstudy_question_list=newArray();varstudy_question_has_been_posed=newArray();varselected_study_question_indices=newArray();vartemp_answer;varcorrect_answer_node;varuser_answer_index=0;varprinted_answer_index=0;varcorrect_answer_index=0;varcorrect_printed_answer_index=0;varselected_answer_indices=newArray();varanswer_has_been_chosen_already=newArray();// Get the bodyContent nodebody_content=document.getElementById('bodyContent');if(!body_content){error_string="ERROR: There is no bodyContent node in this article.";window.alert(error_string);return;}span_nodes=body_content.getElementsByTagName("SPAN");if(!span_nodes){error_string="ERROR: This page has no SPAN nodes.\n";window.alert(error_string);return;}num_span_nodes=span_nodes.length;if(num_span_nodes<1){error_string="ERROR: This page has zero SPAN nodes.\n";window.alert(error_string);return;}//**********************************************// Count the number of study questions on a page//**********************************************for(span_node_index=0;span_node_index<num_span_nodes;span_node_index++){temp_span_node=span_nodes[span_node_index];if(!temp_span_node){continue;}if(temp_span_node.className.match(/study_question/)){num_study_questions++;study_question_list.push(temp_span_node);}}// closes loop over the SPAN nodes in the main articleif(num_study_questions<1){error_string="No study questions were found on this page.";window.alert(error_string);return;}elseif(num_study_questions==1){alert_string="This page has one type of study question.\n\n";backup_string="This page has only one type of study question.\n\n";// window.alert(alert_string);}else{alert_string="This page has "+num_study_questions+" types of study questions.\n\n";backup_string="This page has only "+num_study_questions+" types of study questions.\n\n";// window.alert(alert_string);}// Ask the reader how many study questions they'd like to trynum_questions_desired=window.prompt(alert_string+"How many examples would you like to try?");if(!allow_questions_to_be_repeated){while(num_questions_desired>num_study_questions){num_questions_desired=window.prompt(backup_string+"Please choose a number less than or equal to "+num_study_questions);}}// Loop over the questions to initialize the markersfor(question_index=0;question_index<num_study_questions;question_index++){temp_study_question=study_question_list[question_index];if(!temp_study_question){error_string="Study question "+question_index+" is undefined now (initialization).\n";window.alert(error_string);continue;}study_question_has_been_posed[question_index]=false;}// closes initialization loop over all the study questions // Select the questions at randomnum_questions_selected=0;while(num_questions_selected<num_questions_desired){if(allow_questions_to_be_repeated){question_index=Math.floor(num_study_questions*Math.random());selected_study_question_indices[num_questions_selected]=question_index;num_questions_selected++;}else{num_questions_remaining=num_study_questions-num_questions_selected;random_index=Math.floor(num_questions_remaining*Math.random());question_counter=0;for(question_index=0;question_index<num_study_questions;question_index++){if(study_question_has_been_posed[question_index]!=false){continue;}if(question_counter==random_index){study_question_has_been_posed[question_index]=true;selected_study_question_indices[num_questions_selected]=question_index;num_questions_selected++;break;}question_counter++;}// closes loop over all study questions}// closes check whether questions are allowed to be repeated }// closes while loop selecting the study questions for presentation//************************// Loop over the questions//************************for(question_index=1;question_index<=num_questions_selected;question_index++){study_question_index=selected_study_question_indices[question_index-1];temp_study_question=study_question_list[study_question_index];if(!temp_study_question){error_string="Study question "+question_index+" is undefined now (main loop).\n";window.alert(error_string);continue;}question_string=temp_study_question.title;if(!question_string){error_string="ERROR: No question string in study question "+study_question_index+"\n";window.alert(error_string);continue;}if(!question_string.match(/SUBJECT/)){error_string="ERROR: No SUBJECT in question string of study question "+study_question_index+"\n";window.alert(error_string);continue;}question_string="Question "+question_index+": "+question_string;// window.alert(question_string);// Determine all the answersnum_answers=0;span_nodes=temp_study_question.getElementsByTagName("SPAN");if(!span_nodes){error_string="ERROR: This study question has no SPAN nodes.\n";window.alert(error_string);return;}num_span_nodes=span_nodes.length;if(num_span_nodes<1){error_string="ERROR: This study question has zero SPAN nodes.\n";window.alert(error_string);return;}for(span_node_index=0;span_node_index<num_span_nodes;span_node_index++){temp_span_node=span_nodes[span_node_index];if(!temp_span_node){continue;}if(temp_span_node.className.match(/answer/)){answer_list[num_answers]=temp_span_node;answer_has_been_chosen_already[num_answers]=false;num_answers++;}}// closes loop over the SPAN nodes in the main articlealert_string="Study question "+question_index+" (type "+study_question_index+") has "+num_answers+" possible answers.\n";// window.alert(alert_string);// Choose num_answers_printed_with_question random answers for printingnum_answers_selected=0;while(num_answers_selected<num_answers_printed_with_question){num_answers_remaining=num_answers-num_answers_selected;random_index=Math.floor(num_answers_remaining*Math.random());answer_counter=0;for(answer_index=0;answer_index<num_answers;answer_index++){if(answer_has_been_chosen_already[answer_index]!=false){continue;}if(answer_counter==random_index){answer_has_been_chosen_already[answer_index]=true;selected_answer_indices[num_answers_selected]=answer_index;num_answers_selected++;break;}answer_counter++;}// closes loop over all possible answers to the question}// closes while() loop that selects the answers// Generate the question, choose correct and incorrect answerscorrect_printed_answer_index=Math.floor(num_answers_selected*Math.random());correct_answer_index=selected_answer_indices[correct_printed_answer_index];correct_answer_node=answer_list[correct_answer_index];if(!correct_answer_node){continue;}correct_printed_answer_index++;correct_answer_string=correct_answer_node.innerHTML.replace(/<[^>]+>/ig,"");question_string=question_string.replace(/SUBJECT/,correct_answer_node.title)+"\n\n";// window.alert(question_string); for(printed_answer_index=1;printed_answer_index<=num_answers_selected;printed_answer_index++){answer_index=selected_answer_indices[printed_answer_index-1];// question_string += printed_answer_index + ". Answer " + answer_index + "\n\n";temp_answer=answer_list[answer_index];if(!temp_answer){error_string="ERROR: Printed answer "+printed_answer_index+" of study question "+study_question_index+" is undefined.\n\n";window.alert(error_string);continue;}question_string+=printed_answer_index+". "+temp_answer.innerHTML.replace(/<[^>]+>/ig,"")+"\n\n";}// closes loop over answers to be printed// window.alert(question_string); // Present the question in a prompt windowquestion_string+="Please type in the number of the correct answer (1–"+num_answers_selected+"):";user_answer_index=window.prompt(question_string);num_questions_posed++;// Evaluate the answerif(user_answer_index==correct_printed_answer_index){num_correct_answers++;window.alert("Congratulations! You picked the correct answer.\n");}else{window.alert("Sorry! The correct answer was "+correct_printed_answer_index+": "+correct_answer_string+"\n");}}// closes loop over the study questions// Print summary of number of correct answerspercentage_of_correct_answers=0;if(num_questions_posed>0){percentage_of_correct_answers=Math.round((100*num_correct_answers)/num_questions_posed);summary_string="You gave "+num_correct_answers+" correct answers out of "+num_questions_posed+" questions posed ("+percentage_of_correct_answers+"%).\n\n";window.alert(summary_string);}}// closes function poseStudyQuestions()$(function(){mw.util.addPortletLink('p-navigation','javascript:poseStudyQuestions()','Study questions','ca-questions','Poses study questions on this topic to the reader','','');});//</pre>