Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Also factor out subtracting_decimals

  • Loading branch information...
commit ad6f4a7f81254ea5dbfc28d643e7d9343770d8c7 1 parent 95d6db6
@spicyj spicyj authored
View
43 exercises/subtracting_decimals.html
@@ -7,48 +7,31 @@
</head>
<body>
<div class="exercise">
- <div class="vars" data-ensure="NEW_A > NEW_B">
+ <div class="vars" data-ensure="A_FLOAT > B_FLOAT">
<var id="A">randRange( 101, 999 )</var>
<var id="B">randRange( 101, 999 )</var>
+
<var id="A_DECIMAL">randRange( 1, 3 )</var>
<var id="B_DECIMAL">randRange( 1, 3 )</var>
- <var id="NEW_A">A * ( B_DECIMAL &gt; A_DECIMAL ? pow( 10, B_DECIMAL - A_DECIMAL ) : 1)</var>
- <var id="NEW_B">B * ( A_DECIMAL &gt; B_DECIMAL ? pow( 10, A_DECIMAL - B_DECIMAL ) : 1)</var>
- <var id="A_DIGITS">
- (function() {
- var result = digits( NEW_A );
- for ( var i = 0; i &lt; ( A_DECIMAL - B_DECIMAL ) || result.length &lt; A_DECIMAL + 1; i++ ) {
- result.push( 0 );
- }
- return result;
- })()</var>
- <var id="B_DIGITS">
- (function() {
- var result = digits( NEW_B );
- for ( var i = 0; i &lt; ( B_DECIMAL - A_DECIMAL ) || result.length &lt; B_DECIMAL + 1; i++ ) {
- result.push( 0 );
- }
- return result;
- })()</var>
+
+ <var id="A_FLOAT">A / pow( 10, A_DECIMAL )</var>
+ <var id="B_FLOAT">B / pow( 10, B_DECIMAL )</var>
</div>
<div class="problems">
<div>
- <p><code>\Huge{<var>roundTo( A_DECIMAL, A * pow( 10, -A_DECIMAL ) ).toFixed( A_DECIMAL )</var> - <var>roundTo( B_DECIMAL, B * pow( 10, -B_DECIMAL ) ).toFixed( B_DECIMAL )</var> = {?}}</code></p>
- <div class="solution" data-type="decimal"><var>A * pow( 10, -A_DECIMAL ) - B * pow( 10, - B_DECIMAL )</var></div>
- <div class="graphie" id="numbers">
- graph.subtractor = new Subtractor( NEW_A, NEW_B, A_DIGITS, B_DIGITS );
- DUMMY = Array( graph.subtractor.getNumHints() );
- </div>
+ <p><code>\Huge{<var>A_FLOAT.toFixed( A_DECIMAL )</var> - <var>B_FLOAT.toFixed( B_DECIMAL )</var> = {?}}</code></p>
+ <div class="solution" data-type="decimal"><var>A_FLOAT - B_FLOAT</var></div>
</div>
</div>
<div class="hints">
- <div class="graphie" data-update="numbers">
- graph.subtractor.show();
- graph.subtractor.showDecimals( A_DECIMAL, B_DECIMAL );
+ <div class="graphie" id="numbers">
+ graph.adder = new DecimalSubtractor( A, A_DECIMAL, B, B_DECIMAL );
+ graph.adder.show();
+ graph.adder.showDecimals();
</div>
- <div class="graphie" data-update="numbers" data-each="DUMMY as dummy">
- graph.subtractor.showHint();
+ <div class="graphie" data-update="numbers" data-each="DecimalSubtractor.numHintsFor( A, A_DECIMAL, B, B_DECIMAL ) times">
+ graph.adder.showHint();
</div>
</div>
</div>
View
87 utils/graphie-helpers-arithmetic.js
@@ -123,38 +123,6 @@ Adder.numHintsFor = function( a, b ) {
return KhanUtil.digits( a + b ).length + 1;
};
-function DecimalAdder( a, aDecimal, b, bDecimal ) {
- var newA = a * ( bDecimal > aDecimal ? Math.pow( 10, bDecimal - aDecimal ) : 1 );
- var newB = b * ( aDecimal > bDecimal ? Math.pow( 10, aDecimal - bDecimal ) : 1 );
-
- var aDigits = KhanUtil.digits( newA );
- for ( var i = 0; i < ( aDecimal - bDecimal ) || aDigits.length < aDecimal + 1; i++ ) {
- aDigits.push( 0 );
- }
-
- var bDigits = KhanUtil.digits( newB );
- for ( var i = 0; i < ( bDecimal - aDecimal ) || bDigits.length < bDecimal + 1; i++ ) {
- bDigits.push( 0 );
- }
-
- var adder = new Adder( newA, newB, aDigits, bDigits );
-
- adder.showDecimals = (function( old ) {
- return function() {
- old.call( adder, aDecimal, bDecimal );
- }
- })( adder.showDecimals );
-
- return adder;
-}
-
-DecimalAdder.numHintsFor = function( a, aDecimal, b, bDecimal ) {
- var newA = a * ( bDecimal > aDecimal ? Math.pow( 10, bDecimal - aDecimal ) : 1 );
- var newB = b * ( aDecimal > bDecimal ? Math.pow( 10, aDecimal - bDecimal ) : 1 );
-
- return Adder.numHintsFor( newA, newB );
-}
-
function Subtractor( a, b, digitsA, digitsB ) {
var graph = KhanUtil.currentGraph;
digitsA = digitsA || KhanUtil.digits( a );
@@ -172,7 +140,7 @@ function Subtractor( a, b, digitsA, digitsB ) {
sideY: 1.5 };
var index = 0;
- var numHints = digitsA.length + 1;
+ var numHints = Subtractor.numHintsFor( a, b );
this.show = function() {
graph.init({
@@ -292,6 +260,59 @@ function Subtractor( a, b, digitsA, digitsB ) {
};
}
+Subtractor.numHintsFor = function( a, b ) {
+ return KhanUtil.digits( a ).length + 1;
+};
+
+// convert Adder -> DecimalAdder and Subtractor -> DecimalSubtractor
+(function() {
+ var decimate = function( drawer ) {
+ var news = function( a, aDecimal, b, bDecimal ) {
+ var newA = a * ( bDecimal > aDecimal ? Math.pow( 10, bDecimal - aDecimal ) : 1 );
+ var newB = b * ( aDecimal > bDecimal ? Math.pow( 10, aDecimal - bDecimal ) : 1 );
+ return [ newA, newB ];
+ };
+
+ var decimated = function( a, aDecimal, b, bDecimal ) {
+ var newAB = news( a, aDecimal, b, bDecimal );
+ var newA = newAB[0], newB = newAB[1];
+
+ var aDigits = KhanUtil.digits( newA );
+ for ( var i = 0; i < ( aDecimal - bDecimal ) || aDigits.length < aDecimal + 1; i++ ) {
+ aDigits.push( 0 );
+ }
+
+ var bDigits = KhanUtil.digits( newB );
+ for ( var i = 0; i < ( bDecimal - aDecimal ) || bDigits.length < bDecimal + 1; i++ ) {
+ bDigits.push( 0 );
+ }
+
+ var drawn = new drawer( newA, newB, aDigits, bDigits );
+
+ drawn.showDecimals = (function( old ) {
+ return function() {
+ old.call( drawn, aDecimal, bDecimal );
+ }
+ })( drawn.showDecimals );
+
+ return drawn;
+ };
+
+ decimated.numHintsFor = function( a, aDecimal, b, bDecimal ) {
+ var newAB = news( a, aDecimal, b, bDecimal );
+ var newA = newAB[0], newB = newAB[1];
+
+ return drawer.numHintsFor( newA, newB );
+ };
+
+ return decimated;
+ };
+
+ // I hate global variables
+ DecimalAdder = decimate(Adder);
+ DecimalSubtractor = decimate(Subtractor);
+})();
+
function drawCircles( num, color ) {
with ( KhanUtil.currentGraph ) {
var numCols = Math.floor( Math.sqrt( num ));
Please sign in to comment.
Something went wrong with that request. Please try again.