JavaScript Debugging Help On My Project

Hello,
So my problem is that I’m trying to get a result from the text area based on the dropdown options through a function that I created, but have no clue as to what to do about it. Basically, the purpose of this function is for the user to select a type of food and beverage, and what exactly does the user eat and drink, and the drop down menus are based on data from a website. For the most part, I was able to filter into separate lists and function them for the user to select the options, but when it comes to publishing the result, the result doesn’t work. To prove my point, here’s the function that I’m working on:

function CalculatingResult (meal) {
  var result;
  var decision;
  var filteredFoodTypes;
  var filteredBeverageTypes;
  for (var x = 0; x < filteredFoodTypesList.length; x++) {
    filteredFoodTypes = filteredFoodTypesList[x];
    if (selectedCategoryOfFood == filteredFoodTypes) {
      foodCalories = foodCalories[x];
  }
  }
  
  for (var y = 0; y < filteredBeverageTypesList.length; y++) {
    filteredBeverageTypes = filteredBeverageTypesList[y];
    if (selectedCategoryOfBeverage == filteredBeverageTypes) {
      beverageCalories = beverageCalories[y];
  }
  totalCalories = foodCalories + beverageCalories;
  
  if (meal == "Lunch" || meal == "Dinner" && totalCalories >= 500 && totalCalories <= 700) {
    decision = "Hello, " + name + ", but this would be an acceptable " + meal.toLowerCase() + ", because " + selectedCategoryOfFood.toLowerCase() + " contains " + foodCalories + " calories, while " + selectedCategoryOfBeverage + " contains " + beverageCalories + " calories, which results in the total of " + totalCalories +  " calories , which is therefore acceptable. ";
  } else {
    decision = "Hello, " + name + ", but this would be an unacceptable " + meal.toLowerCase() + ", because " + selectedCategoryOfFood.toLowerCase() + " contains " + foodCalories + " calories, while " + selectedCategoryOfBeverage + " contains " + beverageCalories + " calories, which results in the total of " + totalCalories +  " calories , which is therefore unacceptable. ";
  }
  for (var c = 1; c < 4; c++) {
   result = setProperty("RecommendationTextArea"+c, "text", decision); 
  }
  return result;
}
}

In order to figure out where some of these things are coming from. I provided some examples of some onEvents that I wrote in the program. These are the following

onEvent("CategoryBeverageDropdown3", "change", function ( ) {
  selectedCategoryOfBeverage = getText("CategoryBeverageDropdown3");
});
onEvent("CategoryFoodDropdown3", "change", function ( ) {
  selectedCategoryOfFood = getText("CategoryFoodDropdown3");
});
var foodCalories = getColumn("Fast Food Nutrition", "Calories");
var beverageCalories = getColumn("Beverages Nutrition", "Calories");
var totalCalories;

Based on this information, the result that I was expecting was that…
Hi, (your name), but this would be an unacceptable dinner, because Sushi Topped With Tuna contains 100 calories, while Chocolate Almond Milk contains 50 calories, which results in the total of 150 calories, which is therefore unacceptable.
Instead, it shows me as…
Hi, (your name), but this would be an unacceptable dinner, because Sushi Topped With Tuna contains 284 calories, which Chocolate Almond Milk contains 15 calories, which results in the total of 299 calories, which is therefore unacceptable.
This isn’t right because I want Sushi Topped With Tuna to have 100 calories and the Chocolate Almond Milk to have 50 calories, and it seems like the values are pretty much the length of the lists which is something that I don’t want, and I have no clue as to how to fix this problem, despite the many attempts that I tried to do.
Thanks.

Can you show a bit more code, ideally on something like codepen or on GitHub or similar so can see the full context?

Suppose that I use Codepen for you to show a bit more code and to understand the full context, do I have to give you a link to the code that I wrote, or it is something that I have to copy my full code into this conversation?
Thanks. :grin:

HI,
Your

var foodCalories = getColumn("Fast Food Nutrition", "Calories");
var beverageCalories = getColumn("Beverages Nutrition", "Calories");

seems to have wrong info coming or might have wrong calories index and

foodCalories = foodCalories[x]; &   beverageCalories = beverageCalories[y]; 

is wrong usage… do not update global variable. use local variable instead.

it would be better if you can give more code over codepen/chat or by other mode.

Here, I provided the full code of what I have wrote so far. Please check it out. In terms of what you said about the code, I believe that

var foodCalories = getColumn("Fast Food Nutrition", "Calories");
var beverageCalories = getColumn("Beverages Nutrition", "Calories");

has the right information, because if you look for example at the following variables

var foodTypes = getColumn("Fast Food Nutrition","Type");
var food = getColumn("Fast Food Nutrition", "Name");
var beverageTypes = getColumn("Beverages Nutrition","Type");
var beverage = getColumn("Beverages Nutrition", "Name");

they provide the correct information through the usage of filtering into separate lists, but as for this code

foodCalories = foodCalories[x]; & beverageCalories = beverageCalories[y];

, this seems that the result might be due to being the index produced, which is something that I don’t want, which I don’t know what to do at this point.
Thanks. :slight_smile: