Skip to content
Browse files

added KhanUtil.plural

  • Loading branch information...
1 parent 535441a commit f161590fb130cd5eae9bdac244b6d27092742ea4 @pappy74 pappy74 committed
Showing with 22 additions and 3 deletions.
  1. +2 −2 exercises/rate_problems_1.html
  2. +20 −1 khan-exercise.js
View
4 exercises/rate_problems_1.html
@@ -35,7 +35,7 @@
<div class="problem">
<p>Alice traveled by <var>VEHICLE1</var> at an average speed of <var>SPEED1</var> miles per hour.</p>
<p>Then, she traveled by <var>VEHICLE2</var> at an average speed of <var>SPEED2</var> miles per hour.</p>
- <p>In total, she traveled <var>DIST</var> miles for <var>TIME</var> hour<var>TIME > 1 ? "s" : ""</var>.</p>
+ <p>In total, she traveled <var>DIST</var> miles for <var>TIME</var> hour<var>plural(TIME)</var>.</p>
</div>
<p class="question">How many miles did Alice travel by <var>VEHICLE1</var>? (Round to the nearest mile.)</p>
<p class="solution"><var>round(DIST1)</var></p>
@@ -78,4 +78,4 @@
</div>
</div>
</body>
-</html>
+</html>
View
21 khan-exercise.js
@@ -117,6 +117,25 @@ function initRandom() {
jQuery.extend(KhanUtil, {
random: function() {
return m.random();
+ },
+
+ // pluralization helper. There are three signatures
+ // - plural(NUMBER): return "s" if NUMBER is not 1
+ // - plural(NUMBER, plural_suffix): return plural_suffix if NUMBER is
+ // not 1
+ // - plural(NUMBER, singular, plural): return singular if NUMBER is 1,
+ // otherwise return plural
+ plural: function(value, arg1, arg2) {
+ var valIs1 = (value === 1);
+ if ( arg1 === undefined ) {
+ return valIs1 ? "" : "s";
+ }
+
+ if ( arg2 === undefined ) {
+ return valIs1 ? "" : arg1;
+ }
+
+ return valIs1 ? arg1 : arg2;
}
});
}
@@ -344,4 +363,4 @@ function queryString() {
}
return urlParams;
-}
+}

0 comments on commit f161590

Please sign in to comment.
Something went wrong with that request. Please try again.