Home > Software design >  How do i get my simple point to line calculator to work (in javascript)
How do i get my simple point to line calculator to work (in javascript)

Time:10-16

im experienced in python but not javascript the # represent the numbers i would put in

let r
let m2
let b2
let x2
let y2
function rcirc(x,y,m,b) {
  m2 = -1/m
  b2 = y - (m * x)
  y2 = (b2 - b) / (m - m2)
  x2 = (y2 - b) / m
  return(x,y)
};

rcirc("#","#","#","#");
console.log(x,",",y);

CodePudding user response:

You should not have those global variables -- that wouldn't even work in Python. Define them in the function as local variables.

A difference with Python is that there is no concept of tuple in JavaScript, so you would return the values as an array (i.e. a list in Python). But of course, you should return the result of the calculation, not the input values!

Here is how it could look:

function rcirc(x, y, m, b) {
  let m2 = -1/m;
  let b2 = y - (m * x);
  let y2 = (b2 - b) / (m - m2);
  let x2 = (y2 - b) / m;
  return [x2, y2];
}

let point = rcirc(1, 2, 3, 4);
console.log(point);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

While returning the pair in the function, [ ] must be used instead of ( ). Also, the new/calculated value [x2,y2] must be returned.

Also, the returned value must be stored in a variable or pair as per your wish.

let r
let m2
let b2
let x2
let y2
function rcirc(x,y,m,b) {
  m2 = -1/m
  b2 = y - (m * x)
  y2 = (b2 - b) / (m - m2)
  x2 = (y2 - b) / m
  return [x2,y2]
};

let [x,y] = rcirc(1,2,3,4);
console.log(x,y);

Hope this helps!

  • Related