All puzzles
Per page
H
List · Index · Stay on the last item

Next word

Each click on the nextButton button shows the next word of the words list in the wordLabel label. Once the label shows the last word, more clicks keep it on the last word.

After 5 clicks, the label shows fox.

10 strips: H1 to H10

setText("wordLabel", words[index]);
H1
var words = ["cat", "dog", "fox"];
H2
if (index > words.length) {
H3
onEvent("nextButton", "click", function() {
H4
}
H5
});
H6
if (index > words.length - 1) {
H7
index = index + 1;
H8
index = words.length - 1;
H9
var index = 0;
H10

Answer key: H. Next word

Order: H2, H10, H4, H8, H7, H9, H5, H1, H6. Extra: H3

Lines with the same mark can go in either order.

var words = ["cat", "dog", "fox"];
var index = 0;
onEvent("nextButton", "click", function() {
	index = index + 1;
	if (index > words.length - 1) {
		index = words.length - 1;
	}
	setText("wordLabel", words[index]);
});

Extra strip: if (index > words.length) {. The index of the last item is words.length - 1, which is 2. With > words.length, index can reach 3, and words[3] is undefined.