JessieCode icon indicating copy to clipboard operation
JessieCode copied to clipboard

How does loop and closure work in JessieCode ?

Open RyoJerryYu opened this issue 10 months ago • 0 comments

I have a JessieCode snippet as follow:

```jessiecode
FFunc = function(x) {
    return sin(x);
};
F = functiongraph(FFunc, -10, 10);

P1 = point(0, 0);
P2 = point(1, 2);

a = function () {
    return P2.X() - P1.X();
};

b = function () {
    return P2.Y() - P1.Y();
};

GFunc = function(x) {
    return b() * sin(PI * ( x - P1.X() ) / 2 / a() ) + P1.Y();
};
G = functiongraph(GFunc, -10, 10);


InterFunc = function(i, x) {
  return FFunc(x) * i / 10 + GFunc(x) * (1 - i / 10);
};

for (i = 1; i <= 9; i = i + 1) {
  functiongraph(function(x) {
    return InterFunc(i, x);
  }, -10, 10) << strokeOpacity: 0.5 >>;
}
```

Expected

I expected it could render graphs as follow:

Image

Behavior

But it actually rendered as bellow:

Image

Why it was so and how can I fix it?

In addition

I also tried about this snippet:

```jessiecode
FFunc = function(x) {
    return sin(x);
};
F = functiongraph(FFunc, -10, 10);

P1 = point(0, 0);
P2 = point(1, 2);

a = function () {
    return P2.X() - P1.X();
};

b = function () {
    return P2.Y() - P1.Y();
};

GFunc = function(x) {
    return b() * sin(PI * ( x - P1.X() ) / 2 / a() ) + P1.Y();
};
G = functiongraph(GFunc, -10, 10);


InterFunc = function(i) {
  return function(x) {
    return FFunc(x) * i / 10 + GFunc(x) * (1 - i / 10);
  };
};

for (i = 1; i <= 9; i = i + 1) {
  functiongraph(InterFunc(i), -10, 10) << strokeOpacity: 0.5 >>;
}
```

And it result in Operation - not defined on operands object and number

RyoJerryYu avatar Mar 14 '25 01:03 RyoJerryYu