Code will not work

console.log(supplies [3]);
I cannot get this code to find the element of the array when the array is in the dataset? Can anyone tell me what might be going on?

TIA

It would help to see the code, but first try making sure:

  1. The program is running that line code
  2. make sure the variable supplies is defined
  3. check to see if supplies[3] is defined (remember, array[3] refers to the 4th element of an array)

I did all of that and if I ask for the number of items in the array, I get 4 but if I ask it to tell me what is in each position it says undefined. I don’t have all the code and one thing I do not like about Code.org is that we cannot interact with the student code.

@stephany_alhajjaj

It looks like you have a space between supplies and [3] in your original question. Could that be the issue?

Mike

No there is not a space, I wish there was though.

HI @stephany_alhajjaj,

@letti42 is correct - there needs to be 4 items in your list.

var supplies = [“water”, “sleeping bag”, “tent”, “matches”];

Let us know, If you get really stuck, you can send us a share link to your code.
~Michelle

1 Like

There are more than 4 elements in the array. The problem is that we have added them to the dataset and are not able to append the dataset or locate a single item in the dataset when using the code I described.

@stephany_alhajjaj
Hmmm…It is hard to pinpoint what is going on without seeing the code. Here is some code that shows a list with supplies and console.log(supplies[3]) in App Lab. I also appended a list item and then printed the item as well as printed the list. Is your code similar to this? Just trying to pinpoint what might be going on.
~Michelle

onEvent(“dropdownNames”, “change”, function( ) {
var index = 0;
names = getText(“dropdownNames”);
for (var i = 0; i < names.length; i++) {
index = i;
}
if (names == names[i]) {
cost = costs[i];
food = foods[i];
}
});

The list is in a dataset not written in the code. Is this something that code. org does not allow?

@stephany_alhajjaj,

In your code, I will assume that names comes from the dropdown. You need to use the getProperty block to create a names list in the dropdown. I’ve revised my possible solution so that it creates a list from the dropdown and traverses the list to check to see if a name exists (which I think is one task your code was trying to accomplish). If the name does not exist, it adds it to the dropdown. If it is a duplicate, it puts a message in the textbox. There are probably several other ways to accomplish this…this is just what I came up with.

I am not sure what the cost and foods references in your code are referring to. If they also exist in dropdowns, perhaps this sample app will help you solve that part on your own. If this isn’t the direction you were trying to go, give us some more information on what problem you are trying to solve. Hope this is helpful.

~Michelle

The information is in a data set. You have the dropdown and if the name is not there you add the name, if you find the name you go and input what they are bringing and the cost into the dataset. If you want to know what the person is bringing then what we want to happen is for the information to be retrieved and displayed from the dataset. Currently when we try to output the list the last item in the list is always undefined.

There are a few reasons I can think of that the last list item may be undefined. I’m not sure if these will help but here is a checklist I go through when addressing this with students:

  • Does the program recognize the dataset as a list? Can you print out your list? if not, search the forum for “undefined”. There are several ideas in there for debugging that error. I am still not super clear on where you are accessing your dataset so step one may be to make sure your list is initialized.
  • Did the item actually get added - can you console.log and see the item?
  • If the list item did not get added - why? Did you use the correct code? I think you are using App Lab in which case thinking about was insertItem (replaces a list item) used vs appendItem (adds it to the end of a list).
  • Did you traverse the whole list? (i=1 vs i=0 and .length vs .length-1).

Those are a few tips. Without seeing the project, it is difficult to give more specific advice.

Michelle

It does print the entire list, it just always has an undefined at the bottom. This is all the code.

var names = [“Nancy”, “Andrew”, “Susanna”, “Walton”, “Beckah”, “Anabella”, “Darius”, “Jerrard”, “Elizabeth”];

var foods = getColumn(“Thanksgiving Dinner - Planning”, “Items Supplied”);

var costs = getColumn(“Thanksgiving Dinner - Planning”, “Cost of Items in $”);

var selectName;

var food;

var cost;

onEvent(“buttonPlan”, “click”, function( ) {

setScreen(“screenNameandItems”);

});

onEvent(“dropdownNames”, “change”, function( ) {

var index = 0;

selectName = getText(“dropdownNames”);

for (var i = 0; i < names.length; i++) {

if (selectName == names[i]) {

index = i;

cost= costs[i];

food = foods[i];

}

}

});

onEvent(“dropdownNames”, “change”, function( ) {

names = "Hello, " + (getText(“dropdownNames”) + “!” + “\n”);

setText(“itemOutput”, names);

CostofFood();

FoodforPerson();

updateScreen();

});

function CostofFood() {

if (food == “Buttered Bread/Rolls” || “Mashed Potatoes” || “Cranberry Sauce” ) {

cost = 15;

} else if (food == “Pumpkin Pie” || “Baked Mac & Cheese” || “Stuffing” || “Soft Drinks”) {

cost = 20;

} else if (food == “Green Bean Casserole”) {

cost = 30;

} else {

cost = 45;

}

}

function FoodforPerson() {

if (selectName == “Nancy”) {

food = “Buttered Bread/Rolls”;

} else if (selectName == “Andrew”) {

food = “Pumpkin Pie”;

} else if (selectName == “Susanna”) {

food = “Turkey”;

} else if (selectName == “Walton”) {

food = “Cranberry Sauce”;

} else if (selectName == “Beckah”) {

food = “Mashed Potatoes”;

} else if (selectName == “Anabella”) {

food = “Baked Mac & Cheese”;

} else if (selectName == “Darius”) {

food = “Green Bean Casserole”;

} else if (selectName == “Jerrard”) {

food = “Stuffing”;

} else if (selectName == “Elizabeth”) {

food = “Soft Drinks”;

}

}

function updateScreen() {

setText(“itemOutput”, "Hello, " + (getText(“dropdownNames”) + “!” + “\n” + "You will bring " + food + “.” + “\n” + “You will need $” + cost + “.” + “\n”));

}

Dataset and Screen(s):

Home Planning Screen

Name and Items Screen

Dataset (Thanksgiving Dinner - Planning)

Can you click the SHARE button Screen Shot 2022-11-30 at 11.09.13 AM and then COPY LINK TO PROJECT Screen Shot 2022-11-30 at 11.09.21 AM and paste the link to the project?

Some other quick observations. You have 2 onEvents that are the exact same thing:
onEvent(“dropdownNames”, “change”, function( ). So, the computer is probably only reading the 2nd one. All the actions should be in one event. Also, names is defined as a list at the very beginning of the code but then you change it to a text string with names = "Hello, " + (getText(“dropdownNames”) + “!” + “\n”); Those are two different data types. How could you fix this?

I am also going to reassign this to CS Principles. Good luck!!

The original author has already made major changes to her code so that her project works. Due that, I am sending you a link to another students code who is having a very similar problem. I really appreciate your help with this.

I am glad the first app you posted is working! I’m afraid I am not familiar with the data tab so many lines are unfamiliar to me. If you want someone to take a look and give you tips, you may need to repost with just this app. Make sure to categorize as CSPrinciples.

Good luck!
~Michelle

Hi @stephany_alhajjaj,
I went to take a look at your code, but am having a hard time following what your student would like to have happen vs what is happening. To catch me up, could you share:

  • What the student would like to see happen
  • What is happening instead
  • What they’ve tried in order to solve the issue

thank you!
Madeline

1 Like