My students didn’t quite grasp how the index is used in a for loop or how to manipulate it to get different output from their arrays. I used Lesson 12 as the start for this challenge and had them rewrite a changeColor() function to be called with two color parameters. Set alternating LEDs with one color and the rest with another color.
I also reviewed the following code snippets with them before they started the challenge.
//print even numbers 0,2,4,6,8
for (var i = 0; i < 10; i = i+2){
console.log(i);
}
//print odd numbers 1,3,5,7,9
for (var i = 0; i < 10; i=i+2){
console.log(i+1);
}
Instructions:
Remix the code using the link below and complete the task listed in the comments. Submit the link to your remixed app.
- CSD Challenge: Here is the code you created in Unit 6 Lesson 12.
- Rewrite the function changeColor() to take a second color parameter.
- Set the LEDs with odd numbered indexes to the first color.
- Set the LEDs with even numbered indexes to the second color.
- Here is how the function should work.
- If the user clicks the “Red” button all the color LEDs should be set to red.
- If the user selects “Red Green”, the second, fourth, sixth, eighth, and tenth LEDs should be red; the first, third, fifth, seventh, ninth LEDs should be set to green.
- You MUST use ONLY one for loop in the changeColor() function to set the colors of the LEDs.
Do This:
- Add event handlers for the new UI buttons and call the changeColor() function with the appropriate values.
- Don’t forget to change the other calls in the starter code to changeColor() function to include the second parameter.
CSD:U6L12-Challenge - App Lab [Starter Code Link]
(https://studio.code.org/projects/applab/lvTOgHS06MnmAmWoTK8x5NJ8q2_tdEqxSrjOpzzGhIs)
#csd-unit6-lesson12