2 Replies - 2587 Views - Last Post: 21 May 2013 - 06:44 AM

#1 lillemakken  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-May 13

Faculty of a Number (Almost solved!)

Posted 20 May 2013 - 10:21 AM

Hello!

So I have this task where I'm going to create a function that calculates the faculty of a number and output the answer:
i.e faculty of 4 = 1*2*3*4 = 24.

This is my code so far, it's almost solved, but there is a detail that I'm missing which would make it all much simpler I suspect.

function fakultet(tall:int) {
	
	var teller:Number = 1;
	var number = tall*teller*(tall-1);
	while(teller <= tall) {
	teller++;
	trace(teller*number);
	}
}

fakultet(5);



This code will output the following numbers:
40
60
80
100
120 <-- And THAT is the only one number I want to see in the output window for "5" when I run my code.
That is the faculty of "5", so I want that last number for any given number I type in to be the only number to appear.

If I switch out "5" with "4", it will say 24 at the bottom. That is the number I want to come alone. Help is much appreciated.!

- lillemakken, 1st post.

Is This A Good Question/Topic? 0
  • +

Replies To: Faculty of a Number (Almost solved!)

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,845
  • Joined: 02-June 10

Re: Faculty of a Number (Almost solved!)

Posted 20 May 2013 - 10:48 AM

Line 7 is your output.
If you want it to only output the last computation then add a comparison.
For example:
If (tell == tall) {then it's the last computation so write your output}

Was This Post Helpful? 0
  • +
  • -

#3 lillemakken  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-May 13

Re: Faculty of a Number (Almost solved!)

Posted 21 May 2013 - 06:44 AM

Mmmm, not quite sure if I understood that 100%, almost though.. Are you saying I should add "else if" and make (teller==tall) ? If I change my initial "If", it won't output what I need?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1