4

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;
};
1
  • 1
    Read this - meaning of "falsy" and "truthy" values: sitepoint.com/javascript-truthy-falsy Non-Boolean values are converted to "true" or "false" when a Boolean value is needed, and how that is done depends on some rules. In other, more type-strict languages you could not do that but would be forced to declare explicitly whether you want "true" or "false". Commented Dec 6, 2013 at 20:18

4 Answers 4

4

You're close. Here's what the if should be:

if (array[i]) {
    truthyValues.push(array[i]);
}

Since you want to check for truthiness of each array element, just put array[i] in the if block. That gets the value of the array at i, and will evaluate as true if that value is truthy.

Then push not i - the index into array - but array[i], so that the actual value is in truthyValues.

Sign up to request clarification or add additional context in comments.

Comments

4

Just put the value you want to check is truthy in the if statement, since the if statement checks for truthiness.

if(array[i]){
  truthyValues.push(array[i]);
}

Comments

1

I think you can just use:

if (array[i]) { truthyValues.push(array[i]); }

Values like null will trigger false.

Comments

1
if (typeof array[i] !== "undefined" && array[i] !== null) {// push to array // }

most of these other answers will not work for falsy and existy values. You'll need to define what you intend to be existy and write a helper function for that. you could use the one I provided as a start and then roll your own.

4 Comments

He says "falsey" (sic) so I guess the exercise is to use the t-Boolean conversions built into Javascript, so being fuzzy is deliberate here, I think. You do what a good programmer would do - be explicit :)
@Mörre this should help him get started with being explicit in JS: zero.milosz.ca
What's "existy values"? Never heard that term before. The only occurrence on the web is your comment according to Google :)
I don't find "existy" on that webpage. I don't think inventing a new word when the person asking the question already has enough to do to understand "truthy" and "falsy" is not a good idea, especially when there is no one but you using it. By the way, even the variable names in the example say "truthy" - as I said, it seems the exercise is deliberately using this JS "specialty", likely for teaching purposes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.