Help needed for if/else statements in app lab:

I am creating a prototype quiz in applab for my students to refer for their project when they start it.
How can I use an if or if/else statement in my code? And have I structured the code the correct way? I am also trying to use the dropdown menu and select one answer but it doesn’t work.

Thank you for your help!

[Posts in the debugging category need to have the following pieces of information included in order to help other teachers or our moderator team best help you. Feel free to delete everything in brackets before posting your request for debugging help.]

Link to the project or level: [replace with link to the curriculum or get the “Share” link to a project]
What I expect to happen: [replace with a detailed description of the behavior you’re trying to create]
What actually happens: [replace with a detailed description of what actually happens when the code runs including any errors or unexpected behavior]
What I’ve tried: [replace with a detailed description of what you’ve already tried to do to solve the problem]

Hi @e.fat,

A couple of things:

  1. Don’t put onEvents inside of other onEvents. This can lead to unintended consequences, because everytime you click on a button, an extra onEvent is created. Move all of the onEvents out to their own space.

  2. To use the dropdown, you’d write something like this (replace with your own options):

 onEvent("dropdown1", "click", function(){
    if(getText("dropdown1") == "emu"){
        setScreen("winScreen");
    } else if (getText("dropdown1") == "ostrich"){
        setScreen("loseScreen");
    } else {
        setScreen("homeScreen");
    }
});

Thank you for that. I have fixed it. I now put it in a button as well so when the user selects from the option and then presses the button it will take them to the correct screen. But it is not working.

onEvent(“dropdown_quiz2”, “click”, function(){
if(getText(“dropdown_quiz2”) == “soccer”){
onEvent(“quiz2_button”, “click”, function(){
setScreen(“Correct_screen”);
});
} else if (getText(“dropdown_quiz2”) == “basketball”){
onEvent(“quiz2_button”, “click”, function(){
setScreen(“Wrong_screen”);
});
} });

I am having issues with this code. I have created an onevent button after the statement but it doesn’t work. When it comes to the dropdown menu screen when I click the down arrow I can’t select anything and it goes back to main screen. I want it to goto quiz3.

You have two event handlers for dropdown1. The change event is what most people would use there.

What do you mean? Can you show me if possible?

onEvent("dropdown1", "click", function( ) {
  setScreen("Correct_screen");
});

onEvent("dropdown1", "change", function(){
    if(getText("dropdown1") == "Kebab"){
        setScreen("correct_screen2");
    } else if ((getText("dropdown1") == "Burger")){
        setScreen("Wrong_screen");
    } else {
      setScreen("screen1");
    }
});

Most people use the drop down with the first answer being “choose”. That way you must drop down the list and choose one triggering a change. I assume the click is there to allow people to choose the original choice that is showing. That doesn’t actually work as well as you might thing.

Thanks for that. I have used the code you sent, but it still doesn’t work. When I click on the drop down menu without selecting an option it shows me the ‘correctscreen’.

Don’t use what I sent you. Look at what I sent you. There are two onEvent calls for “dropdown1”. Remove the “click” onEvent calls.

You can keep the button with the event handler, but you should not put the event handler inside the conditional. Just set the screen inside each conditional.

onEvent("quiz2_button", "click", function(){
    var selection = getText("dropdown_quiz2");
    if(selection == "soccer"){

       setScreen("Correct_screen");
  
    } else if (selection == "basketball"){

       setScreen("Wrong_screen");
} });