Challenge 4: Draw a Triangle app

Following the instructions, my code looks like this

var drawPixel:String = “X”
var height:Int = 5
var tempRow:String

for columnPixel in 1…height {
tempRow = ("")
for _ in 1…columnPixel {
tempRow == drawPixel
}
print(tempRow)
}

I need the nested For Loop to output the drawPixel columnPixel times, but how do we do that?

Hi @pukoh

Is this the output you are trying to get?

X
XX
XXX
XXXX
XXXXX
1 Like

You were pretty close to getting it right.

If you needed to use a nested Loop then this is what your code should have looked like:

for columnPixel in 1...height {
    tempRow = ""
    for _ in 1...columnPixel {
        tempRow += drawPixel
    }
    print(tempRow)
}

Without using a nested loop you could have done it like this:

for _ in 1...height {
    tempRow += drawPixel
    print(tempRow)
}

Ahhhh i didn’t know you can do the += for strings too!

thanks~