I see this type of error A LOT when we are using Lists:
setProperty() id parameter refers to an id (“txtError”) which does not exist. You should be able to find the list of all the possible ids in the dropdown (unless you created the element inside your code).WARNING: Line: 10: setNumber() value parameter value (90,100,85,77) is not a number.WARNING: Line: 10: setText() text parameter value (90,100,85,77) is not a uistring…]
Can someone explain please? Thank you in advance!
What I’ve tried: [replace with a detailed description of what you’ve already tried to do to solve the problem]
You have this line of code
var gradeList = [90, 100, 85];
and then this
setNumber("gradeOutput", gradeList);
Think about what type of parameters setNumber
expects. Try putting console.log(gradeList);
in front of that call to setNumber
to see what you are sending it.
1 Like
Ok thanks! It’s a list (as expected) [90, 100, 85, 77] So how do you display the lists in the labels then? What type is it? I have tried setText and set Property… same error.
I also have looked through code.org Lesson U5 L3 and all of the example problems print out to the console… I am not seeing one that shows a list in a label?
I am guessing I need to loop and add each element to the label output… one at at time?
Thanks for your time and help.
Go back to setText
. (Look at line 4.) App Lab is really good at changing things into strings.
One reason you might want to use a loop is to format it differently from what App Lab does.
I agree that code.org does not show how to print a list to a screen. There are 2 ways, but note, you have to convert numbers to string. Just google it. It will not print numbers to a screen. You have to convert them first.
Here is my list.
var names=[“Lisa”, “Leslie”, “Scott”, “Craig”, “Kent”, “Amy”, “Kris”, “Joel”];
It can’t print this to screen because there are 8 strings, you need to convert this to 1 string.
Here are ways to print the list to a screen:
setProperty(“outputScreen”, “text”,names.join("\n");
This prints to a screen each item of a list on a separate line using setProperty. It lists the items vertically one by one. This joins the names together and gets rid of the ""s. It is now 1 string with the names joined and each on a separate line. So now the list looks like this:
“Lisa
Leslie
Scott
Craig
Kent
Amy
Kris
Joel”
setText(“outputScreen”,"\n"+names);
This prints a list to a screen using setText and it lists the items horizontally. It doesn’t do a join. It looks like this:
“Lisa, Leslie, Scott, Craig, Kent, Amy, Kris, Joel”
It is 1 string which is why you can print it to a screen.
I hope this helps.
1 Like
I don’t know how to explain it in English, but this way the error stops here, if you want to study how it turned out.
I just know that you really have to transform everything into text, since you get the information from the output and there’s no way not to see it as text. So it gives error as number and as text. Because this is not clear in the code.
There must be easier ways to work this out… that’s mine.
setText("gradeOutput", getText("gradeOutput") + gradeList[(gradeList.length - 1)]);
Unfortunately, that won’t work. You are set up to add grades, but not remove them. What I recommended over a year ago was this.
setText("gradeOutput", gradeList);
Since then it has started to be flagged with a warning. You could use the toString() method of an array to reassure the lint program it is what you intended, but we don’t cover that in CS Principles. You could ignore the warning. It works. That seems like a bad thing to teach.
So now you must use a loop. I would recommend creating a new function like this.
function setTextFromArray (id, values) {
var text = "";
if (values.length > 0) {
text = text + values[0];
if (values.length > 1) {
for(var i = 1; i < values.length; ++i) {
text = text + ", " + values[i];
}
}
}
setText(id, text);
}
Teaching them to extend the existing abstraction is a good thing. And the function is based on the list reduce pattern here Code.org. Though I prefer a modified pattern as given here Everything You Wanted to Know About App Lab Arrays, But Were Afraid to Ask.
Thanks a lot for the info. As you pointed out, the key to convert a list to a text is to use the join() function, such as names.join(" ");