Breakfast or Lunch 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'];

``````
1 Like

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?

1 Like

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

2 Likes

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:
And with that the function know if itâ€™s the other way around then the test is false?

1 Like

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

3 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 Like

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 .

1 Like

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

I added what I think should be the autput

If breakfast. Length >lunch.length
Console.log("choose breakfast ")
Else
Console.log(â€śchoose lunchâ€ť)

Hey there, that looks very cool, but thereâ€™s one small problemâ€¦ what if you want your function to look at other menus, and not just `breakfast` and `lunch`?

For example, what if instead of importing `breakfast` and `lunch` into the puzzle, we imported a different array named `dinner` and another named `afternoonTea`. The function would then break, because it canâ€™t find the `breakfast` and `lunch` variables.

This is why we use the function parameters, `menu1` and `menu2`. That way, when you call the function like this: `longer(breakfast, lunch)`, `breakfast` gets stored in `menu1` and `lunch` gets stored in `menu2`. If you call the function with other arguments, the function will still work.

For a refresher on parameters vs arguments, try taking a look at this post.

Hope this helps! Let me know if you have questions.
Ben

2 Likes

Did it work???

Whyâ€¦the heck am returning `menu2` a second time, outside of the `if` statement if my `else` statement already accounts for this??

Use this screen shot. This is a tricky Quiz

2 Likes