Permalink
Browse files

Merge pull request #1 from briidragon073/master

Omar got the absolute value solving algorithm to convert pipes (|) into the Javascript Math.abs() function for evaluating.
  • Loading branch information...
2 parents 4e7ec46 + f5a8451 commit a86e6f057577a2d8fe89fe3b3aeadcd52f4dee79 @danfinlay committed Oct 4, 2012
Showing with 49 additions and 22 deletions.
  1. +16 −18 005.html
  2. +33 −4 005.js
View
@@ -17,29 +17,27 @@
<form class="question">
<div class = answerBox>
- |<span class="q">
- 1-2
- </span>|
+ <span class="q">|1-2|</span>
= <input type="text" class="answer">
</div>
<input type="submit">
</form>
<form class="question">
<div class = answerBox>
- |<span class="q">
- 5*-5
- </span>|
+ <span class="q">
+ |5*-5|
+ </span>
= <input type="text" class="answer">
</div/>
<input type="submit">
</form>
<form class="question">
<div class = answerBox>
- |<span class="q">
- -5+(-10*6)
- </span>|
+ <span class="q">
+ |-5|+(-10*6)-|-5|
+ </span>
= <input type="text" class="answer">
</div>
<input type="submit">
@@ -49,9 +47,9 @@
<form class="question">
<div class = answerBox>
- |<span class="q">
- 7-23
- </span>|
+ <span class="q">
+ |7-23|
+ </span>
= <input type="text" class="answer">
</div>
<input type="submit">
@@ -70,9 +68,9 @@
<form class="question">
<div class = answerBox>
- |<span class="q">
- 1+(-7*5)
- </span>| = <input type="text" class="answer">
+ <span class="q">
+ 1+|(-7*5)|
+ </span> = <input type="text" class="answer">
</div>
<div>
@@ -82,9 +80,9 @@
<form class="question">
<div class = answerBox>
- |<span class="q">
- 3*-4 + -2*5
- </span>| = <input type="text" class="answer">
+ <span class="q">
+ |3*-4| + -2*5
+ </span> = <input type="text" class="answer">
</div>
<div>
View
37 005.js
@@ -1,3 +1,31 @@
+function solveAbsolutes(questionText){
+ console.log("Beginning to try to solve "+questionText);
+ var answer="";
+ var startAbs=false;
+
+ for(var i = 0; i < questionText.length; i++){
+ var letter = questionText[i];
+ console.log("Letter: "+letter);
+ if(letter!=="|"){
+ console.log("This letter is not a pipe.");
+ answer=answer+letter;
+ }else{
+ if(startAbs){
+ console.log("Ending absolute.");
+ answer=answer+")";
+ startAbs=false;
+ }else{
+ console.log("Beginning absolute.");
+ answer=answer+"Math.abs(";
+ startAbs=true;
+ }
+ }
+ console.log("Answer is currently: "+answer);
+ }
+
+ return eval(answer);
+}
+
var questions = $('.question');
function createQuestion (n) {
@@ -8,10 +36,11 @@ function createQuestion (n) {
var theirAnswer = parseInt(qn.find('.answer').val());
console.log("Their answer:" + theirAnswer);
console.log("Right answer text"+ qn.find('.q').text());
- var answerText = eval(qn.find('.q').text());
- console.log("Right answer evaluated"+eval(qn.find('.q').text()));
- console.log("Absdolute value of it is: "+Math.abs(answerText));
- var rightAnswer = Math.abs(answerText);
+ var answerText = qn.find('.q').text();
+ console.log("Running our algorithm on: "+answerText);
+ var answer = solveAbsolutes(answerText);
+ console.log("Right answer evaluated"+answer);
+ var rightAnswer = answer;
console.log("The right answer is: "+rightAnswer);
if (theirAnswer === rightAnswer){
qn.hide();

0 comments on commit a86e6f0

Please sign in to comment.