Skip to content
Browse files

add except argument in getRandomNumber and a pattern to manage params

  • Loading branch information...
1 parent 0ed4b3b commit b2e6228e4067ff560d84d69afeaed4a4c3a6c5b4 @acazsouza committed Apr 22, 2012
Showing with 256 additions and 80 deletions.
  1. +25 −5 src/index.html
  2. +148 −38 src/tests/TwinooTests.html
  3. +83 −37 src/twinoo.js
View
30 src/index.html
@@ -3,6 +3,10 @@
<head>
<meta charset="utf-8">
<title>Twinoo</title>
+
+ <style type="text/css">
+
+ </style>
</head>
<body>
<p id="expression"></p>
@@ -11,15 +15,31 @@
<span id="secondNumber"></span>
<span id="thirdNumber"></span>
+ <script type="text/javascript" src="../libs/jquery-1.7.2/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="twinoo.js"></script>
<script type="text/javascript">
window.onload = function () {
- var expression = getExpressionString(getRandomNumber(), getRandomOperator(operators), getRandomNumber());
+ var operators = ['+', '+', '+', '-', '-', '-', '/', '/', '*', '*'];
+
+ var operator = getRandomOperator(operators);
+ var firstNumber = getFirstNumber(operator);
+ var secondNumber = getSecondNumber(firstNumber, operator);
+ var expression = buildExpression(firstNumber, operator, secondNumber);
+
+ document.getElementById('expression').innerHTML = firstNumber.toString() + ' ' + operator.toString() + ' ' + secondNumber.toString();
+
+ var fields = [document.getElementById('firstNumber'), document.getElementById('secondNumber'), document.getElementById('thirdNumber')]
+
+ var index = (getRandomNumber({ limit: 3, prevRandomNumberId: "indexFields" }) - 1);
+
+ for (var i = 0; i <= 2; i++) {
+ if (index != i)
+ fields[i].innerHTML = getRandomNumber({
+ except: expression.correctAnswer
+ });
+ }
- document.getElementById("expression").innerHTML = expression;
- document.getElementById("firstNumber").innerHTML = getAnswer(expression);
- document.getElementById("secondNumber").innerHTML = getRandomNumber();
- document.getElementById("thirdNumber").innerHTML = getRandomNumber();
+ fields[index].innerHTML = expression.correctAnswer;
}
</script>
</body>
View
186 src/tests/TwinooTests.html
@@ -14,7 +14,7 @@
<script type="text/javascript">
$(function () {
- module('getRandomNumber(limit) Tests');
+ module('getRandomNumber() Tests');
test("getRandomNumber() should return a number between 1..9", function () {
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
@@ -23,25 +23,25 @@
equal(-1 < $.inArray(getRandomNumber(), numbers), true);
});
- test("getRandomNumber(anyNumber) should return a number between 1..anyNumber", function () {
+ test("getRandomNumber({ limit: anyNumber }) should return a number between 1..anyNumber", function () {
var result;
for (var i = 1; i <= 9; i++) {
- result = getRandomNumber(i);
+ result = getRandomNumber({ limit: i });
equal((0 < result && result < (i + 1)), true);
}
});
- test("getRandomNumber(1) should return 1", function () {
- equal(1 == getRandomNumber(1), true);
+ test("getRandomNumber({ limit: 1 }) should return 1", function () {
+ equal(1 == getRandomNumber({ limit: 1 }), true);
});
test("getRandomNumber() should never return the same number once and again", function () {
var prevNumber, actualNumber, assertResult;
for (var i = 0; i <= 200; i++) {
- actualNumber = getRandomNumber(9);
+ actualNumber = getRandomNumber({ limit: 9 });
assertResult = prevNumber != actualNumber;
equal(assertResult, true);
@@ -53,25 +53,37 @@
}
});
- module('getFirstNumber Tests');
+ test("getRandomNumber({ except: anyNumber }) should return a number between 1..9 except anyNumber", function () {
+ for (var i = 0; i <= 100; i++)
+ equal(6 != getRandomNumber({ except: 6 }), true);
+ });
+
+
- test("getFirstNumber('+') should return a single number for sum operations that result in a single number", function () {
+ module('getFirstNumber() Tests');
+
+ test("getFirstNumber('+') should return a number between 1..8", function () {
var operator = '+';
var numbers = [1, 2, 3, 4, 5, 6, 7, 8];
for (var i = 0; i <= 100; i++)
equal(-1 < $.inArray(getFirstNumber(operator), numbers), true);
});
- test("getFirstNumber('-') should return a single number for substraction operations that result in a single number", function () {
+ test("getFirstNumber('-') should return a number between 2..9", function () {
var operator = '-';
- var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
+ var numbers = [2, 3, 4, 5, 6, 7, 8, 9];
for (var i = 0; i <= 100; i++)
equal(-1 < $.inArray(getFirstNumber(operator), numbers), true);
});
- test("getFirstNumber('*') should return a single number for multiplication operations that result in a single number", function () {
+ test("getFirstNumber('-') should not return 1", function () {
+ for (var i = 0; i <= 100; i++)
+ equal(1 < getFirstNumber('-'), true);
+ });
+
+ test("getFirstNumber('*') should return a number between 1..9", function () {
var operator = '*';
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
@@ -87,6 +99,11 @@
equal(-1 < $.inArray(getFirstNumber(operator), numbers), true);
});
+ test("getFirstNumber('/') should not return 1", function () {
+ for (var i = 0; i <= 100; i++)
+ equal(1 < getFirstNumber('/'), true);
+ });
+
module('getSecondNumber Tests');
@@ -163,68 +180,57 @@
equal(-1 < $.inArray(getSecondNumber(9, operator), numbers), true);
});
- test("getSecondNumber(8, '-') should return a number between 1..7 and 9", function () {
+ test("getSecondNumber(8, '-') should return a number between 1..7", function () {
var operator = '-';
- var numbers = [1, 2, 3, 4, 5, 6, 7, 9];
+ var numbers = [1, 2, 3, 4, 5, 6, 7];
for (var i = 0; i <= 100; i++)
equal(-1 < $.inArray(getSecondNumber(8, operator), numbers), true);
});
- test("getSecondNumber(7, '-') should return a number between 1..6 and 8..9", function () {
+ test("getSecondNumber(7, '-') should return a number between 1..6 ", function () {
var operator = '-';
- var numbers = [1, 2, 3, 4, 5, 6, 8, 9];
+ var numbers = [1, 2, 3, 4, 5, 6];
for (var i = 0; i <= 100; i++)
equal(-1 < $.inArray(getSecondNumber(7, operator), numbers), true);
});
- test("getSecondNumber(6, '-') should return a number between 1..5 and 7..9", function () {
+ test("getSecondNumber(6, '-') should return a number between 1..5", function () {
var operator = '-';
- var numbers = [1, 2, 3, 4, 5, 7, 8, 9];
+ var numbers = [1, 2, 3, 4, 5];
for (var i = 0; i <= 100; i++)
equal(-1 < $.inArray(getSecondNumber(6, operator), numbers), true);
});
- test("getSecondNumber(5, '-') should return a number between 1..4 and 6..9", function () {
+ test("getSecondNumber(5, '-') should return a number between 1..4", function () {
var operator = '-';
- var numbers = [1, 2, 3, 4, 6, 7, 8, 9];
+ var numbers = [1, 2, 3, 4];
for (var i = 0; i <= 100; i++)
equal(-1 < $.inArray(getSecondNumber(5, operator), numbers), true);
});
- test("getSecondNumber(4, '-') should return a number between 1..3 and 5..9", function () {
+ test("getSecondNumber(4, '-') should return a number between 1..3", function () {
var operator = '-';
- var numbers = [1, 2, 3, 5, 6, 7, 8, 9];
+ var numbers = [1, 2, 3];
for (var i = 0; i <= 100; i++)
equal(-1 < $.inArray(getSecondNumber(4, operator), numbers), true);
});
- test("getSecondNumber(3, '-') should return a number between 1..2 and 4..9", function () {
+ test("getSecondNumber(3, '-') should return a number between 1..2", function () {
var operator = '-';
- var numbers = [1, 2, 4, 5, 6, 7, 8, 9];
+ var numbers = [1, 2];
for (var i = 0; i <= 100; i++)
equal(-1 < $.inArray(getSecondNumber(3, operator), numbers), true);
});
- test("getSecondNumber(2, '-') should return a number between 1 and 3..9", function () {
- var operator = '-';
- var numbers = [1, 3, 4, 5, 6, 7, 8, 9];
-
- for (var i = 0; i <= 100; i++)
- equal(-1 < $.inArray(getSecondNumber(2, operator), numbers), true);
- });
-
- test("getSecondNumber(1, '-') should return a number between 2..9", function () {
- var operator = '-';
- var numbers = [2, 3, 4, 5, 6, 7, 8, 9];
-
+ test("getSecondNumber(2, '-') should return number 1", function () {
for (var i = 0; i <= 100; i++)
- equal(-1 < $.inArray(getSecondNumber(1, operator), numbers), true);
+ equal(1 == getSecondNumber(2, '-'), true);
});
test("getSecondNumber(1, '*') should return a number between 1..9", function () {
@@ -304,34 +310,138 @@
});
test("getSecondNumber(2, '/') should return number 1 or 2", function () {
+ var secondNumber, result;
+
+ for (var i = 0; i <= 100; i++) {
+ secondNumber = getSecondNumber(2, '/');
+ result = (1 <= secondNumber && secondNumber <= 2);
+
+ equal(result, true);
+
+ if (!result)
+ break;
+ }
});
test("getSecondNumber(3, '/') should return number 1 or 3", function () {
+ var secondNumber, result;
+
+ for (var i = 0; i <= 100; i++) {
+ secondNumber = getSecondNumber(3, '/');
+ result = (1 == secondNumber || secondNumber == 3);
+ equal(result, true);
+
+ if (!result)
+ break;
+ }
});
test("getSecondNumber(4, '/') should return a number between 1..2 and 4", function () {
+ var secondNumber, result;
+
+ for (var i = 0; i <= 100; i++) {
+ secondNumber = getSecondNumber(4, '/');
+ result = ((1 <= secondNumber && secondNumber <= 2) || secondNumber == 4);
+
+ equal(result, true);
+ if (!result)
+ break;
+ }
});
test("getSecondNumber(5, '/') should return number 1 or 5", function () {
+ var secondNumber, result;
+ for (var i = 0; i <= 100; i++) {
+ secondNumber = getSecondNumber(5, '/');
+ result = (1 == secondNumber || secondNumber == 5);
+
+ equal(result, true);
+
+ if (!result)
+ break;
+ }
});
- test("getSecondNumber(6, '/') should return a number between 1..4 and 6", function () {
+ test("getSecondNumber(6, '/') should return a number between 1..3 and 6", function () {
+ var secondNumber, result;
+
+ for (var i = 0; i <= 100; i++) {
+ secondNumber = getSecondNumber(6, '/');
+ result = ((1 <= secondNumber && secondNumber <= 3) || secondNumber == 6);
+
+ equal(result, true);
+ if (!result)
+ break;
+ }
});
test("getSecondNumber(7, '/') should return number 1 or 7", function () {
+ var secondNumber, result;
+ for (var i = 0; i <= 100; i++) {
+ secondNumber = getSecondNumber(7, '/');
+ result = (1 == secondNumber || secondNumber == 7);
+
+ equal(result, true);
+
+ if (!result)
+ break;
+ }
});
test("getSecondNumber(8, '/') should return a number between 1..2 and 4 and 8", function () {
+ var secondNumber, result;
+
+ for (var i = 0; i <= 100; i++) {
+ secondNumber = getSecondNumber(8, '/');
+ result = ((1 <= secondNumber && secondNumber <= 2) || secondNumber == 4 || secondNumber == 8);
+ equal(result, true);
+
+ if (!result)
+ break;
+ }
});
- test("getSecondNumber(9, '/') should return number 1 and 3 and 9", function () {
+ test("getSecondNumber(9, '/') should return number 1 or 3 or 9", function () {
+ var secondNumber, result;
+
+ for (var i = 0; i <= 100; i++) {
+ secondNumber = getSecondNumber(9, '/');
+ result = (secondNumber == 1 || secondNumber == 3 || secondNumber == 9);
+
+ equal(result, true);
+
+ if (!result)
+ break;
+ }
+ });
+
+
+
+ module('buildExpression() Tests');
+
+ test("buildExpression(firstNumber, operator, secondNumber) should return a filled expression object", function () {
+ var operators = ['+', '+', '+', '+', '-', '-', '-', '/', '/', '*'];
+ var operator = operators[Math.floor((Math.random() * operators.length))];
+ var firstNumber = getFirstNumber(operator);
+ var secondNumber = getSecondNumber(firstNumber, operator);
+
+ var expression = buildExpression(firstNumber, operator, secondNumber);
+
+ equal(null != expression.firstNumber, true)
+ equal(null != expression.operator, true)
+ equal(null != expression.secondNumber, true)
+ equal(null != expression.correctAnswer, true)
+ equal(firstNumber == expression.firstNumber, true)
+ equal(operator == expression.operator, true)
+ equal(secondNumber == expression.secondNumber, true)
+ equal((Number(eval(firstNumber.toString() + operator.toString() + secondNumber.toString()))) == expression.correctAnswer, true)
});
});
</script>
View
120 src/twinoo.js
@@ -1,7 +1,5 @@
//http://www.kongregate.com/games/zigah111/twinoo-the-brain-train
-var operators = ['+', '+', '+', '+', '-', '-', '-', '/', '/', '*'];
-
function getRandomOperator(operators) {
var length = operators.length;
var randomOperator = operators[Math.floor(Math.random() * length)];
@@ -11,73 +9,121 @@ function getRandomOperator(operators) {
function getFirstNumber(operator) {
if ('+' == operator) {
- return getRandomNumber(8);
+ return getRandomNumber({ limit: 8 });
} else if ('-' == operator) {
- return getRandomNumber(9);
+ return getRandomNumber({ limit: 8, indexDelay: 2 });
} else if ('*' == operator) {
- return getRandomNumber(9);
+ return getRandomNumber({ limit: 9 });
} else if ('/' == operator) {
- return getRandomNumber(7, 2);
+ return getRandomNumber({ limit: 8, indexDelay: 2 });
}
return null;
}
function getSecondNumber(firstNumber, operator) {
if ('+' == operator) {
- return getRandomNumber(9 - firstNumber);
+ return getRandomNumber({ limit: 9 - firstNumber });
} else if ('-' == operator) {
- if (9 == firstNumber)
- return getRandomNumber(8);
- else {
- var randomNumber = getRandomNumber(9);
- while (firstNumber == randomNumber) {
- randomNumber = getRandomNumber(9);
- }
-
- return randomNumber;
- }
+ return getRandomNumber({ limit: 9 - (10 - firstNumber) });
} else if ('*' == operator) {
if (2 == firstNumber) {
- return getRandomNumber(4);
+ return getRandomNumber({ limit: 4 });
} else if (3 == firstNumber) {
- return getRandomNumber(3);
+ return getRandomNumber({ limit: 3 });
} else if (4 == firstNumber) {
- return getRandomNumber(2);
+ return getRandomNumber({ limit: 2 });
}
return 1;
+ } else if ('/' == operator) {
+ if (2 == firstNumber) {
+ return getRandomNumber({ limit: 2 });
+ } else if (3 == firstNumber) {
+ var randomNumber = getRandomNumber({ limit: 3 });
+ while (2 == randomNumber) {
+ randomNumber = getRandomNumber({ limit: 3 });
+ }
+
+ return randomNumber;
+ } else if (4 == firstNumber) {
+ return getRandomNumber({ limit: 2 });
+ } else if (5 == firstNumber) {
+ var randomNumber = getRandomNumber({ limit: 5 });
+ while (2 == randomNumber || 3 == randomNumber || 4 == randomNumber) {
+ randomNumber = getRandomNumber({ limit: 5 });
+ }
+
+ return randomNumber;
+ } else if (6 == firstNumber) {
+ var randomNumber = getRandomNumber({ limit: 6 });
+ while (5 == randomNumber || 4 == randomNumber) {
+ randomNumber = getRandomNumber({ limit: 6 });
+ }
+
+ return randomNumber;
+ } else if (7 == firstNumber) {
+ var randomNumber = getRandomNumber({ limit: 5 });
+ while (2 == randomNumber || 3 == randomNumber || 4 == randomNumber || 5 == randomNumber || 6 == randomNumber) {
+ randomNumber = getRandomNumber({ limit: 5 });
+ }
+
+ return randomNumber;
+ } else if (8 == firstNumber) {
+ var randomNumber = getRandomNumber({ limit: 8 });
+ while (3 == randomNumber || 5 == randomNumber || 6 == randomNumber || 7 == randomNumber) {
+ randomNumber = getRandomNumber({ limit: 8 });
+ }
+
+ return randomNumber;
+ } else if (9 == firstNumber) {
+ var randomNumber = getRandomNumber({ limit: 9 });
+ while (2 == randomNumber || 4 == randomNumber || 5 == randomNumber || 6 == randomNumber || 7 == randomNumber || 8 == randomNumber) {
+ randomNumber = getRandomNumber({ limit: 9 });
+ }
+
+ return randomNumber;
+ }
}
return null;
}
-window.prevRandomNumber = -1;
-function getRandomNumber(limit, indexDelay) {
- //limit = Number(limit);
- if (!limit) limit = 9;
- if (!indexDelay) indexDelay = 1;
+function getRandomNumber(params) {
+ var o = {
+ limit: 9,
+ indexDelay: 1,
+ except: null,
+ prevRandomNumberId: "universalPrevRandomNumberId"
+ }
+
+ for (param in params) {
+ o[param] = params[param];
+ }
- if (1 == limit) {
- window.prevRandomNumber = -1;
+ if (1 == o.limit) {
+ prevRandomNumber = -1;
return 1;
}
- var actualRandomNumber = Math.floor((Math.random() * limit) + indexDelay);
+ var actualRandomNumber = Math.floor((Math.random() * o.limit) + o.indexDelay);
- while (window.prevRandomNumber == actualRandomNumber) {
- actualRandomNumber = Math.floor((Math.random() * limit) + indexDelay)
+ while (window[o.prevRandomNumberId] == actualRandomNumber || (o.except && (actualRandomNumber == o.except))) {
+ actualRandomNumber = Math.floor((Math.random() * o.limit) + o.indexDelay)
}
- window.prevRandomNumber = actualRandomNumber;
+ window[o.prevRandomNumberId] = actualRandomNumber;
return actualRandomNumber;
}
-/*function getExpressionString(firstNumber, operator, secondNumber) {
- return (firstNumber.toString() + operator.toString() + secondNumber.toString());
-}
+function buildExpression(firstNumber, operator, secondNumber) {
+ var answer = Number(eval(firstNumber.toString() + operator.toString() + secondNumber.toString()));
-function getAnswer(expression) {
- return eval(expression);
-}*/
+ return {
+ firstNumber: firstNumber,
+ operator: operator,
+ secondNumber: secondNumber,
+ correctAnswer: answer
+ }
+}

0 comments on commit b2e6228

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