Permalink
Browse files

pulled out helpers into math-table.js and cleaned up adding and subtr…

…acting negative numbers
  • Loading branch information...
1 parent 963cdcf commit add7eae73ed6b7adc6ff032a146877d87f256728 @Cyrik Cyrik committed May 31, 2011
@@ -13,32 +13,22 @@
<div class="problems">
<div id="original">
- <div class="vars">
- <var id="OP">"+"</var>
- </div>
- <p class="question"><var>A</var> <var>OP</var> <var>B</var></p>
+ <p class="question"><var>A</var> + <var>B</var></p>
<div class="solution"><var>A+B</var></div>
<div class="hints">
- <p data-if="B < 0 " class="hint1">+(<var>B</var>) = -<var>B*-1</var></p>
- <p class="hint2"><var>A</var> <var>OP</var> <var>B</var> = <var>A+B</var></p>
+ <p data-if="B < 0 ">+(<var>B</var>) = -<var>B*-1</var></p>
+ <p><var>A</var> + <var>B</var> = <var>A+B</var></p>
</div>
</div>
- <div data-type="original">
- <div class="vars">
- <var id="OP">"-"</var>
- </div>
- <p class="question"><var>A</var> <var>OP</var> <var>B</var></p>
+ <div>
+ <p class="question"><var>A</var> - <var>B</var></p>
<div class="solution"><var>A-B</var></div>
<div class="hints">
- <p data-if="B < 0 " class="hint1">-(<var>B</var>) = +<var>B*-1</var></p>
- <p class="hint2"><var>A</var> <var>OP</var> <var>B</var> = <var>A-B</var></p>
+ <p data-if="B < 0 ">-(<var>B</var>) = +<var>B*-1</var></p>
+ <p><var>A</var> - <var>B</var> = <var>A-B</var></p>
</div>
</div>
</div>
- <div class="hints">
- <p class="hint1"></p>
- <p class="hint2"></p>
- </div>
</div>
</body>
</html>
@@ -1,46 +1,9 @@
<!DOCTYPE html>
-<html data-require="math">
+<html data-require="math math-table">
<head>
<title>Adding Decimals</title>
<script src="../khan-exercise.js"></script>
- <script>
- // takes a and b as addends in array form
- // returns the carrys at each position
- function createLongAdditionTable( a, b ){
- var length = Math.max( a.length, b.length );
- var paddedA = padd( a, length);
- var paddedB = padd( b, length);
- var carry = new Array( length + 1);
- var result = new Array( length + 1);
- initArray( carry, 0 );
- initArray( result, 0 );
- for( var i = 0; i < length; i++){
- var tempResult = KhanUtil.digits(paddedA[i] + paddedB[i] + carry[i]);
- if( tempResult[1] ){
- carry[i+1] = tempResult[1];
- }
- result[i] = tempResult[0];
- }
- return carry;
- }
- // returns a padded version of arr, that has arr.length>=length and is padded with 0´s
- function padd( arr, length){
- var result = arr.slice();
- if( result.length >= length ){
- return result;
- }
- for( var i = result.length ; i < length; i++ ){
- result[i] = 0;
- }
- return result;
- }
- // completly fills the arr array with the val value
- function initArray( arr, val){
- for( var i = 0; i < arr.length; i++){
- arr[i] = val;
- }
- }
- </script>
+
</head>
<body>
<div class="exercise">
@@ -53,6 +16,7 @@
<var id="A_ARRAY">digits( round( A * pow( 10, MAX_SHIFT )))</var>
<var id="B_ARRAY">digits( round( B * pow( 10, MAX_SHIFT )))</var>
<var id="ADDITION_TABLE">createLongAdditionTable( A_ARRAY, B_ARRAY)</var>
+ <var id="RESULT">(round( A * pow( 10, MAX_SHIFT ))) + (round( B * pow( 10, MAX_SHIFT ))) / pow( 10, MAX_SHIFT)
</div>
<div class="problems">
@@ -1,46 +1,9 @@
<!DOCTYPE html>
-<html data-require="math">
+<html data-require="math math-table">
<head>
<title>Addition 3</title>
<script src="../khan-exercise.js"></script>
- <script>
- // takes a and b as addends in array form
- // returns the carrys at each position
- function createLongAdditionTable( a, b ){
- var length = Math.max( a.length, b.length );
- var paddedA = padd( a, length);
- var paddedB = padd( b, length);
- var carry = new Array( length + 1);
- var result = new Array( length + 1);
- initArray( carry, 0 );
- initArray( result, 0 );
- for( var i = 0; i < length; i++){
- var tempResult = KhanUtil.digits(paddedA[i] + paddedB[i] + carry[i]);
- if( tempResult[1] ){
- carry[i+1] = tempResult[1];
- }
- result[i] = tempResult[0];
- }
- return carry;
- }
- // returns a padded version of arr, that has arr.length>=length and is padded with 0´s
- function padd( arr, length){
- var result = arr.slice();
- if( result.length >= length ){
- return result;
- }
- for( var i = result.length ; i < length; i++ ){
- result[i] = 0;
- }
- return result;
- }
- // completly fills the arr array with the val value
- function initArray( arr, val){
- for( var i = 0; i < arr.length; i++){
- arr[i] = val;
- }
- }
- </script>
+
</head>
<body>
<div class="exercise">
@@ -1,46 +1,8 @@
<!DOCTYPE html>
-<html data-require="math">
+<html data-require="math math-table">
<head>
<title>Addition 4</title>
<script src="../khan-exercise.js"></script>
- <script>
- // takes a and b as addends in array form
- // returns the carrys at each position
- function createLongAdditionTable( a, b ){
- var length = Math.max( a.length, b.length );
- var paddedA = padd( a, length);
- var paddedB = padd( b, length);
- var carry = new Array( length + 1);
- var result = new Array( length + 1);
- initArray( carry, 0 );
- initArray( result, 0 );
- for( var i = 0; i < length; i++){
- var tempResult = KhanUtil.digits(paddedA[i] + paddedB[i] + carry[i]);
- if( tempResult[1] ){
- carry[i+1] = tempResult[1];
- }
- result[i] = tempResult[0];
- }
- return carry;
- }
- // returns a padded version of arr, that has arr.length>=length and is padded with 0´s
- function padd( arr, length){
- var result = arr.slice();
- if( result.length >= length ){
- return result;
- }
- for( var i = result.length ; i < length; i++ ){
- result[i] = 0;
- }
- return result;
- }
- // completly fills the arr array with the val value
- function initArray( arr, val){
- for( var i = 0; i < arr.length; i++){
- arr[i] = val;
- }
- }
- </script>
</head>
<body>
<div class="exercise">
@@ -1,5 +1,5 @@
<!DOCTYPE html>
-<html data-require="math">
+<html data-require="math math-table">
<head>
<title>Division 1.5</title>
<script src="../khan-exercise.js"></script>
@@ -15,27 +15,6 @@
}
return result;
}
- function createLongDivisionTable(n,div){
- var temp = KhanUtil.digits(n);
- var divisions= [];
- var subtrahends = [];
- var minuends = [];
- for( var i=temp.length-1;i>=0;i--){
- var curDividend = 0;
- for( var y=i;y<temp.length;y++){
- curDividend+=temp[y]*Math.pow(10,y-i);
- }
- if(curDividend>=div || divisions.length > 0){
- var division=Math.floor(curDividend/div);
- divisions.push(division);
- var subtrahend = division*div;
- subtrahends.push(subtrahend);
- temp= KhanUtil.digits(parseInt(temp.reverse().join(""))-subtrahend*Math.pow(10,i));
- minuends.push(curDividend);
- }
- }
- return {divisions:divisions, subtrahends:subtrahends, minuends:minuends};
- }
</script>
</head>
<body>
@@ -1,31 +1,9 @@
<!DOCTYPE html>
-<html data-require="math">
+<html data-require="math math-table">
<head>
<title>Division 2</title>
<script src="../khan-exercise.js"></script>
- <script>
- function createLongDivisionTable(n,div){
- var temp = KhanUtil.digits(n);
- var divisions= [];
- var subtrahends = [];
- var minuends = [];
- for( var i=temp.length-1;i>=0;i--){
- var curDividend = 0;
- for( var y=i;y<temp.length;y++){
- curDividend+=temp[y]*Math.pow(10,y-i);
- }
- if(curDividend>=div || divisions.length > 0){
- var division=Math.floor(curDividend/div);
- divisions.push(division);
- var subtrahend = division*div;
- subtrahends.push(subtrahend);
- temp= KhanUtil.digits(parseInt(temp.reverse().join(""))-subtrahend*Math.pow(10,i));
- minuends.push(curDividend);
- }
- }
- return {divisions:divisions, subtrahends:subtrahends, minuends:minuends};
- }
- </script>
+
</head>
<body>
<div class="exercise">
@@ -1,31 +1,8 @@
<!DOCTYPE html>
-<html data-require="math">
+<html data-require="math math-table">
<head>
<title>Division 3</title>
<script src="../khan-exercise.js"></script>
- <script>
- function createLongDivisionTable(n,div){
- var temp = KhanUtil.digits(n);
- var divisions= [];
- var subtrahends = [];
- var minuends = [];
- for( var i=temp.length-1;i>=0;i--){
- var curDividend = 0;
- for( var y=i;y<temp.length;y++){
- curDividend+=temp[y]*Math.pow(10,y-i);
- }
- if(curDividend>=div || divisions.length > 0){
- var division=Math.floor(curDividend/div);
- divisions.push(division);
- var subtrahend = division*div;
- subtrahends.push(subtrahend);
- temp= KhanUtil.digits(parseInt(temp.reverse().join(""))-subtrahend*Math.pow(10,i));
- minuends.push(curDividend);
- }
- }
- return {divisions:divisions, subtrahends:subtrahends, minuends:minuends};
- }
- </script>
</head>
<body>
@@ -1,32 +1,8 @@
<!DOCTYPE html>
-<html data-require="math">
+<html data-require="math math-table">
<head>
<title>Division 4</title>
<script src="../khan-exercise.js"></script>
- <script>
- function createLongDivisionTable(n,div){
- var temp = KhanUtil.digits(n);
- var divisions= [];
- var subtrahends = [];
- var minuends = [];
- for( var i=temp.length-1;i>=0;i--){
- var curDividend = 0;
- for( var y=i;y<temp.length;y++){
- curDividend+=temp[y]*Math.pow(10,y-i);
- }
- if(curDividend>=div || divisions.length > 0){
- var division=Math.floor(curDividend/div);
- divisions.push(division);
- var subtrahend = division*div;
- subtrahends.push(subtrahend);
- temp= KhanUtil.digits(parseInt(temp.reverse().join(""))-subtrahend*Math.pow(10,i));
- minuends.push(curDividend);
- }
- }
- return {divisions:divisions, subtrahends:subtrahends, minuends:minuends};
- }
- </script>
-
</head>
<body>
<div class="exercise">
@@ -1,41 +1,9 @@
<!DOCTYPE html>
-<html data-require="math">
+<html data-require="math math-table">
<head>
<title>Multiplication 2</title>
<script src="../khan-exercise.js"></script>
- <script>
- function createLongMultiplicationTable( a, b ) {
- var carry = new Array(a.length);
- var digits = [];
- initArray( carry, 0);
- for( var i = 0; i < a.length; i++ ){
- var multi = paddedDigits( a[i] * b[0], 2 );
- var digitResult = paddedDigits( multi[0] + carry[i], 2 );
- digitResult[1] += multi[1];
- carry[i+1] = digitResult[1];
- digits[i] = digitResult[0];
- if( digitResult[1] !== 0 ){
- digits[i+1] = digitResult[1];
- }
- }
- return { carry: carry, digits: digits };
- }
- function paddedDigits( n, length ){
- var result = KhanUtil.digits(n);
- if( result.length >= length ){
- return result;
- }
- for( var i = result.length ; i < length; i++ ){
- result[i] = 0;
- }
- return result;
- }
- function initArray( arr, val){
- for( var i = 0; i < arr.length; i++){
- arr[i] = val;
- }
- }
- </script>
+
</head>
<body>
<div class="exercise">
Oops, something went wrong.

0 comments on commit add7eae

Please sign in to comment.