Blockland Forums > Modification Help
Is there a way to multiply together the output of a loop?
<< < (2/4) > >>
Headcrab Zombie:
Yeah, set %answer = 1 before the loop.
Also, you could just use %answer *= %a, its the same thing as doing %answer = %answer * %a; just less typing
Works with -=, +=, and /= too
Jetpuff:
Ok thanks all, Headcrab was right.

%answer=1;
for(%a=1; %a<10; %a++)
{
%answer=%answer*%a;
echo(%a);
}
echo(%answer);

Now can someone show me how to write a chat command and explain all the syntax to me?

function serverCmdFactorial

(also %a<10 is going to change to %a<=%num)


EDIT: Just tried this, Syntax Error

function serverCmdFactorial(%client, %num)
 {
  %answer=1;
  for(%a=1; %a<=%num; %a++)
   {
    %answer=%answer*%a;
     {
      messageClient(%client, '', %num @ "! is" @ echo(%answer) );
     }
   }
 }
MegaScientifical:

--- Quote from: Jetpuff on November 20, 2010, 03:43:28 AM ---Ok thanks all, Headcrab was right.

%answer=1;
for(%a=1; %a<10; %a++)
{
%answer=%answer*%a;
echo(%a);
}
echo(%answer);

Now can someone show me how to write a chat command and explain all the syntax to me?

function serverCmdFactorial

(also %a<10 is going to change to %a<=%num)


EDIT: Just tried this, Syntax Error

function serverCmdFactorial(%client, %num)
 {
  %answer=1;
  for(%a=1; %a<=%num; %a++)
   {
    %answer=%answer*%a;
     {
      messageClient(%client, '', %num @ "! is" @ echo(%answer) );
     }
   }
 }

--- End quote ---

You have random {} marks in there with no if/else/etc. Also, an extra { mark. Also, you put echo into the message... Dang dude... I'm sorry, and I know you're just starting out, but you have so little understanding... Sorry...


--- Code: ---function serverCmdFactorial(%client, %num) {
%answer = 1;
for(%a = 1; %a <= %num; %a++) {
%answer *= %a;
messageClient(%client, '', %num @ " is " @ %answer @ ".");
}
}
--- End code ---

I tried to throw that together... but all that would do is give the list of numbers because 1 times a number is that number.
Jetpuff:
K thanks

I just wrote this to see if I could write a chat command alone
Simply enough it squares a number. Is "^" usable syntax?


function serverCmdSquare(%client, %num)
 {
  %blackpeople=%num*%num;
  messageClient(%client, '', %num @ " ^2= " @ %blackpeople);
 }

I don't know when to use {}'s and when not to use them. I first put them after %num; to seperate messageClient. (Syntax Error) Is that because there was no loop for it to come after in this function?

EDIT: Oooh, why doesn't this work?

function serverCmdAdd(%client, %x, %y)
 {
  %sum = %x + %y;
  messageClient(%client, '', %x @ "+" @ %y "=" %sum);
 }
Space Guy:
Second one, you're missing an @ symbol between the %y, "=" and %sum.

You can also use the symbol SPC that automatically puts in a space when connecting strings:


--- Code: ---%x = 3;
%y = 4;
echo(%x @ "+" @ %y @ "=" @ (%x + %y));
--- End code ---
Output: 3+4=7


--- Code: ---%x = 3;
%y = 4;
echo(%x SPC "+" SPC %y SPC "=" SPC (%x + %y));
--- End code ---
Output: 3 + 4 = 7
Navigation
Message Index
Next page
Previous page

Go to full version