Skip to content

Instantly share code, notes, and snippets.

@sophiebits
Created June 3, 2011 00:04
Show Gist options
  • Select an option

  • Save sophiebits/1005615 to your computer and use it in GitHub Desktop.

Select an option

Save sophiebits/1005615 to your computer and use it in GitHub Desktop.
diff --git a/exercises/exponents_1.html b/exercises/exponents_1.html
index 70f4076..fdd8bf4 100644
--- a/exercises/exponents_1.html
+++ b/exercises/exponents_1.html
@@ -14,22 +14,16 @@
<div id="original" data-weight="3">
<div class="vars">
<var id="BASE">randRangeExclude( -10, 10, [0, 1, -1] )</var>
- <var id="BASE_STRING">BASE < 0 ? "(" + BASE + ")" : BASE</var>
+ <var id="BASE_STR">negParens( BASE )</var>
<!-- an array of reasonable exponents for specific bases; the ith element is the highest exponent which is reasonable to raise i to. note that this is somewhat arbitrary. -->
<var id="REASONABLE_EXPS">[ undefined, undefined, 5, 4, 4, 4, 3, 3, 3, 3, 10 ]</var>
<!-- we limit the occurrences of 1 as an exponent because it is to easy, but we want it to appear at least a little. -->
<var id="EXP">randRangeWeighted( 1, REASONABLE_EXPS[abs( BASE )], 1, .1 )</var>
<var id="SOLUTION">round( pow( BASE, EXP ) )</var>
- <var id="HINT">function() {
- var result = BASE_STRING;
- for ( var i = 1; i < EXP; i++ ) {
- result += " \\cdot" + BASE_STRING;
- }
- return result;
- }()</var>
+ <var id="HINT">expandExponentiation( BASE, EXP )</var>
</div>
- <p class="question"><code><var>BASE_STRING</var>^{<var>EXP</var>}</code></p>
+ <p class="question"><code><var>BASE_STR</var>^{<var>EXP</var>}</code></p>
<p class="solution" data-type="decimal"><var>SOLUTION</var></p>
<div class="hints">
@@ -43,10 +37,10 @@
<div id="zero-exp">
<div class="vars">
<var id="BASE">randRangeNonZero( -500, 500 )</var>
- <var id="BASE_STRING">BASE < 0 ? "(" + BASE + ")" : BASE</var>
+ <var id="BASE_STR">negParens( BASE )</var>
</div>
- <p class="question"><code><var>BASE_STRING</var>^{0}</code></p>
+ <p class="question"><code><var>BASE_STR</var>^{0}</code></p>
<p class="solution" data-type="decimal">1</p>
<div class="hints">
@@ -60,7 +54,7 @@
<div class="vars">
<!-- -1 is the most interesting case, so we choose -1 half the time, 0 a quarter of the time, and 1 a quarter of the time. -->
<var id="BASE">random() < 0.5 ? -1 : (random() < 0.5 ? 0 : 1)</var>
- <var id="BASE_STRING">BASE === -1 ? "(-1)" : BASE</var>
+ <var id="BASE_STR">negParens( BASE )</var>
<var id="EXP">randRange( 1, 1000 )</var>
<var id="SOLUTION">round( pow( BASE, EXP ) )</var>
<var id="POWER_HINT">function() {
@@ -70,7 +64,7 @@
}()</var>
</div>
- <p class="question"><code><var>BASE_STRING</var>^{<var>EXP</var>}</code></p>
+ <p class="question"><code><var>BASE_STR</var>^{<var>EXP</var>}</code></p>
<p class="solution" data-type="decimal"><var>SOLUTION</var></p>
<div class="hints">
diff --git a/exercises/exponents_2.html b/exercises/exponents_2.html
index 3a90335..55256d9 100644
--- a/exercises/exponents_2.html
+++ b/exercises/exponents_2.html
@@ -1,5 +1,5 @@
<!DOCTYPE html>
-<html data-require="math">
+<html data-require="math mathformat">
<head>
<title>Exponents 2</title>
<script src="../khan-exercise.js"></script>
@@ -10,37 +10,29 @@
<div class="problems">
<div>
- <div class="vars" data-ensure="BASE_NUM !== BASE_DENOM">
- <var id="BASE_NUM">randRange( 1, 10 )</var>
+ <div class="vars" data-ensure="abs( BASE_NUM ) !== abs( BASE_DENOM )">
+ <var id="BASE_NUM">randRangeNonZero( -10, 10 )</var>
<!-- we want 25% of the exercises to have integer bases. -->
<var id="BASE_DENOM">randRangeWeighted( 1, 10, 1, .25 )</var>
- <!-- we want 50% of the exercises to have negative bases. -->
- <var id="BASE_NEG">random() < 0.5</var>
<!-- an array of reasonable exponents for specific bases; the ith element is the highest exponent which is reasonable to raise i to. note that this is somewhat arbitrary. -->
<var id="REASONABLE_EXPS">[ undefined, 1000, 5, 4, 4, 4, 3, 3, 3, 3, 10 ]</var>
- <!-- we want only 10% of the exercises to have 1 as an exponent. -->
- <var id="EXP">randRangeWeighted( 1, min( REASONABLE_EXPS[BASE_NUM], REASONABLE_EXPS[BASE_DENOM] ), 1, .1)</var>
- <!-- we want 75% of the exercises to have negative exponents. -->
- <var id="EXP_NEG">random() < 0.75</var>
- <var id="BASE_STRING">function() {
- var result = BASE_NUM;
- if ( BASE_DENOM !== 1 )
- result = "\\frac{BASE_NUM}{BASE_DENOM}";
- if ( BASE_NEG )
- result = "\\left(-" + result + "\\right)";
- return result;
- }()</var>
- <var id="HINT">function() {
- /* FIXME: implement! */
- }()</var>
- <var id="SOLUTION">round( pow( BASE_NUM / BASE_DENOM, EXP ) )</var>
+ <!-- we want only 10% of the exercises to have 1 as an exponent, and 75% of the exercises to have a negative exponent. -->
+ <var id="EXP">randRangeWeighted( 1, min( REASONABLE_EXPS[abs( BASE_NUM )], REASONABLE_EXPS[BASE_DENOM] ), 1, .1) * (random() < 0.75 ? -1 : 1)</var>
+ <var id="BASE_STR">fractionParens( BASE_NUM, BASE_DENOM )</var>
+ <var id="HINT">expandFracExp( BASE_NUM, BASE_DENOM, EXP )</var>
+ <var id="SOLUTION_NUM">EXP > 0 ? pow( BASE_NUM, EXP ) : pow( BASE_DENOM, -EXP)</var>
+ <var id="SOLUTION_DENOM">EXP > 0 ? pow( BASE_DENOM, EXP) : pow( BASE_NUM, -EXP)</var>
+ <var id="SOLUTION_STR">fraction( SOLUTION_NUM, SOLUTION_DENOM )</var>
+ <var id="SOLUTION">SOLUTION_NUM / SOLUTION_DENOM</var>
</div>
- <p class="question"><code><var>BASE_STRING</var>^{<var>(EXP_NEG ? "-" : "")+EXP</var>}</code></p>
+ <p class="question"><code><var>BASE_STR</var>^{<var>EXP</var>}</code></p>
<p class="solution" data-type="rational"><var>SOLUTION</var></p>
<div class="hints">
- <!-- FIXME: IMPLEMENT! -->
+ <div><code>= <var>HINT</var></code></div>
+ <div><code>= <var>SOLUTION_STR</var></code></div>
+ <div><code>= <var>SOLUTION</var></code></div>
</div>
</div>
</div>
diff --git a/utils/mathformat.js b/utils/mathformat.js
index fa70001..3727f4d 100644
--- a/utils/mathformat.js
+++ b/utils/mathformat.js
@@ -17,11 +17,35 @@ jQuery.extend(KhanUtil, {
d = d / gcd;
return d > 1 ?
- sign + "\\frac{" + n + "}{" + d + "}" :
+ sign + "\\dfrac{" + n + "}{" + d + "}" :
sign + n;
},
- fractionSimplification: function(n, d) {
+ /* Wrap a fraction in parentheses if it's actually displayed as a fraction or
+ if it's negative. */
+ fractionParens: function( n, d ) {
+ var neg = n / d < 0;
+ var frac = d !== 0 && d !== 1;
+ var nonzero = n !== 0;
+
+ if (nonzero && (frac || neg))
+ return "\\left(" + this.fraction( n, d ) + "\\right)";
+ else
+ return this.fraction( n, d );
+ },
+
+ /* expandExp for rational bases. */
+ expandFracExp: function( base_num, base_denom, exp ) {
+ var base_str = exp >= 0 ?
+ this.fractionParens( base_num, base_denom ) :
+ this.fractionParens( base_denom, base_num );
+
+ var str = base_str;
+ for ( var i = 1; i < Math.abs( exp ); i++ ) str += " \\cdot" + base_str;
+ return str;
+ },
+
+ fractionSimplification: function( n, d ) {
var result = "\\frac{" + n + "}{" + (d < 0 ? "(" + d + ")" : d) + "}";
if ( this.getGCD( n, d ) > 1 || d == 1 ) {
@@ -31,10 +55,19 @@ jQuery.extend(KhanUtil, {
return result;
},
+ /* Wrap a number in parentheses if it's negative. */
negParens: function( n ) {
return n < 0 ? "(" + n + ")" : n;
},
+ /* Expand something like (-2)^4 into (-2)*(-2)*(-2)*(-2) */
+ expandExp: function( base, exp ) {
+ var base_str = this.negParens( base );
+ var str = base_str;
+ for ( var i = 1; i < exp; i++ ) str += " \\cdot" + base_str;
+ return str;
+ },
+
splitRadical: function( n ) {
if ( n === 0 ) {
return [ 0, 1 ];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment