Author Topic: Is there a way to multiply together the output of a loop?  (Read 1314 times)

Loop: for(%a=1; %a<10; %a++)
if echoed: 1 2 3 4 5 6 7 8 9
Question: How do I multiply the output together?

(Yes it probably is a stupid question, I just started scripting)

Is it something like this you want?
Code: [Select]
for(%i=1;%i<=10;%i++)
{
    echo(%i);
    %num = %num * %i;
}
echo(%num);
Echoes the following:
1
2
3
4
5
6
7
8
9
10
3628800

Or did you mean this:
Code: [Select]
for(%i=1;%i<=10;%i++)
{
    echo(%i);
    %num = %num + %i;
}
echo(%num);
1
2
3
4
5
6
7
8
9
10
55
« Last Edit: November 20, 2010, 01:53:08 AM by Bauklotz »



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

I end up with zero. Why is that? Does %answer need a value?

I end up with zero. Why is that? Does %answer need a value?

Yes. It's like trying to multiply by nothing (zero).

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

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) );
     }
   }
 }
« Last Edit: November 20, 2010, 03:56:38 AM by 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) );
     }
   }
 }

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: [Select]
function serverCmdFactorial(%client, %num) {
%answer = 1;
for(%a = 1; %a <= %num; %a++) {
%answer *= %a;
messageClient(%client, '', %num @ " is " @ %answer @ ".");
}
}

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.

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);
 }
« Last Edit: November 20, 2010, 05:16:30 AM by Jetpuff »

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: [Select]
%x = 3;
%y = 4;
echo(%x @ "+" @ %y @ "=" @ (%x + %y));
Output: 3+4=7

Code: [Select]
%x = 3;
%y = 4;
echo(%x SPC "+" SPC %y SPC "=" SPC (%x + %y));
Output: 3 + 4 = 7

^ is a bitwise XOR.

You want mPow(a, b) (where a and b are replaced with numbers).

a @ b joins a and b as a string, SPC does the dame thing with a space between them, TAB with a tab, and NL with a newline.

{}s must be used when declaring a function or package, may be used during a new statement to set specific values for the created object, can be used with if, while, and for to allow any number of statements rather than exactly one, and is generally a syntax error otherwise. I might have missed a few valid uses.

K put in some @ signs, Still a syntax error...


function serverCmdAdd(%client, %x, %y)
 {
  %sum=%x+%y;
  messageClient(%client, '', %x @ "+" @ %y @ "=" @ %sum);
 }

Also if it helps, I want something to type in like

/Add 2 2

and then the message will say something like

2+2=4

Are you not allowed to use two variables (excluding %client) in a serverCmd function?

You're allowed to use as many as you like.

Show the entire code, because the syntax error is likely before the add function.

I read it in in console, still the same as what I posted.

The console will not show what is causing the problem.

It just shows you where it realized there's a problem.