Mostly this works now. I uncommented all the code that you took out to debug. I also removed event as a parameter to get rid of all those yellow triangles. While it is true they are just warnings, it is also true they are sometimes useful and best to remove them all.
I moved the 4 text labels around so it doesn’t overlap the 4 radio buttons. Testing shows it can put things into the database. That’s good.
You have a bit of an initialization problem. One programming proverb I like is: Once and only once. People forget the first part a lot, the once part. So let’s include something you didn’t say and create a function.
function learnNames () {
currentNameIndex = -1;
score = 0;
tries = 0;
learnNextName();
}
This function says that the first name is different than the following name. And it is because we need to do some initialization. So we call this function to start the guessing game instead of learnNextName
.
Let’s look at the guessing game portion.
else {
randomIndex = i;
while (randomIndex === i)
randomIndex = randomNumber(0, numStudents-1);
setText (nameLabels[i], goesBys[randomIndex]+ " " +lasts[randomIndex].substring(0,1)+".");
}
There are some problems here. First off if there is only one student in the database you have an infinite loop. The second thing I see is you expect there to be 4 and only 4 students. I think maybe you meant to have randomIndex = currentNameIndex
. So we can try that.
Next we see if((correctNameIndex == 1) && (getChecked (nameRadios[correctNameIndex]))) {
That first clause is only going to work 25% of the time so lets take that out.
Let’s also get rid of a couple lines and create a function with the duplicated code:
function shortName(n) {
return goesBys[n]+ " " +lasts[n].substring(0,1)+".";
}
Now we see an error. setText(nameLabels[i], shortName(i));
When we substitute the function we see that i
is not the correct index and we change it to setText(nameLabels[i], shortName(currentNameIndex));
And that seems good, but…
Now we have an out of bounds problem. Looking at the code we see: if (currentNameIndex == numStudents)
. That is a problem because currentNameIndex
starts at 0. I know that you know this because you accounted for that elsewhere in the program. So change that to if (currentNameIndex === numStudents - 1)
I think you have a naming problem. You have correctNameIndex
, currentNameIndex
, and randomIndex
. If you change these to correctRadioButton
, currentStudent
, and randomRadioButton
you wouldn’t have the same kind of confusion.
I think that is enough for you to chew on for now. Let us know how it is going.