Reputation: 1267
So I can across this neat helper function and was getting confused with the syntax. There is a variable (bool declared as true, which appears to be an array. Its using the bracket object notation, but then going to compare if bool[j] or [i] is true yet, there is nothing adding [i] or [j] to the object map.
const helper = (word, words) => {
let bool = [true];
//if you console.log(typeof bool) returns object ??
//This comes out as an Obj at first glance I thought it was an arr, but its bracket notation
for (var i = 1; i <= word.length; i++) {
for (var j = 0; j <= i; j++) {
//how is bool[j] being evaluated? or i if its an obj?
if (bool[j] === true && words[word.substring(j, i)] === true) {
bool[i] = true;
break;
} else {
bool[i] = false;
}
}
}
return console.log(bool[word.length] ? true : false);
}
helper('aa', ['aa', 'aabb', 'someotherword']);
Upvotes: 0
Views: 64
Reputation: 23955
In JavaScript, arrays are an instance of Object and their type will register as such.
A statement like
foo[i] = 'bar'
will assign the value 'bar' at the i
th zero-based index of the array (or object) foo
. That's happening in the for
loops in the code you posted.
var a = []
console.log(a instanceof Object)
a[3] = 4
console.log(a)
Upvotes: 2