As pointed out in the instructions, i am using the rectangle function only once. But, i am struggling with the color variable. Because i am declaring the rectangle function on the outer side of my repeat loop, the color variable is getting out of scope. Since, we have different colours in different if and else statements, i am struggling on how do you get them into the single rectangle function.
// TODO: Draw the 20 stripes
let left = 0
let top = 0
let width = 5
let height = 100
let canvasSize = 100
repeat(canvasSize / 5){
if(left === 0 || left === canvasSize - width){
let color = “purple”
}
else if(left % 10 === 0){
let color = “yellow”
}
else if(left % 4 === 3){
let color = “green”
}
else if(left % 5 === 0){
let color = “blue”
}
rectangle(left, top, width, height, color)
left = left + 5
}
Sorry for not converting code into pre-formatted text here. My page is freezing when i am trying to convert my code into preformatted text here. i have posted this under the bugs and problems topic
Rather than creating a new color variable each time, how about if you create one color variable at the top, with the other variable, and then update it in each if/else block? That’s the more normal way to do this.
You could create the variable inside the repeat loop or with the other variables at the top. Both are acceptable. And you can give it any value as a default, but the most normal would be to do let color = ""
P.S. Thank you for trying to use codeblocks - I updated it for you
Got it, solved the exercise . Thank you @iHiD for having the strict condition of using the rectangle function only once in the code. It forced me to think a bit out of the box and also revise the updating of variable thing. Just looking back through our exercises, we used the same thing of updating the variable in an if/else block in the digital clock exercise as well. The only difference was that there was no repeat loop in the digital clock exercise.
Ah nice memory. Yes! That’s where I first introduced that pattern when I did the bootcamp teaching. I’ll remember to include that in the deep-dive videos when I finally get to them. Thanks for the reminder!