arguiot/DisplayJS

The Core language wiki has strange code example

Poetro opened this issue · 3 comments

In the The Core language page, there is a code example:

// create DisplayJS instance
var $ = new DisplayJS(window)

var lines = [
	"First line",
	"Second line",
	"Third line"
]
var text = $.select(".text")

for (let i in lines) {
	$.append(text, lines.indexOf(i) + " " + i + "<br>")
}

What is the intention with using lines.indexOf(i)? It always returns -1, as the index is never in the list of lines. Maybe it wanted to be lines[i]?

Well, i is not the index but the value, so, to have the index, I’m taking the indexOf the value.

When I run the following code:

var lines = [
	"First line",
	"Second line",
	"Third line"
]

for (let i in lines) {
	console.log(lines.indexOf(i), i, lines[i])
}

I get the following output:

-1 "0" "First line"
-1 "1" "Second line"
-1 "2" "Third line"

Ok, I didn’t knew that, because I actually learned for...in loops from Python 🐍 and I guess they are different from JavaScript, I’ll fix that 😊.