My force calculating code won't print to the console

128 Views Asked by At

I wrote some javascript code in droidscript but it won't work. I made several attempts to fix it, even deleting irrelevant code, but it still won't print any thing to the console, what's wrong with it?

var Vector = function(x, y) {
  this.x = typeof x == "number" ? x : 0;
  this.y = typeof y == "number" ? y : 0;
  console.log("vector created");

  this.length = function() {
    return Math.sqrt(x * x + y * y);
  };

  this.sub = function(vec) {
    return new Vector(this.x - vec.x, this.y - vec.y);
  };
};

function slope(pos1, pos2) {
  console.log("slope calculated");
  return (pos1.y - pos2.y) / (pos1.x - pos2.x);
}

function calc_vector(f, s) {
  console.log("vector calculated");
  return new Vector(f * Math.cos(s), f * Math.sin(s));
}

function normal_force(forcevec, s) {
  var f = calc_vector(-forcevec.length(), slope(forcevec, new Vector()) - s);
  console.log("normal force calculated: " + f.y);
  return f.y;
}

//Called when application is started.
function OnStart() {
  var force = normal_force(new Vector(0, 1), 45);
  console.log(force);
}


OnStart();

1

There are 1 best solutions below

0
Shrimpy On

console.log() is tolerated, but in DroidScript, it does not display anything. The following code overrides the console object and creates an appropriate layout context for your snippet. Chose to create a 'New JavaScript App', insert your code at the indicated location, and it will run straight away in DroidScript.

if (typeof(app) == "object") 
  var console = new CONSOLE();

// INSERT YOUR CODE HERE \/

console.log("TEST");
console.log("TEST");
console.log("TEST");

// INSERT YOUR CODE HERE /\

function CONSOLE() {
  this.txt = "";
  this.log = function con_log(str) {
    this.txt += (this.txt.length == 0 ? "" : "\r\n") + str;
    this.edt.SetText(this.txt);
  }
  this.create = function() {
    this.lay = app.CreateLayout("linear", "TopCenter,FillXY");
    this.edt = app.CreateTextEdit("", 1, .95, "NoSpell");
    this.edt.SetBackColor("#000000");
    this.edt.SetTextSize(12);
    this.lay.AddChild(this.edt);
    app.AddLayout(this.lay);
    this.edt.SetText("");
  }
  this.create();
}