Sieht sehr interessant aus. Ich werde es bestimmt auch mal brauchen.
http://aktuell.de.selfhtml.org/artikel/javascript/squarematrix/ ?
Für den Moment habe ich allerdings versucht alles über Objekte zu machen. Dabei schaffe ich es aber nicht einmal über ein Problem hinweg zu kommen: "too much recursion"
Woher kommt die?
Folgender Code liegt bislang vor:
/*******************************/
function Term() {
this.firstOperand = null;
this.secondOperand = null;
this.operation = "";
}
Term.prototype.getFirstOperand = function() {return this.firstOperand;};
Term.prototype.setFirstOperand = function(sOperand) {this.firstOperand = sOperand};
Term.prototype.getSecondOperand = function() {return firstOperand;};
Term.prototype.setSecondOperand = function(sOperand) {this.secondOperand = sOperand};
Term.prototype.getOperation = function() { return firstOperand;};
Term.prototype.setOperation = function(sOperation) {this.operation = sOperation};
Term.prototype.build = function() {
//alert(typeof this.firstOperand );
if (typeof this.firstOperand == "object") var sFirst = this.firstOperand.build(); else var sFirst = this.firstOperand;
if (typeof this.secondOperand == "object") var sSecond = this.secondOperand; else var sSecond = this.secondOperand;
return sFirst+this.operation+sSecond;
};
/*******************************/
Damit erzeuge ich ein Termobjekt.
Jetzt erzeuge ich den Ausdruck.
/*******************************/
function rnd(u,o) {
return Math.floor(Math.random()*(o-u+1))+u;;
}
function getNumber() {
var newNumber = rnd(0,20);
if (rnd(0,100)>60 && params['type']=='fraction') {
newNumber *= -1;
}
return newNumber;
}
function getVariable() {
var posssibleVariables = "abcmn";
return posssibleVariables.split("")[rnd(0,posssibleVariables.length-1)]
}
function getSimpleTerm() {
return getVariable()+"^"+getNumber();
}
function getCalc() {
var resultString = "*";
return resultString;
}
function buildTerm() {
var curString = new Term();
curString.setFirstOperand(getSimpleTerm());
curString.setSecondOperand(getSimpleTerm());
curString.setOperation(getCalc());
curString.setFirstOperand(curString);
curString.setSecondOperand(getSimpleTerm());
curString.setOperation(getCalc());
return curString.build();
}
/*******************************/
Wieso kann ich den Term nicht rekursiv ausgeben?