# Code will not work

**URL:** <https://forum.code.org/t/code-will-not-work/37226>\
**Category:** CS Principles\
**Tags:** applab\
**Created:** [November 11, 2022, 7:43pm UTC](https://forum.code.org/t/code-will-not-work/37226 "2022-11-11T19:43:11Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![stephany\_alhajjaj](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@stephany\_alhajjaj](https://forum.code.org/u/stephany_alhajjaj)\
**Post date:** [November 11, 2022, 7:43pm UTC](https://forum.code.org/t/code-will-not-work/37226/1 "2022-11-11T19:43:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![letti42](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@letti42](https://forum.code.org/u/letti42)\
**Post date:** [November 11, 2022, 8:14pm UTC](https://forum.code.org/t/code-will-not-work/37226/2 "2022-11-11T20:14:43Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![stephany\_alhajjaj](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@stephany\_alhajjaj](https://forum.code.org/u/stephany_alhajjaj)\
**Post date:** [November 11, 2022, 8:21pm UTC](https://forum.code.org/t/code-will-not-work/37226/3 "2022-11-11T20:21:07Z")

</div>

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](http://Code.org) is that we cannot interact with the student code.

---

<div class="post-metadata">

**Author:** ![mwood](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mwood/32/4941_2.png) [@mwood](https://forum.code.org/u/mwood)\
**Post date:** [November 11, 2022, 9:00pm UTC](https://forum.code.org/t/code-will-not-work/37226/4 "2022-11-11T21:00:24Z")

</div>

@stephany_alhajjaj

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

Mike

---

<div class="post-metadata">

**Author:** ![stephany\_alhajjaj](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@stephany\_alhajjaj](https://forum.code.org/u/stephany_alhajjaj)\
**Post date:** [November 11, 2022, 9:07pm UTC](https://forum.code.org/t/code-will-not-work/37226/5 "2022-11-11T21:07:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![melynn](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/melynn/32/4925_2.png) [@melynn](https://forum.code.org/u/melynn)\
**Post date:** [November 11, 2022, 10:01pm UTC](https://forum.code.org/t/code-will-not-work/37226/6 "2022-11-11T22:01:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stephany\_alhajjaj](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@stephany\_alhajjaj](https://forum.code.org/u/stephany_alhajjaj)\
**Post date:** [November 28, 2022, 7:09pm UTC](https://forum.code.org/t/code-will-not-work/37226/7 "2022-11-28T19:09:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![melynn](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/melynn/32/4925_2.png) [@melynn](https://forum.code.org/u/melynn)\
**Post date:** [November 28, 2022, 7:38pm UTC](https://forum.code.org/t/code-will-not-work/37226/8 "2022-11-28T19:38:17Z")

</div>

@stephany_alhajjaj  
Hmmm…It is hard to pinpoint what is going on without seeing the code. [Here is some code](https://studio.code.org/projects/applab/lgP7pcPEunRFR5I_dJ-a_BeVwfaXI3dKlP2c1lnMBuE) 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

---

<div class="post-metadata">

**Author:** ![stephany\_alhajjaj](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@stephany\_alhajjaj](https://forum.code.org/u/stephany_alhajjaj)\
**Post date:** [November 29, 2022, 7:40pm UTC](https://forum.code.org/t/code-will-not-work/37226/9 "2022-11-29T19:40:09Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![melynn](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/melynn/32/4925_2.png) [@melynn](https://forum.code.org/u/melynn)\
**Post date:** [November 29, 2022, 11:47pm UTC](https://forum.code.org/t/code-will-not-work/37226/10 "2022-11-29T23:47:53Z")

</div>

@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](https://studio.code.org/projects/applab/lgP7pcPEunRFR5I_dJ-a_BeVwfaXI3dKlP2c1lnMBuE) 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

---

<div class="post-metadata">

**Author:** ![stephany\_alhajjaj](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@stephany\_alhajjaj](https://forum.code.org/u/stephany_alhajjaj)\
**Post date:** [November 30, 2022, 3:14pm UTC](https://forum.code.org/t/code-will-not-work/37226/11 "2022-11-30T15:14:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![melynn](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/melynn/32/4925_2.png) [@melynn](https://forum.code.org/u/melynn)\
**Post date:** [November 30, 2022, 5:27pm UTC](https://forum.code.org/t/code-will-not-work/37226/12 "2022-11-30T17:27:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stephany\_alhajjaj](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@stephany\_alhajjaj](https://forum.code.org/u/stephany_alhajjaj)\
**Post date:** [November 30, 2022, 5:33pm UTC](https://forum.code.org/t/code-will-not-work/37226/13 "2022-11-30T17:33:38Z")

</div>

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](https://lrsdorg0-my.sharepoint.com/:i:/g/personal/368165_students_lrsd_org/EZyy8NqMJ6lPgIWNlxJGOboBPzBxGv2_KU2v32WGUitneQ)**

**[Name and Items Screen](https://lrsdorg0-my.sharepoint.com/:i:/g/personal/368165_students_lrsd_org/ETTnNhjNihxJgIzlcabG4a8B3j2BGjMLLshzXWZ98SmWLw)**

**[Dataset (Thanksgiving Dinner - Planning)](https://lrsdorg0-my.sharepoint.com/:i:/g/personal/368165_students_lrsd_org/ERuIUjzf43JFsfLxbrafhrwBAZIg_jhzXT8sexNC-dSynQ)**

---

<div class="post-metadata">

**Author:** ![melynn](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/melynn/32/4925_2.png) [@melynn](https://forum.code.org/u/melynn)\
**Post date:** [November 30, 2022, 6:11pm UTC](https://forum.code.org/t/code-will-not-work/37226/14 "2022-11-30T18:11:29Z")

</div>

Can you click the SHARE button ![Screen Shot 2022-11-30 at 11.09.13 AM](https://us1.discourse-cdn.com/flex016/uploads/codeorgforum/original/2X/c/c5d32ccf636689d306d760a1deb3bd5186074767.png) and then COPY LINK TO PROJECT ![Screen Shot 2022-11-30 at 11.09.21 AM](https://us1.discourse-cdn.com/flex016/uploads/codeorgforum/original/2X/3/3c554ab20524e2869693e2a55a5335eb2a81eb78.png) and paste the link to the project?

---

<div class="post-metadata">

**Author:** ![melynn](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/melynn/32/4925_2.png) [@melynn](https://forum.code.org/u/melynn)\
**Post date:** [November 30, 2022, 6:38pm UTC](https://forum.code.org/t/code-will-not-work/37226/15 "2022-11-30T18:38:02Z")

</div>

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!!

---

<div class="post-metadata">

**Author:** ![stephany\_alhajjaj](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@stephany\_alhajjaj](https://forum.code.org/u/stephany_alhajjaj)\
**Post date:** [November 30, 2022, 8:03pm UTC](https://forum.code.org/t/code-will-not-work/37226/16 "2022-11-30T20:03:16Z")

</div>

> **[Check out what I made](https://studio.code.org/projects/applab/chLNp4aK-zX-RqWFVDd_I7aej-GOshDrykB4bW7Sj-E)**
>
> I wrote the code myself with Code.org

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.

---

<div class="post-metadata">

**Author:** ![melynn](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/melynn/32/4925_2.png) [@melynn](https://forum.code.org/u/melynn)\
**Post date:** [November 30, 2022, 10:11pm UTC](https://forum.code.org/t/code-will-not-work/37226/17 "2022-11-30T22:11:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![madeline\_r\_burton](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/madeline_r_burton/32/5037_2.png) [@madeline\_r\_burton](https://forum.code.org/u/madeline_r_burton)\
**Post date:** [December 1, 2022, 2:53am UTC](https://forum.code.org/t/code-will-not-work/37226/18 "2022-12-01T02:53:17Z")

</div>

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
