Skip to content

How does loop and closure work in JessieCode ? #25

@RyoJerryYu

Description

@RyoJerryYu

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions