Problem with student code

[Posts in the debugging category need to have the following pieces of information included in order to help other teachers or our moderator team best help you. Feel free to delete everything in brackets before posting your request for debugging help.]

Link to the project or level: [replace with link to the curriculum or get the “Share” link to a project]
What I expect to happen: [replace with a detailed description of the behavior you’re trying to create]
What actually happens: [replace with a detailed description of what actually happens when the code runs including any errors or unexpected behavior]
What I’ve tried: [replace with a detailed description of what you’ve already tried to do to solve the problem]

https://studio.code.org/projects/applab/-zU6QumRIMrybFBdFvvTvZ8ttrVrgBhlrt9dhEjnKjAvar counter = index * -1;
What I want it to do: As soon the list shows zero names on the list, it should move to screen 3.
What actually happens: When I get to the end of the list of names it does not show empty field, but undefined.
What I’ve tried: Tried to use a if/else statement, tried a separate variable to count, general if index is = to number of lists.

Code below:

Thank you

var namesArray =
onEvent(“text_input1”,“change”,function(){
var names = getText(“text_input1”);
console.log(names);
namesArray = names.split(",");
console.log(namesArray);
setProperty(“label2”,“hidden”,true);

});
function getRandomName(list) {
var index = Math.round(Math.random() * (list.length - 1));
var randomWord = list[index];
console.log(index);

console.log(randomWord);
setProperty(“label2”,“text”, randomWord + “\n is chosen”);
if (namesArray.length == index) {
setProperty(“label2”,“hidden”,true);
} else {
removeItem(namesArray, index);
console.log(“removed” + " " + randomWord);
console.log(namesArray);
}

}
onEvent(“button3”,“click”,function(){
getRandomName(namesArray);
setProperty(“label2”,“hidden”,false);
var counter = counter + 1;

});

//loading motion1
onEvent(“slider1”,“input”,function(){
setProperty(“radio_button” + getNumber(“slider1”),“hidden”,true);
//setProperty(“radio_button” + getNumber(“slider1”),“hidden=”,false);
});
// sets the screen
onEvent(“button1”,“click”,function(){
setScreen(“screen2”);
createCanvas(“canvas1”,320,450);

});

@sharon.deutscher - I am not sure why but I can seem to get to the code. It just hangs and says “this is taking longer than usual.” Can you check the link?

I couldn’t get to the project either. Here are some comments from just looking at the code.

var namesArray =
onEvent(

onEvent doesn’t return anything useful, probably returns undefined. You probably meant to initialize it to [ ].

You create a function getRandomName(list). You pass a list but then break that contract by referencing namesArray anyway.

You have a line of code if (namesArray.length == index). I could probably figure out what the code does eventually…but I shouldn’t have to. It makes very little sense at first glance. What were you trying to say here? If you are looking for an empty array, my first guess, then you should just say list.length == 0. That should be the very first thing you check in that function.

That function getRandomName(list) messes around with "label2". It shouldn’t do that. In any event (no pun intended) you are setting the "label2" to be shown after you call it any way.

onEvent(“button3”,“click”,function(){
getRandomName(namesArray);
setProperty(“label2”,“hidden”,false);
var counter = counter + 1;

});

Instead of setting the hidden property you could use the showElement and hideElement functions given you you by the kind people at code dot org.

If you can share the project in a way that we can all take a look I can give you more details on what is going wrong. Otherwise, try the general comments I have given.

Thank you for your help. We will see what happens today.