I tried to use the loop method but the program stuck.
function getPoints() {
var x, y;
for (
x = 1, y = 2, a = 1.4, b = 0.3;
x < 10, y < 10;
x = 1 - a * x ** 2 y, y = b * x
) {
console.log(x, y);
}
}
CodePudding user response:
I would like to see how this system works and get a list with points (x,y) after a few iterations.
function getPoints() {
const a = 1.4,
b = 0.3
for (
let i = 0, x = 1, y = 2; i < 10; i = 1
) {
x = 1 - a * x ** 2 y
y = b * x
console.log(x, y);
}
}
getPoints()
CodePudding user response:
As Konrad pointed out in a comment, x
and y
become negative and never again become positive, so the loop never ends. In this case, a better solution would be to use a while-loop and check for the absolute value of x
and y
, so the loop will terminate regardless of what direction the values go:
let x = 1, y = 2
const a = 1.4, b = 0.3;
while (Math.abs(x) < 10 && Math.abs(y) < 10) {
console.log(x, y)
x = 1 - a * x ** 2 y;
y = b * x;
}
Also, note that your original for-loop does not do what you think it does. In the expression x < 10, y < 10
, the comma does not mean and, it is a comma operator and means forget everything except the thing after the last comma, which is y < 10
. Expand the snippet below to see an example.
for (
let a = 0, b = 0;
a < 10, b < 10;
a = 2, b
) {
console.log(a, b)
}