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'];