Week 1
Activity Guide
Week 2
Description of Week 2
The catquestion2 is used to store the answers to the text input question and check if they are correct. The dropdown is used to select what type of quiz will want to be taken. The on events are for navigating through the app and answering the multiple choice questions.
Week 3
Code
Code :
// Image for homepage : https://www.petspyjamas.com/uploads/2013/06/cat-and-dog.jpg
// Breed names and images are used from these lists from Code.org :
// Data Library : "Cats", Data Library : "Dogs"
// Variable section
var catcorrectcounter = 0;
var catincorrectcounter = 0;
var dogcorrectcounter = 0;
var dogincorrectcounter = 0;
var petname_prevlist = ["dummy"];
// Dropdown menu for quiz selection
onEvent("dropdown1", "input", function( ) {
var dropdown = getText("dropdown1");
if (dropdown == "Dogs Quiz") {
setScreen("DogInstructions");
}
if (dropdown == "Cats Quiz") {
setScreen("CatInstructions");
}
});
// Start to begin quiz and Back to change quiz selection to cat or dog
onEvent("CatStart", "click", function( ) {
setScreen("CatsQuiz");
catbase();
});
onEvent("CatBack", "click", function( ) {
setScreen("Home");
});
onEvent("DogStart", "click", function( ) {
setScreen("DogsQuiz");
dogbase();
});
onEvent("DogBack", "click", function( ) {
setScreen("Home");
});
// On Next button click Get the pet name from user and display right or wrong and total points for cat quiz.
//supports only 3 Max try.
onEvent("CatNext", "click", function( ) {
var cat_name = getText("catinput");
var in_list = pet_namecheck("Cats",cat_name);
if (cat_name == in_list) {
setText("catresults", "You got it right!");
catcorrectcounter = catcorrectcounter + 1;
setText("catpoints", "Points : " + catcorrectcounter + "/3");
} else {
setText("catresults", "You got it wrong!");
catincorrectcounter = catincorrectcounter + 1;
}
var cat_maxtry = catcorrectcounter + catincorrectcounter;
if (cat_maxtry == 3) {
setScreen("CatScore");
setText("Cat_Score", "You scored a total of " + catcorrectcounter + "/3 points");
onEvent("CatScoreHome", "click", function( ) {
catcorrectcounter = 0;
catincorrectcounter = 0;
setText("catpoints", "Points : " + catcorrectcounter + "/3");
setText("catresults", "");
setScreen("Home");
});
}
setText("catinput","");
catbase();
});
// On Next button click Get the pet name from user and display right or wrong and total points for dog quiz.
//supports only 3 Max try.
onEvent("DogNext", "click", function( ) {
var dog_name = null;
dog_name = getText("doginput");
var in_list = pet_namecheck("Dogs",dog_name);
if (dog_name == in_list) {
setText("dogresults", "You got it right!");
dogcorrectcounter = dogcorrectcounter + 1;
setText("dogpoints", "Points : " + dogcorrectcounter + "/3");
} else {
setText("dogresults", "You got it wrong!");
dogincorrectcounter = dogincorrectcounter + 1;
setText("dogpoints", "Points : " + dogcorrectcounter + "/3");
}
var dog_maxtry = dogcorrectcounter + dogincorrectcounter;
if (dog_maxtry == 3) {
setScreen("DogScore");
setText("Dog_Score", "You scored a total of " + dogcorrectcounter + "/3 points" );
onEvent("DogHomeScore", "click", function( ) {
dogcorrectcounter = 0;
dogincorrectcounter = 0;
setText("dogpoints", "Points : " + dogcorrectcounter + "/3");
setText("dogresults", "");
setScreen("Home");
});
}
setText("doginput","");
dogbase();
});
// Base Function for the cat to get random index and its name
function catbase() {
var catindex = randompetindex("Cats");
getpetimage_id("Cats",catindex); // call to set image on quiz panel
var catname = getpetname("Cats",catindex);
console.log("Name : " + catname); // name to show in console for test only
}
// Base Function for the dog to get random index and its name
function dogbase() {
var dogindex = randompetindex("Dogs");
getpetimage_id("Dogs",dogindex); // Call to set image on quiz panel
var dogname = getpetname("Dogs",dogindex);
console.log("Name : " + dogname); // name to show in console for test only
}
// Function to generate Randem Pet Index
// parameter pet_columnname - Cats or Dogs
function randompetindex(pet_columnname) {
var petnamelist = getColumn(pet_columnname, "Name");
var petindex = randomNumber(1, petnamelist.length - 1);
return petindex;
}
// Function to set the pet image on the quiz panel
// parameter pet_columnname - Cats or Dogs
function getpetimage_id(pet_columnname,petname_index) {
var petpicturelist = getColumn(pet_columnname, "Image");
var setpetpic = petpicturelist[petname_index];
if(pet_columnname == "Cats") {
setProperty("catimage", "image", setpetpic);
}
if(pet_columnname == "Dogs") {
setProperty("dogimage", "image", setpetpic);
}
}
// Function to get the pet name to display on the console for testing only
// parameter pet_columnname - Cats or Dogs
function getpetname(pet_columnname,petname_index) {
var petnamelist = getColumn(pet_columnname, "Name");
var petname = petnamelist[petname_index];
return petname;
}
// Function to check the petname and return the petname if present
// parameter pets - Cats or Dogs
// Parameter petname - Input petname to check
function pet_namecheck(pets,petname) {
var petnamelist = getColumn(pets, "Name");
var finalpetlist = [];
var petname_prevlength = petname_prevlist.length;
// AppendItems to the finalpetlist with out duplicate entry.
// By checking against the previously entered pet name list
for (var i = 0; i < petnamelist.length; i++) {
if (petnamelist[i] != petname_prevlist[petname_prevlength-1]){
appendItem(finalpetlist, petnamelist[i]);
}
}
// Build petnamerepeatlist with previously entered pet name
appendItem(petname_prevlist,petname);
// Iterate the finalpetlist to check the given petname
for (var j = 0; j < finalpetlist.length; j++) {
if (petname == finalpetlist[j]) {
return petname;
}
}
}
Description of Week 3
Added for loop and added functions to allow for code to be less repeated and more organized. Added points system and question limit of 3. Finished all the code and user interface. Link to video of completed app : https://drive.google.com/file/d/1j43r4BHJaW1LVqe6gwoL52IrjkHk45-L/view?usp=sharing
Will finish writing response and submit everything on Tuesday.