1. Determine whether the lasagna is done
- Use an if-statement to return a result based on some condition.
- The task can be best solved when checking for the two specific conditions first.
- Remember that missing parameters will be
undefinedand you can check whether a variable is
undefinedusing the strict equality operator
2. Estimate the preparation time
- Use a default parameter to set the average preparation time in case it was not provided when the function was called.
- Revisit arrays to find out how to determine the number of layers (length of the layers array).
3. Compute the amounts of noodles and sauce needed
- First, set up two variables to track the amount of noodles and sauce.
Define them with
letso you can change the value later in your code. Also, assign an appropriate initial value.
- Use a for loop to iterate through the layers.
- If you encounter a
'sauce'layer in your loop, increase the amount stored in the respective variable accordingly.
- Use an object to return both results.
4. Add the secret ingredient
- Revisit arrays to find out how to retrieve an element from an array and how to add something to the end of an array.
- The index of the last element in an array
a.length - 1.
- The function should not have a
5. Scale the recipe
- First, calculate the factor that you need to apply to all amounts based on the target portions.
- One way to ensure the arguments is not modified is to use a new object to save the results.
- Use a
for...inloop to go through all the entries in the original recipe and fill the object accordingly.