Array dimension


#1

Hi, everybody.

I have a question about next code. As you can see, the first variable is string, but, it is changed to an array in the condition "if", so, why does it occcur?


var text text = "Blah blah blah blah blah blah Yeison \
blah blah blah Eric blah blah Eric blah blah \
blah blah blah blah blah Eric";

var myName = "Yeison";
var hits = [];

// Look for "E" in the text
for(var i = 0; i < text.length; i++) {
	if (text[i] === "Y") {
		// If we find it, add characters up to
		// the length of my name to the array
		for(var j = i; j < (myName.length + i); j++) {
			hits.push(text[j]);
		}
	}
}

if (hits.length === 0) {
	console.log("Your name wasn't found!");
} else {
	console.log(hits);
}


#2

Invalid: unexpected identifier error.

var text = "...";

It is not change to an array, though it may appear as such owing that the indexing is the same for all iterables.

string[i]

array[i]

object[i]

We are working with strings, only.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.