Question
Swift Arrays: Write and test the code in Swift that meets the following requirements. Declare a variable called colors that is an array that holds
Swift Arrays: Write and test the code in Swift that meets the following requirements.
Declare a variable called colors that is an array that holds the string names of a collection of colors and is initialized with the following color names: red, orange, yellow, and pink.
Remove the last item from the colors array using an array method. (This will remove "pink".)
Add "green" to the end of the colors array.
Create a constant called numColors and set it equal to the number of colors in the array. (Use the computed property available on the array that provides the number of items in the array.)
Print the number of colors in the array using the format: number of color =
Print the string "colors:"
Iterate through the colors array using a for-in loop and print out each array element.
Declare a constant called moreColors that is an array that is initialized with the following color names: blue, purple
Declare a variable called allColors and assign it the combined elements of colors and moreColors. The colors elements are to come first and the moreColors elements are to come second in allColors.
print the string "all colors:"
Iterate through the allColors array using a for-in loop and print out each array element.
The output of your program should look like the following: number of colors = 4 colors: red orange yellow green all colors: red orange yellow green blue purple
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started