forked from Khan/khan-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolynomials.js
More file actions
34 lines (28 loc) · 1.46 KB
/
polynomials.js
File metadata and controls
34 lines (28 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
module("polynomial");
(function(){
//define the polynomial 2x^{4}-3x^{2}+12x+5+3x^{-1}
var polyX = "x"; // polynomial is specified using x
var polyCoefs = [];//add in the coefs for the range that we are using minDegree 2, maxDegree 5
polyCoefs[-1] = 3;
polyCoefs[0] = 5;
polyCoefs[1] = 12;
polyCoefs[2] = -3;
polyCoefs[4] = 2;
test("Polynomial constructor defaults", function(){
equals((new KhanUtil.Polynomial(-1, 4, polyCoefs)).toString(),"2x^{4}-3x^{2}+12x+5+3x^{-1}", "defaults variable name to x");
ok((new KhanUtil.Polynomial(-1, 4)).toString(), "randomly generate coefs (3rd param) if not passed");
equals(new KhanUtil.Polynomial(-1, 4, polyCoefs).getNumberOfTerms(),5,"should only have 5 terms as no 3 coef");
});
test("Polynomial evalOf", function(){
equals((new KhanUtil.Polynomial(-1, 4, polyCoefs, polyX)).evalOf(1),19,"2*1^4-3*1^2+12*1+5+3*1^-1 = 2-3+12+5+3 = 19");
});
test( "Polynomial extractFromExpr", function(){
var polynomial = new KhanUtil.Polynomial(-1, 4, polyCoefs);
equals(polynomial.getCoefAndDegreeForTerm( 0 ).coef,2,"leading term is 2x^4");
equals(polynomial.getCoefAndDegreeForTerm( 0 ).degree,4,"leading term is 2x^4");
equals(polynomial.getCoefAndDegreeForTerm( 1 ).coef,-3,"second term is -3x^2");
equals(polynomial.getCoefAndDegreeForTerm( 1 ).degree,2,"second term is -3x^2");
equals(polynomial.getCoefAndDegreeForTerm( 2 ).coef,12,"third term is 12x");
equals(polynomial.getCoefAndDegreeForTerm( 2 ).degree,1,"third term is 12x");
});
})();