1

I'm trying to make a circle from a radius and x,y coordinate. I have it all done except the array is not the correct format for my use. I get:

[
    "X_PROPS:40,Y_PROPS:0",
    "X_PROPS:39.99390780625565,Y_PROPS:0.6980962574913405",
    "X_PROPS:39.97563308076383,Y_PROPS:1.3959798681000388",
    "X_PROPS:39.94518139018295,Y_PROPS:2.093438249717753"
]

but I need:

[
    {X_PROPS:40,Y_PROPS:0},
    {X_PROPS:39.99390780625565,Y_PROPS:0.6980962574913405},
    {X_PROPS:39.97563308076383,Y_PROPS:1.3959798681000388},
    {X_PROPS:39.94518139018295,Y_PROPS:2.093438249717753}
]

I tried this:

function spec(radius, steps, centerX, centerY){
  var xValues = [centerX];
  var yValues = [centerY];
  var result = [];
  for (var i = 0; i < steps; i++) {
    xValues[i] = (centerX + radius * Math.cos(2 * Math.PI * i / steps));
    yValues[i] = (centerY + radius * Math.sin(2 * Math.PI * i / steps));

    result.push('X_PROPS:'+ xValues[i]+','+'Y_PROPS:'+ yValues[i]);
  }

  return result;

}
console.log(spec(40,360,0,0))

1 Answer 1

5

This expression 'X_PROPS:'+ xValues[i]+','+'Y_PROPS:'+ yValues[i] creates a string. Create an object literal instead:

function spec(radius, steps, centerX, centerY) {
  var result = [];
  for (var i = 0; i < steps; i++) {
    result.push({
      X_PROPS: (centerX + radius * Math.cos(2 * Math.PI * i / steps)),
      Y_PROPS: (centerY + radius * Math.sin(2 * Math.PI * i / steps))
    });
  }

  return result;

}
console.log(spec(40, 360, 0, 0))

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.