Hackathon unit6 lesson 13 uistring error

I have searched many posts and tried many solutions. Can someone please help me settle this code?

Code:unit 6 lesson 13

//Create lists
var typeList = [(getColumn(“Beverages Nutrition”, “Type”))];
var caloriesList = [(getColumn(“Beverages Nutrition”, “Calories”))];
var sugarsList = [(getColumn(“Beverages Nutrition”, “Sugars (g)”))];
var proteinList = [(getColumn(“Beverages Nutrition”, “Protein (g)”))];

//Create variables
var index = 0;

//Create filtered lists
var filteredTypeList = ;
var filteredCaloriesList = ;
var filteredSugarsList = ;
var filteredProteinList = ;

//Create functions

//Create onEvents
//When clicked, changes the screen to the sugarsScreen and displays the sugars
onEvent(“sugarsButton”, “click”, function( ) {
//When clicked, changes the screem to the proteinScreen and displays the protein
onEvent(“proteinButton”, “click”, function( ) {
//When clicked, changes the screen to the caloriesScreen and displays the calories
onEvent(“caloriesButton”, “click”, function( ) {
//When clicked, changes the screen back to the homeScreen
onEvent(“backButton1”, “click”, function( ) {
onEvent(“backButton2”, “click”, function( ) {
onEvent(“backButton3”, “click”, function( ) {

//Create function to filter the lists
function filter() {
for (var i = 0; i < typeList.length; i++) {
var soda = typeList[i];
if (soda == “Soda”) {
appendItem(filteredTypeList, typeList[i]);
appendItem(filteredCaloriesList, caloriesList[i]);
appendItem(filteredSugarsList, sugarsList[i]);
appendItem(filteredProteinList, proteinList[i]);
//Create function to update the screen
function updateScreen() {
index = randomNumber(0, filteredCaloriesList.length - 1);
setText(“caloriesOutput”, filteredCaloriesList[index]);
setText(“sugarsOutput”, filteredSugarsList[index]);
setText(“proteinOutput”, filteredProteinList[index]);

Please explain what is happening and what is the expected result. What errors are you getting? Also provide the link to the project (Click on the share button and copy paste the link here).

The expected result is for each screen, “Calories, Protein, and Sugars” to display the nutrition information from the type of beverage “Soda”. The code should pull from the data column and filter out the selected results. The errors are related to uistring and toString.

Error message:
WARNING: setText() text parameter value (undefined) is not a uistring.
ERROR: TypeError: Cannot read properties of undefined (reading ‘toString’)

Project link;

Hi @matthewj,

I’ve been playing with this one for about an hour now and I feel like I’m making progress, but I’m not quite there yet. Hopefully this will give you a light bulb moment, or someone else can build on my work: link to my remix

I’ve figured out that it needs to recognize that it’s looking for text to print, and right now it’s trying to print the array. There’s not a toString() function in App Lab, which was my first instinct to look for. I added a +“” segment at the end of lines 65-67, and I no longer get the error, but it now just prints empty.

I added a console.log call under those lines (so now it’s 66, 68, and 70) and those also show up empty. I have not done a deep-debug on the rest of the code.

Sorry I couldn’t get it all the way there, but like I said, hopefully this gives you (or someone else!) a nudge in the right direction on a solution.

–Michael K.

The actual error comes from filteredCaloriesList being an empty array when updateScreen() is called. As @mkmietowicz pointed out, this is because the program is trying to append arrays to each other. All of this is due to how the list variables are defined:

var typeList = [(getColumn("Beverages Nutrition", "Type"))];
var caloriesList = [(getColumn("Beverages Nutrition", "Calories"))];
var sugarsList = [(getColumn("Beverages Nutrition", "Sugars (g)"))];
var proteinList = [(getColumn("Beverages Nutrition", "Protein (g)"))];

The brackets define each variable as an array containing another array, which causes all sorts of problems with indexing. You can fix it by removing the brackets like this:

var typeList = getColumn("Beverages Nutrition", "Type");
var caloriesList = getColumn("Beverages Nutrition", "Calories");
var sugarsList = getColumn("Beverages Nutrition", "Sugars (g)");
var proteinList = getColumn("Beverages Nutrition", "Protein (g)");

Hopefully this fixes the issue!

1 Like