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
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:
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.
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.
@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
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?
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:
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):
Can you click the SHARE button and then COPY LINK TO PROJECT
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:
thank you!
Madeline