Miguel's Choice explainer

Aim of the puzzle: Use non-specific instructions to complete a function.
Walk through of solution: This puzzle is the 2nd in a series of lessons that will teach you how to break down word problems and real-world challenges and solve them with code.

In the previous puzzle, you had specific instructions on how to create the longer() function. In this puzzle, however, you will read a word problem and recreate the longer() function without being given specific instructions.

To complete this puzzle, complete the longer() function so that it takes 2 arrays and returns whichever array is longer.
Sample code solution:
There are multiple ways to solve the puzzle.

function longer(a, b) {
    if (a.length > b.length) {
        return a;
    } else {
        return b;
    }
}
console.log(longer(breakfast, lunch));

Another Solution:

function longer(a, b) {
    return a.length > b.length ? a : b;
}
console.log(longer(breakfast, lunch));

Javascript Concepts: Functions, .length, Arrays

Why won’t this work? This method just always returns “false”:

return array1.length > array2.length ? array1 : array2;

If i just return the array, then it will return the array, but using ternary inside the function ALWAYS returns false. I don’t understand.

I tried using the exact code posted in the “Another Solution” above, but it still just returns “false”. So that solution isn’t correct. I don’t understand.

Hey there, can you post a screenshot of your code?

Hey there,

There is an annoying bug that affects how ternary statements are entered and is likely causing this problem. We’re working on fixing it, but the good news is we can work around it.

The bug is that invisible parentheses might be placed around your ternary, depending on what code is highlighted when you tap the ? : ternary key.

For example, let’s say you have:

array1.length > array2.length

If array2.length or even just the .length is highlighted when you tap the ? : key, then the code does the following:

array1.length > (array2.length ? ___ : ___ );

This is most likely the problem you are experiencing. Again, the parentheses aren’t being displayed, so you aren’t seeing them.

To fix the issue, make sure the entire array1.length > array2.length is highlighted when you tap the ? : key.

To highlight the entire expression, you’ll have to tap the space around the >. Then you can add the ternary.

Apologies for the wonkiness! We hope to have the issue fixed soon.

Let me know if you have any questions.
Ben