开发者

How do I translate the below javascript to C#?

I don't understand how 's' is being used in '(+s[i])'. What is the '+' for? Also I do not understand the use of the || symbol in this way, what is it doing?

var draw = function(s){
...
if (+s[i]) a = (a+90)%360||360; // Right
else a = (a-90||360); // Left

In the code below I do not understand what 'while (n--)' does?

var getS = function(n){
    var s = '';
    while (n--) s += getNext(0);
    return s;
};

If you want to look at this code in context go to http://fractal.qfox.nl/ and press F12 to get the developer tools up and look for dragon.js in the scripts. Please feel entirely free to po开发者_如何学运维st a complete translation to C# as well if you fancy the challenge.


Putting + in front of an expression coerces it into a number, e.g. from a string.

The || operator has the value of its left side if that can convert to true, otherwise the value of its right side. And so a||b would mean "use a if it's not null, false, zero or an empty string, otherwise use b".

And n-- will have boolean value false when n reaches zero.


if (+s[i]) is checking if s[i] exists and is a number != 0. In C# it would be the same as

int n;
if (int.TryParse(s[i], out n) && n != 0) { }

a = (a-90||360); is basically saying if leftside of || is null, undefined, false or zero, then take rightside. In C# it would look something like

a = (a-90 > 0)? a-90 : 360;

but a would have to be declared prior to that line.


while (n--){ } keeps repeating itself until n is 0. n must be declared prior to running that code though such as var n = 10;. In C# it would be

int n = 10;
while (n >= 0)
{
    n--;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜