Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Surinder Singh
Surinder Singh
274 Points

How to print this pattern in swift code.

Write a program that takes an integer (in a variable) and displays, using asterisks, the figure below. For example, if the input is 5, the program should display.

    *
  * *
* * *



* * *
  * *
    *

well , I did this in java but it is little difficult to grab the syntax for me. I want you to explain the code in a proper manner . Thanx in advance

Kyle Johnson
Kyle Johnson
33,528 Points

Can you answer this question to help me understand your question more.

Do you want a function and takes 1 input parameter (5 in your example) and then prints out a String of asterisks in the shape of a triangle?

2 Answers

Surinder Singh
Surinder Singh
274 Points

i just want to print simply a pattern of stars

 *

**




** *

Like this way

Kyle Johnson
Kyle Johnson
33,528 Points

Try this!

func newFunc(someNumber: Int) {
     for index in someNumber {
          print("*") // There might be a code for the * symbol.
     }
}

// Call the function
newFunc(someNumebr: 5)