Trouble with code for project in Unit 4

What I expect to happen: Student’s code seems correct, and it works for me when I copy and paste it into my account.
What actually happens: Output is “undefined” for restaurant recommendation and political affiliation
What I’ve tried: I recreated her design and pasted her code into my account, and it worked. But it will not work on her account.

The project link goes to the lesson instead of your student’s code.

Thanks for letting me know. Here’s the code:

var politicalaffDrop;
var zipCodeInput;
var todayDrop;
var restaurantsug;
onEvent(“zipCodeInput”, “change”, function( ) {
zipCodeInput = getText(“zipCodeInput”);
onEvent(“politicalAffDrop”, “input”, function( ) {
politicalaffDrop = getText(“politicalAffDrop”);
onEvent(“todayDrop”, “change”, function( ) {
todayDrop = getText(“todayDrop”);
//Different Choices result in different restaurants
function updateScreen() {
if (politicalaffDrop==“liberal” && todayDrop==“Italian”) {
restaurantsug = " Versatile in San Clemente";
} else if (politicalaffDrop==“liberal” && todayDrop==“Mediterranean”) {
restaurantsug = " Kabob It";
} else if (politicalaffDrop==“liberal” && todayDrop==“Mexican”) {
restaurantsug = " Taki Taco";
} else if (politicalaffDrop==“liberal” && todayDrop==“Asian”) {
restaurantsug = " Hiramatoya Sushi";
} else if (politicalaffDrop==“liberal” && todayDrop==“FastFood”) {
restaurantsug = " BurgerIn";
} else if ((politicalaffDrop==“moderate” && todayDrop==“Italian”)) {
restaurantsug = " Versatile in San Clemente or Bellisima in Long Beach" ;
} else if ((politicalaffDrop==“moderate” && todayDrop==“Mediterranean”)) {
restaurantsug = " Kabob It or Mediterranean Fresh";
} else if ((politicalaffDrop==“moderate” && todayDrop==“Mexican”)) {
restaurantsug = " Taki Taco or King Burrito&Taco";
} else if ((politicalaffDrop==“moderate” && todayDrop==“Asian”)) {
restaurantsug = " Hiramatoya Sushi or Mong BBQ";
} else if ((politicalaffDrop==“moderate” && todayDrop==“FastFood”)) {
restaurantsug = " BurgerIn or Lesley’s";
} else if ((politicalaffDrop==“conservative” && todayDrop==“Italian”)) {
restaurantsug = " Bellisma in Long Beach";
} else if ((politicalaffDrop==“conservative” && todayDrop==“Mediterranean”)) {
restaurantsug = " Mediterranean Fresh";
} else if ((politicalaffDrop==“conservative” && todayDrop==“Mexican”)) {
restaurantsug = " King Burrito&Taco";
} else if ((politicalaffDrop==“conservative” && todayDrop==“Asian”)) {
restaurantsug = " Mong BBQ";
} else if ((politicalaffDrop==“conservative” && todayDrop==“FastFood”)) {
restaurantsug = " Lesley’s";
} else if ((todayDrop==“other”)) {
restaurantsug = " local restaurants around your neighborhood";

setText(“restaurant”, (((((("Hello. Today, you want to eat " + todayDrop) + “\nfood. So, we suggest you eat at”) + restaurantsug) + "\nto support ") + politicalaffDrop) + "\nrestaurants near ") + zipCodeInput );

I am guessing from the widget names that you have two dropdowns on the screen. Dropdowns can be annoying, misunderstood, and often both.

The callback in onEvent for a dropdown is only called when the value changes. If you pull down the menu and choose the same thing that was showing nothing runs. What that means is that your two variables politicalaffDrop and todayDrop may still be set to undefined as updateScreen is called.

If you change the type of food but not the political affiliation then I would expect to see exactly what you describe. updateScreen has a cascading if without a final default that leaves restaurantsug set to undefined.

What you can do is initialize those variables to be what you see on the screen in the dropdowns, the default, then they will have values other than undefined.

Thanks so much for your reply. I’ve had trouble with dropdowns on student projects before. They usually end up working if we use the “change” option. In this case, even if we change the political affiliation and food type it doesn’t work. But what’s strange is that the code works when I put it in my account. This has happened with other projects, too. I wondered about that final default else statement being missing. I’ll tell the student to fix that.

I really appreciate your help. This is my first year teaching AP CSP (as I’m sure you can tell!).

One of the two onEvent calls for the two dropdowns is using “input” instead of “change”.