I'm coding a project in the app lab feature of code.org. I wrote a function with the goal of taking two parameters (text input and a number) and evaluating the text input as a yes or no question. I have to use a list, so the function adds a certain amount to a variable named "user_ans" each time and then later in the code the variable is used to print an string item from the list. The problem is, I need "user_ans" to start at 0, run the function once, save the updated value, and then run the function again, adding to the updated value rather than 0 again. I can't find a way to do this because it's inside a function. I can't get rid of the list, and I have to run the function at least twice. I cannot use closures. All my code is below.
//Variables for output
var output = ["You should get more sleep and exersise! Exersise is good for your body and your mind! Sleep is an essential component to feeling happy and healthy.", "You should get more exersise! Exersise is good for your body and your mind!", "You should get more sleep! Sleep is an essential component to feeling happy and healthy.", "Congrats! You're on track for a healthy lifestlye. Keep up the good work!"];
var user_ans = 0;
//defines a variable that will hold the final output
var fin = "";
// controls home buttons on all the pages
onEvent("home1", "click", function( ) {
setScreen("homeScreen");
});
onEvent("home2", "click", function( ) {
setScreen("homeScreen");
});
onEvent("home3", "click", function( ) {
setScreen("homeScreen");
});
onEvent("home4", "click", function( ) {
setScreen("homeScreen");
});
onEvent("home5", "click", function( ) {
setScreen("homeScreen");
});
//controls the start button
onEvent("startButton", "click", function( ) {
setScreen("screen1");
});
//controls the collection of data from screen1 and screen2
onEvent("enter1", "click", function( ) {
get_ans(getText("text_input"), 1);
user_ans = user_ans + get_ans(getText("text_input"), 1);
});
onEvent("enter2", "click", function( ) {
get_ans(getText("text_input1"), 2);
user_ans = user_ans + get_ans(getText("text_input1"), 2);
if (getText("text_input1") == "yes") {
fin = output[user_ans];
setText("final_output_area", fin);
setScreen("screen3");
}
});
//Controls the output of how much more sleep or exercise you need. Only occurs if you had answered "no".
onEvent("enter1a", "click", function( ) {
var sleep_no = 7 - getNumber("slider2");
var sleep_needed = "You should be getting at least " + (sleep_no + " more hours of sleep.");
setText("final_output_sleep", sleep_needed);
setScreen("screen2");
});
onEvent("enter2a", "click", function( ) {
var exrs_no = 3 - getNumber("slider3");
var exrs_needed = "You should be getting at least " + (exrs_no + " more days of exercise.");
setText("final_output_exrs", exrs_needed);
fin = output[user_ans];
setText("final_output_area", fin);
setScreen("screen3");
});
//evaluates yes or no answers
function get_ans(text, number) {
if (text == "yes") {
user_ans = user_ans + number;
setScreen("screen" + (number + 1));
} else if (text == "no") {
user_ans = user_ans + 0;
setScreen("noScreen" + number);
} else {
playSpeech("You must enter a lowercase yes or no.", "female", "English");
}
return user_ans;
}