0

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?

4 Answers 4

1

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);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

with AS3 and AIR you can create apps for iOS and Android, the web is already surpassed.
0
 var x:int = 5;
 while(x > 0) {
      var output:String = "";
      for(var i:int = x; i > 0; i--) {
          output = i + " " + output;
          x--;
      }
      trace(output);
 }

Comments

0
//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);
}

1 Comment

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
0

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.