9.10. Studio: Loops¶
Now that we've launched our shuttle, let's use loops (iteration) to automate some tasks.
Fork this repl to start coding. Before you dive in, you might notice that we have several files and folders inside.
Your work will go into
solution.js, but please feel free to explore the program and please don't edit anything outside
9.10.1. Part A: Put dinner together¶
solution.js, use the variables provided to store the following arrays.
['chicken', 'pork', 'tofu', 'beef', 'fish', 'beans']
['rice', 'pasta', 'corn', 'potato', 'quinoa', 'crackers']
['peas', 'green beans', 'kale', 'edamame', 'broccoli', 'asparagus']
['juice', 'milk', 'water', 'soy milk', 'soda', 'tea']
['apple', 'banana', 'more kale', 'ice cream', 'chocolate', 'kiwi']
mealAssembly(), write a
forloop to assemble
The meals must include one item from each category in the
The computer needs to know how many crew members to prepare food for and what ingredients. Consider creating a nested loop that will create a meal for each crew member and then add it into a larger collection of meals.
Each ingredient can only be used ONCE.
Add each meal to the
mealsarray once it is assembled.
To test your own solution, scroll down to another function defined in this file called
runProgram(). Uncomment the lines that call the
mealAssemblyfunction and print the result. Use the repl.it 'Run' button as usual to see your results printed to the console.
9.10.2. Part B: Collect User Input¶
askForNumber() to add user input and validation.
- Using a
whileloop, ask the user to select the number of meals to assemble. Validate the input to make sure it is an integer from 1 - 6.
- Save the result to the
numMealsvariable returned by the function.
- Test your solution to this part by returning to the
runProgram()function in the file and uncommenting the section labelled for testing Part B.
9.10.3. Checking Your Work¶
If you want to make sure that you have checked all the boxes, run the following command in your shell on replit.
This command runs the Jasmine tests that are checking your work for you. If you have a test that fails, check out the name of the test to get a hint as to what you are missing. If you need a refresher on how running the tests works, check out the appendix on Tested Code.
9.10.4. Bonus Mission¶
Working and living aboard this amazing space shuttle requires you to pay the utmost attention to cybersecurity. Once you are done working on the meal system, you are prompted to create a new password that will be used for the next 24 hours. Having run out of strong password ideas, your shuttle captain has encouraged you to make a password generator for yourself.
Write your code inside
forloop that combines the two strings together, alternating the characters from each source, and saves the combined string to the
string1 = "1234"and
string2 = "5678", then the output will be "15263748".
string1 = "ABCDEF"and
string2 = "notyet", then the output will be "AnBoCtDyEeFt".
string1 = "LoOt"and
string2 = "oku!", then the output will be "LookOut!".