FizzBuzz
- From 1 to 100 output text adhering to the following
- Multiples of 3 echo (or hint in the VBS2/ArmA case) "Fizz"
- Multiples of 5 echo "Buzz"
- Multiples of 3 and 5 echo "FizzBuzz"
- Everything else, just echo the number.
What this demonstration is not, is it's not a perfect implementation or the only implementation, but it might help with understanding some simple concepts in SQF.
Now for the SQF:
Hopefully this seems pretty simple to begin with, it should be largely self explanatory. If not, let me know and I'll try and cover it better in later posts!// FizzBuzz// VBS2 Script responding to the FizzBuzz problem// This is an alternative take, with hints being output instead of print.// Multiples of 3 = hint "Fizz";// Multiples of 5 = hint "Buzz";// Multiples of 3 and 5 = hint "FizzBuzz";_i = 1; // Start at 1for [{_x=1},{_x<=100},{_x=_x+1}] do{// Start the for loop here// Divide the number by 3 and 5_iThree = (_i/3);_iFive = (_i/5);// Round the divided numbers down (we can't check for int easily any other way)_fThree = (floor(_i/3));_fFive = (floor(_i/5));// True if _i / (3 OR 5) is equal to the floored value, meaning it's an integer_condOne = ((_iThree) == (_fThree));_condTwo = ((_iFive) == (_fFive));// Determine if we need to hint Fizz, Buzz, FizzBuzz or neither.if ((_condOne) && !(_condTwo)) then {hint "Fizz";};if (!(_condOne) && (_condTwo)) then {hint "Buzz";};if ((_condOne) && (_condTwo)) then {hint "FizzBuzz";};if (!(_condOne) && !(_condTwo)) then {hint str(_i);};_i = _i+1;sleep 0.5;};
Alternatively rather than using a separate variable _i we could of just used _x inside the for loop as this would normally incremement, however we might want to influence the loop externally, while not apart of the original design requirement, this at least demonstrates some extra utility in the flow.
No comments:
Post a Comment