Meghana p
Meghana p

Reputation: 11

Flex- Action Script programming Language

I am new to Action Script programming Language, and I want to create the following number pattern:

1 2 3 4 5
1 2 3 4
1 2 3
1 2
1 

How can I code this pattern in Action Script Language?

Upvotes: 0

Views: 220

Answers (4)

Stack820
Stack820

Reputation: 13

This is my code to create a pattern like that:

for (var i:int = 5; i >= 1; i--) { // represent the row
    trace("1"); // print first element
    for (var j:int = 2; j <= i; j++) { // represet the line
        trace(" " + j.toString()); // print space and a number
    }
    trace("\n"); //next line
}

Upvotes: 0

esdebon
esdebon

Reputation: 2739

//Here the variable to build the string to "print"
var str:String="";

//We need two loops
//The first to change the row

for (var lim:int= 5; lim > 0; lim--) {

    //Reset the string each new row
    str="";

    //And here the scond to build the string to "print"
    for (var num:int= 1; num <= lim; num++) {
       str+=num+" ";
    }

    //Here "print" the string
    trace(str);
}

Upvotes: 0

Alexander Shcheglakov
Alexander Shcheglakov

Reputation: 157

 var x:int = 5;
 while(x > 0) {
      var output:String = "";
      for(var i:int = x; i > 0; i--) {
          output = i + " " + output;
          x--;
      }
      trace(output);
 }

Upvotes: 0

paxdiablo
paxdiablo

Reputation: 882606

I'm surprised anyone's starting to learn ActionScript (assuming you're using it for Flash) when the rest of the planet is moving toward HTML5 but, to each their own :-)

The algorithm you want for that sequence is relatively simple, and should be simple to convert into any procedural language:

for limit = 5 to 1 inclusive, stepping by -1:
    for number = 1 to limit inclusive, stepping by 1:
        output number
    output newline

That's basically it. Based on my (very) limited exposure to AS3, that would come out as something like:

for (var lim:Number = 5; lim > 0, lim--) {
    for (var num:Number = 1; num <= lim, num++) {
        doSomethingWith(num);
    }
}

Upvotes: 1

Related Questions