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
Additional Code (hidden code that runs before the puzzle’s code):

let breakfast =['rice','omelette','eggs'];
let lunch =['potato','tofu','edamame','gyoza','calamari','salad','oyster','shrimp','eel','chicken','beef curry','katsu pork','udon'];

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.

Hi, I tried the solution on the screenshot. The output seems to be correct, but I don’t really understand what the suggestion says. Could it be I should eliminate the else statement, just write:
If menu1.length < menu2.length
Return menu2
And with that the function know if it’s the other way around then the test is false?

Got it! Not only did I erase the 2nd return in the example above, the else statement is not needed. You write “return menu1” outside the if brackets. Well, the one in this screenshot uses menu1 > menu2. But still, I got it