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?


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 is that we cannot interact with the student code.


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


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.

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.

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.

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?


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.