Student project help

Link to the project or level: (App Lab - Code.org)
What I expect to happen: If the zip code entered by the user is in the list bankZip, the address of the food bank will be displayed. If not, a message stating the zip code does not have a food bank listed yet and prompting the user to add a food bank to the list will be displayed.
What actually happens: When the else statement is present, the message stating there is no food bank appears; when the else statement is removed, the program does provide the address of the food bank in that zip code or area. But then the statement saying no food banks listed yet does not appear, of course.
What I’ve tried: moving the conditional statement to an on event for the search button. Checking syntax carefully.

Let’s talk about what this code does:

  for(var i = 0; i < bankZip.length; i++) {
    if(bankZip[i] == userZip) {
      setText("addressInfo", "The food bank in this zip code is called " +
      foodBankName [i] + "and " + bankAddress [i] + " is where it's located!");
    } else { 
      setText("addressInfo", "The zip code you have chosen is not in our database." 
  + " If you know of a food bank in this zip code and choose to enter it in our data base," +
   " please click Add New Bank!");
    }
  }

You are checking each and every zip code and doing one of two things. Since both of these two things set the same text box, the only zip code that matters is the last one on the list.

If the very last zip code in the bankZip array matches the zip code entered you will show the address. If the very last zip code in the bankZip array matches the zip code entered it will say no address.

How often are you asking for the very last zip code in the bankZip array? Is that really the only zip code that matters to you? What do you need to change if you want the other zip codes to matter?

Hi Don,

Thanks for your reply. I added another text area to display the not available message. But it shows the message even when the zip code is found, along with the message in the other text box. What can we change to be able to display only the message in the top box when the zip code is found? I thought the for loop was traversing the list and checking each element.

From my understanding, it sounds like you only want the “else” to happen IF after the whole list has been traversed you still have not found the one you want. In that case, I might use a boolean flag to tell the computer whether I have found the desired zip after the end of the traversal and whether it should show the failed message or not.

Here is an example with finding a number in an array:

boolean isNumInList(num, list) {
  for (int i = 0; i < array.size; i++) {
    if (list[i] == num) { 
      return true;
    } 
  }
  return false; 
} 

We haven’t talked much with kids about returning early from a method (not completing all of the loops of the for loop), so it is more efficient, but might be a little weird looking. Another version (with the boolean flag) would be something like this:

boolean isNumInList(num, list) {
  var exists = false
  for (int i = 0; i < array.size; i++) {
    if (list[i] == num) { 
      exists = true;
    } 
  }
  return exists; 
} 
1 Like

Thanks for your reply, Madeline. I tried using the boolean you mentioned above, but I was unable to make it work. I’m not too sure about how to use return in this case in order to display the message. Could you possibly rewrite this function for me so that I can help my student tomorrow in class? I’ve spent so many hours on this app, using all the documentation provided on code.org and previous lessons. I usually am able to find a solution if I work hard enough on an app, but I’m really stumped on this one.

function getBank() {
var userZip = getText(“userZip”);
for(var i = 0; i < bankZip.length; i++) {
if(bankZip[i] == userZip) {
setText(“addressInfo”, "The food bank in this zip code is called " +
foodBankName [i] + “and " + bankAddress [i] + " is where it’s located!”);
} else { setText(“addressInfo”, “The zip code you have chosen is not in our database,”

  • " if you know a food bank in this zip code and choose to enter it in our database," +
    " please click Add New Bank!");
    }
    }
    }

Here is how you would use the boolean. See the code that is in bold. Once the zipcode is found, you do not want to change the text in the textbox. If it is not found, then it should display the not found message.

function getBank() {
  var userZip = getText("userZip");
  var found = false;
  for(var i = 0; i < bankZip.length; i++) {
    if(bankZip[i] == userZip) {
      found = true;
      setText("addressInfo", "The food bank in this zip code is called " +
      foodBankName [i] + "and " + bankAddress [i] + " is where it's located!");
    } else if (!found) { 
   setText("addressInfo", "The zip code you have chosen is not in our database," 
    + " if you know a food bank in this zip code and choose to enter it in our data base," +
    " please click Add New Bank!");
    }
  }
}
2 Likes

Thank you so much! This works perfectly, and now I understand how it works. I appreciate your help.

1 Like

The take away here is that you are looking at every element in the list. By setting the text box for each and every element only the last element matters.

Here is a metaphorical explanation. Grab a book. You want to know if that book has any even numbered pages.

Here is your process: Start at the first page. If the page has an even number turn on the lamp next to you. If the page has an odd number turn off the lamp next to you. Turn the pages till the end. If the light is on then the book has an even numbered page. If the light is off it doesn’t.

Try it with a book that has pages 1 through 100. Did that process work? Now try it with a book that has 1 through 101 pages. Did that process work?

Does the lamp being off mean you didn’t see an even page? Why not? How could you make the state of the lamp mean the book has even pages in it?

The answer of course is to stop turning it off when you see an odd page! But what if the lamp is already on when you start? Obviously the first thing is to turn it off.

The code Sangeeta has written implements that idea.

From a style point of view I would write something like this.1 I am using the text box itself to remember if it was found.

function getBank() {
  var userZip = getText("userZip");
  setText(
    "addressInfo", "The zip code you have chosen is not in our database;" + 
    " if you know a food bank in this zip code and choose to enter it into our database," +
    " please click Add New Bank!"
  );
  for(var i = 0; i < bankZip.length; ++i) {
      if (bankZip[i] == userZip) {  
         setText(
          "addressInfo", "The food bank in this zip code is called " +
          foodBankName [i] + " and " + bankAddress [i] + " is where it's located!"
        );
    }
  }
}

Now you might be thinking “If I am looking for an even numbered page in a book can’t I stop when I find one?”

Yes, Madeline addressed that and pointed out that the students are not familiar with breaking out of a loop. I agree with that decision. There was a time when practitioners of the Arcane Art of Computer Programming spent years trying to avoid that very thing. It was considered confusing. (Later we discovered it wasn’t the multiple return paths, but rather the size of a function that was confusing.) It still remains as a point of contention.2

You could write something like this.

function getBank() {
  var i;
  var userZip = getText("userZip");
  for(i = 0; i < bankZip.length && bankZip[i] != userZip; ++i) {
  }
  if (i == bankZip.length) {
    setText(
      "addressInfo", "The zip code you have chosen is not in our database;" + 
      " if you know a food bank in this zip code and choose to enter it into our database," +
      " please click Add New Bank!"
    );
  } else {
    setText(
      "addressInfo", "The food bank in this zip code is called " +
      foodBankName [i] + " and " + bankAddress [i] + " is where it's located!"
      );
  }
} 

This is concise, but probably confusing because instead of using an explicit boolean like found we are using the index variable i to tell us what happened. This is also going to be difficult for block users to code. But it does stop looking as soon as we find what we wanted.


1You may also have noticed that I use ++i instead of i++. I found out that the former does what students think it does while the latter does not.

2It may surprise you how heated an argument can result from 5 lines of code within the professional community. When the new way to program, unfortunately named Agile, was born there was a huge outrage. People got angry. There is even a book that has satirical and derisive song parodies about the new ideas. 3

3Rosenberg, D., & Stephens, M. (2008). Extreme programming refactored: The case against XP. Apress.

1 Like