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.
Ben

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

2 Likes

I had problems with this issue. My experience was that the best workaround was to create the ternary early on before entering too much other stuff. Hope this helps someone!

1 Like

My issue in learning this is I don’t know when breakfast = menu1 and lunch = menu2 were set. I don’t know how it’s comparing the breakfast menu and the lunch menu, only that it’s comparing breakfast and lunch. I don’t know if I’m just missing the rest of the code in the exercises or if I’m just not figuring out how this works.

Hey there, menu1 and menu2 are the parameters for the function, and breakfast and lunch are the parameters.

Let’s look at a simpler example:

function add(num1, num2) { // num1 and num2 are parameters
    return num1 + num2;
}
add(3, 5) // 3 and 5 are the arguments

When the function above is called like this: add(3, 5), num1 is set to 3 and num2 is set to 5. The function will then return 3 + 5.

In this puzzle, menu1 and menu2 are the parameters. When the function is called like this: longer(breakfast, lunch), menu1 is set to breakfast and menu2 is set to lunch.

Hope this makes sense! Let me know if you have any questions.
Ben

1.Why there is no “playground " in this exercise?
2.Haw I can add : " Miguel- choose breakfast/lunch” .
For "my Miguel " he dont won’t to no all the list he just wont to eat
3. And let say that he is Muslim or Jewish. And doesn’t eat pork . He is lazy to read all the list , he want that javascript will look and worn him .

Actually I was thinking after reading the answer it only shows the properties but how can I know if this belong to lunch or breakfast?

I don’t get what I’m supposed to do to complete it