[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);
});