FranCarstens
FranCarstens

Reputation: 1403

Why does this code log return false?

I can't figure out why this snippet logs false. I'm convinced it should log to true. What am I doing wrong?

var hasElb = function(string12b,char12b) {
    var el = [];
    for (var i = 0; i < string12b.length; i++ ) {
        if (string12b[i] === char12b) {
            el += char12b;
        }
    }
    if (el[0] === char12b) {
        console.log(true + " el[0] = " + el[0] + " and char12b = " + char12b);
    }
    else {
        console.log(false + " el[0] = " + el[0] + " and char12b = " + char12b);
    }
};
hasElb([1,3,5,7,9,11],7);

Upvotes: 1

Views: 60

Answers (1)

Pointy
Pointy

Reputation: 413717

To add an element to an array, you use .push(), not +=:

    if (string12b[i] === char12b) {
        el.push(char12b);
    }

Upvotes: 1

Related Questions