The document outlines a JavaScript program that generates random country-related questions for a quiz application. It includes functions to retrieve country data such as name, flag, GDP per capita, income level, and region, and allows users to navigate through a list of countries. Users can answer questions about the countries, and the application tracks their performance, providing feedback based on their answers.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
2 views6 pages
Create Task PDF - Country App
The document outlines a JavaScript program that generates random country-related questions for a quiz application. It includes functions to retrieve country data such as name, flag, GDP per capita, income level, and region, and allows users to navigate through a list of countries. Users can answer questions about the countries, and the application tracks their performance, providing feedback based on their answers.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6
var countries = getColumn("Countries and Territories", "Country Name");
var flags = getColumn("Countries and Territories", "Flag");
var gdpPerCapita = getColumn("Countries and Territories", "GDP Per Capita"); var development = getColumn("Countries and Territories", "Income Level"); var abbreviation = getColumn("Countries and Territories", "Two Letter Country Code"); var region = getColumn("Countries and Territories", "Region"); var appendOwnRegionsList = []; var appendOwnGDPPerCapitaList = []; var appendOwnFlagsList = []; var appendOwnDevelopmentList = []; var appendOwnCountriesList = []; var appendOwnCodeList = []; var countryName=""; var index = 0; var randomCountryIndex; var numberInput=0; var randomIndex=0; var questionBank = ["What is the level of development of ", "In what region is "]; var randomQuestion; randomQuestion = randomNumber(0, questionBank.length - 1); var randomCountry; var guess; var counter = 0; var correctDevelopment; var correctRegion; // This is the variable initializations, global variables to be used throughout the code \\ onEvent("random_Button", "click", function( ) { randomIndex = randomNumber(0, countries.length - 1); countryName = countries[randomIndex]; appendCountryInfo(); if (appendOwnCountriesList.length == 1) { index = 0; } else { index = index + 1; } updateScreen(); }); // This is the Event for the country button pulling a random index from the country database using the index in the countries list to make a random country // // Then there is a call to appendCountryInfo that appends the country name and its corresponding statistics in separate lists // function appendCountryInfo() { appendItem(appendOwnCountriesList, countryName); appendItem(appendOwnRegionsList, region[randomIndex]); appendItem(appendOwnGDPPerCapitaList, gdpPerCapita[randomIndex]); appendItem(appendOwnFlagsList, flags[randomIndex]); appendItem(appendOwnDevelopmentList, development[randomIndex]); appendItem(appendOwnCodeList, abbreviation[randomIndex]); } //This is the function that appends the corresponding statistics of the random country to empty lists// function regionFunction(countryName) { for (var i = 0; i < countries.length; i++) { if (countries[i] == countryName) { return (region[i]); } } } //This function matches the random country name to a place in the region // function flagFunction(countryName) { for (var i = 0; i < countries.length; i++) { if (countries[i] == countryName) { return (flags[i]); } } } // This function matches the random country name to its corresponding GDP Per Capita statistic // function gdpPerCapitaFunction(countryName) { for (var i = 0; i < countries.length; i++) { if (countries[i] == countryName) { return (gdpPerCapita[i]); } } } // This function matches the random country name to its corresponding development level // function developmentFunction(countryName) { for (var i = 0; i < countries.length; i++) { if (countries[i] == countryName) { return (development[i]); } } } // This function matches the random country name to its corresponding two code abbreviation // function abbreviationFunction(countryName) { for (var i = 0; i < countries.length; i++) { if (countries[i] == countryName) { return (abbreviation[i]); } } } // This event makes the left arrow work, allowing the user to go back in the list // onEvent("left_Arrow", "click", function( ) { if (index > 0) { index = index - 1; } countryName = appendOwnCountriesList[index]; updateScreen(); }); // This event makes the right arrow work, allowing the user to go ahead in their list // onEvent("right_Arrow", "click", function( ) { if (index < appendOwnCountriesList.length - 1) { index = index + 1; } countryName = appendOwnCountriesList[index]; updateScreen(); }); // This is the update function that is called after the random Event to display the statistics in each element on the screen // function updateScreen() { setText("abbreviation_Area", abbreviationFunction(countryName)); setProperty("gdpPerCapita_Area", "text", "$" + gdpPerCapitaFunction(countryName)); setProperty("count_Area", "text", appendOwnCountriesList.length); setProperty("region_Area", "text", regionFunction(countryName)); setText("countryName", countryName); setProperty("flag_Image", "image", flagFunction(countryName)); setText("development_Area", developmentFunction(countryName)); } // This is the quiz button click Event that goes to the quiz_Question_Page that asks the user how many questions they want to generate // // Uses the length of their country list to set a limit to the amount of questions they want to generate // onEvent("quiz_Button", "click", function( ) { setScreen("quiz_Question_Page"); if (numberInput == 0) { setProperty("question_Display", "text", "Go learn something!"); } if (appendOwnCountriesList.length == 0) { setProperty("question_Display", "text", "Go learn something!"); hideElement("generation_Button"); hideElement("number_Question"); hideElement("question_Request"); showElement("back_Button_Generation"); } if (appendOwnCountriesList.length > 0) { showElement("generation_Button"); showElement("number_Question"); showElement("question_Request"); onEvent("number_Question", "click", function( ) { numberInput = getNumber("number_Question"); setProperty("question_Display", "text", numberInput); if (numberInput > appendOwnCountriesList.length) { setProperty("question_Display", "text", "Come on, learn!"); } }); } }); // For the quiz_Question_Page, there is a back button element that allows the user to go back to the country App screen to learn and generate more information // onEvent("back_Button_Generation", "click", function( ) { setScreen("country_App_Scren"); }); // For the quiz_Screen, there is a back button element that allows the user to go back to the country App Screen // onEvent("back_Button", "click", function( ) { setScreen("country_App_Scren"); }); // Once the generate button is clicked from the quiz_Question_Page, there will be a redirection to quiz_Screen, that randomly generates a question for the user and calls the traversals function // onEvent("generation_Button", "click", function( ) { setScreen("quiz_Screen"); randomCountryIndex = randomNumber(0, appendOwnCountriesList.length - 1); randomCountry = appendOwnCountriesList[randomCountryIndex]; setProperty("question_Display_Output", "text", "Here is your question: " + (questionBank[randomQuestion] + (randomCountry + "?"))); setProperty("flag_Quiz_Image", "image", appendOwnFlagsList[randomCountryIndex]); traversals(randomCountry); }); // This function is called twice: generation button event and quizTime function, used to sift through the premade lists of information to get the correct answer for the question, as a check for the user's guess // function traversals(randomCountry) { for (var i = 0; i < appendOwnCountriesList.length; i++) { if (appendOwnCountriesList[i] == randomCountry) { correctDevelopment = appendOwnDevelopmentList[randomCountryIndex]; } if (appendOwnCountriesList[i] == randomCountry) { correctRegion = appendOwnRegionsList[randomCountryIndex]; } } } // Once this button is clicked from the quiz_Time screen, the input will be collected and the quizTime function will be called // onEvent("submition_Button", "click", function( ) { guess = getText("input_Answer"); quizTime(guess); }); // This is the quizTime function that uses the user's guess and the premade answer choices as a check, onsetting a conditional, if the guess is correct, the program will create a new question using the numberInput to generate the amount of questions desired // // If the guess is wrong at any point in the code, the screen will change to the tried_Screen, telling the user that they were wrong and needs to click the back button to start again // // If the guess is right for every question, then the screen will change to the completion_Screen, telling the user they scored perfectly on each question // function quizTime(guess) { traversals(randomCountry); if (guess == correctRegion) { setText("input_Answer", ""); counter = counter + 1; for (var i = 0; i < numberInput; i++) { randomCountryIndex = randomNumber(0, appendOwnCountriesList.length - 1); randomCountry = appendOwnCountriesList[randomCountryIndex]; randomQuestion = randomNumber(0, questionBank.length - 1); setProperty("question_Display_Output", "text", "Here is your question: " + (questionBank[randomQuestion] + (randomCountry + "?"))); setProperty("flag_Quiz_Image", "image", appendOwnFlagsList[randomCountryIndex]); if (guess == correctRegion) { setText("input_Answer", ""); } if (counter >= numberInput) { setScreen("completion_Screen"); } } } else if (guess == correctDevelopment) { setText("input_Answer", ""); counter = counter + 1; for (var j = 0; j < numberInput; j++) { randomCountryIndex = randomNumber(0, appendOwnCountriesList.length - 1); randomCountry = appendOwnCountriesList[randomCountryIndex]; randomQuestion = randomNumber(0, questionBank.length - 1); setProperty("flag_Quiz_Image", "image", appendOwnFlagsList[randomCountryIndex]); setProperty("question_Display_Output", "text", "Here is your question: " + (questionBank[randomQuestion] + (randomCountry + "?"))); if (guess == correctDevelopment) { setText("input_Answer", ""); } if (counter == numberInput) { setScreen("completion_Screen"); } } } else { setScreen("tried_Screen"); } } // Once the user completes their desired questionaire and scores perfectly, going to the completion_Screen, they can select the back button to go back to the country information screen // onEvent("success_BackButton", "click", function( ) { setText("input_Answer", ""); setScreen("country_App_Scren"); numberInput = 0; counter = 0; }); // When the user goes to the tried screen, they can click the back button that is specific to the tried_Screen to return back to the Country_App_Scren and generate more information // onEvent("failure_BackButton", "click", function( ) { setText("input_Answer", ""); setScreen("country_App_Scren"); counter = 0; numberInput = 0; });