Hi everyone. I would like to discuss this sample because I really think it is not the best one to be used as a guide and/or to share with the students.
The idea of the program is to look for the minimum and maximum values on different categories in order to display the corresponding cereal brand on the user interface.
The problem is that, for some categories, there are several cereal brands meeting the conditions. Ex: both Cheerios and Special K have the most proteins with a value of 6 grams. Another example: Cinnamon Toast Crunch, Cracklin’ Oat Bran, and Great Grains Pecan all have the maximum fat value with 3 grams. There are also similar cases for the minimum values.
When traversing the category list, we get the first of the maximum or minimum values and only display one brand name on the screen. This is wrong.
I modified the sample code by adding an extra loop. I would like to know what you think. I also would like to know if there is a better solution. This is what I did with the findMost function. I used a similar code for the findLeast function.
New findMost function:
function findMost() {
var input = getText(“mostDropdown”);
var category = getColumn(“Cereal Nutrition”, input);
var filteredMaxNames = ;
var max = 0;
var mostName;
for(var i = 0; i < category.length; i++){
if(category[i] > max) {
max = category[i];
}
}
for (var j = 0; j < category.length; j++) {
if (max == category [j] ) {
mostName = names[j];
appendItem(filteredMaxNames,mostName);
}
}
setText(“mostOutput”, ((“The cereal/s with the most " + input) + " is/are “) + filteredMaxNames.join(”\n”));
}
A few more things: when remixing the app you need to import the Cereal Nutrition Table again, but you also have to change the names of the categories in the design tab, since they are different from the ones used in the sample.
Also, you will have to be especially careful with the protein column named: Grams of Protein. The column name has an extra space between the words “of” and “Protein”. You either need to edit the name in the table or add an extra space in the name in the dropdown menu. If not, you will see an error message in the console.
Thanks for pointing this out. I will pass on this info to the code.org staff. I like your solution that finds a list of all cereals that fit the criteria.
In the future, if you find any other mistakes please let support@code.org know.
When the students select a category from the dropdown menu and, after that, they select Pick a Category, they get an error message. The message says: "You tried to get a column called “Pick a Category” from a table called “Cereal Nutrition”, but that column doesn’t exist. "
To solve this other issue, I added another if statement. See the screenshot.
Yes. I think the names of the columns in the table changed from last year to this year. So, if you want to remix the app, you not only need to import the table again, but you need change the names listed in the dropdown menu.
But again, if you select a category (any one) and then you click in “select a category” you will get an error message, unless you somehow fix the code. I added the else - if statement shown in the screenshot to solve it.
Besides this, there is something else that I would fix. If you select a category in the findMost or findLeast screens, go back to the home screen and then back again to the findMost or findLeast screens, the mostOutput or leastOutput are not cleared. Here you have another screenshot of what I did to solve this.
I have explained to my students that if they need to find the max value in a list, they are to create a variable and assign a very small value to it, like 0. Then, they have to traverse the list using a loop so they are able to compare the value of each element in the list against the value stored in the variable. If the value stored at each specific index in the list is higher than the value stored in the variable, they are to replace the lower value with the higher one.
All this is fine. But, instead of assigning the numerical value of 0 to the variable max, can we assign to the variable the value of the element at index 0 of the list that we need to traverse?
@rossiai Thanks for posing this question. We can set the Max value to the first index of the list or it can be set to zero. Both situations will find max.
I always use the first element of the list. Then traverse the list skipping the first element. That is easier to explain to students because it mirrors what a person would do. If you lined up a bunch of cans of soup and want to find the cheapest you don’t start with a fictitious soup that is free. You pick up the first one as your baseline and compare it to the rest until you find a cheaper one.
The other reason for this pattern is that it doesn’t require you to know the smallest possible number. In this example, we know the range is positive integers so we can start at zero. In general, we might not have that a priori knowledge.