Push to an Array using a for loop

Viewed 618

im a JS newbie. Trying to figure out this problem below. I am stuck on the "if" statement and not sure exactly what to put in. Also im not sure if my "push" is setup right

// Define a function named `compact` that accepts an array and returns
// another array with all the falsey values removed from it. For example,
// if this array was passed in:
//   [1, 4, 0, '', undefined, false, true, null, {mj: 'mj'}, 'Hello']
// the function would return this, as a result:
//   [1, 4, true, {mj: 'mj'}, 'Hello']

var compact = function (array) {
    var truthyValues = [];
    for (var i = 0; i < array.length; i += 1) {
        if () {
            truthyValues.push[i];
        }
    }
    return truthyValues;
};
4 Answers
Related