Skip to content

Commit

Permalink
add problem generator that can handle levels and disable on button wh…
Browse files Browse the repository at this point in the history
…en on
  • Loading branch information
barelyknown committed Dec 21, 2015
1 parent 384e4c0 commit 1745830
Show file tree
Hide file tree
Showing 4 changed files with 306 additions and 20 deletions.
34 changes: 16 additions & 18 deletions app/controllers/application.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Ember from 'ember';
import ProblemGenerator from 'little-professor/models/problem-generator';

export default Ember.Controller.extend({
levels: [1,2,3,4],
Expand All @@ -8,7 +9,9 @@ export default Ember.Controller.extend({
isRetrying: Ember.computed.gt('tryCount', 0),

problem: Ember.computed('problems', 'problemIndex', function() {
return this.get('problems').objectAt(this.get('problemIndex'));
if (Ember.isPresent(this.get('problems')) && Ember.isPresent(this.get('problemIndex'))) {
return this.get('problems').objectAt(this.get('problemIndex'));
}
}),

isComplete: Ember.computed('problems', 'problemIndex', function() {
Expand Down Expand Up @@ -51,10 +54,12 @@ export default Ember.Controller.extend({

actions: {
turnOn() {
this.set('isOn', true);
this.set('level', this.get('levels').objectAt(0));
this.set('operator', '+');
this.set('mode', 'settings');
if (!this.get('isOn')) {
this.set('isOn', true);
this.set('level', this.get('levels').objectAt(0));
this.set('operator', '+');
this.set('mode', 'settings');
}
},

turnOff() {
Expand Down Expand Up @@ -115,20 +120,12 @@ export default Ember.Controller.extend({
termOne = t[uniqueRandomIndexes[problems.length]][0];
termTwo = t[uniqueRandomIndexes[problems.length]][1];
break;
case '-':
termOne = Math.floor(Math.random() * (10 + 1));
termTwo = Math.floor(Math.random() * (termOne + 1));
break;
default:
termOne = Math.floor(Math.random() * 10);
termTwo = Math.floor(Math.random() * 10);
}
let problem = this.store.createRecord('problem', {
termOne: termOne,
termTwo: termTwo,
operator: this.get('operator')
const problemGenerator = ProblemGenerator.create({
operator: this.get('operator'),
level: this.get('level')
});
problems.pushObject(problem);
problems.pushObject(problemGenerator.generate());
}
this.set('problems', problems);
this.send('nextProblem');
Expand Down Expand Up @@ -179,11 +176,12 @@ export default Ember.Controller.extend({
this.set('isIncorrect', true);
Ember.run.later(this, function() {
this.set('isIncorrect', false);
this.set('isAnswerLocked', false);
if (this.get('tryCount') === 3) {
this.set('answer', this.get('problem.answer'));
this.toggleProperty('isDisplayingAnswer');
this.set('isAnswerLocked', true);
} else {
this.set('isAnswerLocked', false);
this.set('answer', null);
}
}, 1000);
Expand Down
259 changes: 259 additions & 0 deletions app/models/problem-generator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
import Ember from 'ember';
import Problem from 'little-professor/models/problem';

export default Ember.Object.extend({
operator: null,

level: null,

minTermOne: Ember.computed('operator', 'level', function() {
switch (this.get('operator')) {
case '+':
switch (this.get('level')) {
case 3:
return 11;
case 4:
return 11;
}
case '-':
return this.get('minAnswer') + this.get('minTermTwo');
case '*':
switch (this.get('level')) {
case 1:
return 1;
case 2:
return 2;
case 3:
return 3;
case 4:
return 3;
}
default:
return 0;
}
}),

maxTermOne: Ember.computed('operator', 'level', function() {
switch (this.get('operator')) {
case '+':
switch (this.get('level')) {
case 1:
return 5;
case 2:
return 10;
case 3:
return 25;
case 4:
return 51;
}
case '-':
switch (this.get('level')) {
case 1:
return 9;
case 2:
return 20;
case 3:
return 50;
case 4:
return 99;
}
case '*':
switch (this.get('level')) {
case 1:
return 5;
case 2:
return 9;
case 3:
return 12;
case 4:
return 19;
}
case '/':
return this.get('minTermTwo') * this.get('maxAnswer');
}
}),

minTermTwo: Ember.computed('operator', 'level', function() {
switch (this.get('operator')) {
case '*':
return this.get('minTermOne');
break;
case '-':
switch (this.get('level')) {
case 3:
return 11;
case 4:
return 11;
default:
return 0;
}
case '/':
switch (this.get('level')) {
case 1:
return 1;
break;
case 2:
return 2;
break;
case 3:
return 3;
break;
case 4:
return 11;
break;
}
default:
return 0;
}
}),

maxTermTwo: Ember.computed('operator', 'level', function() {
switch (this.get('operator')) {
case '/':
return this.get('maxTermOne') / this.get('minAnswer');
default:
return 99;
}
}),

minAnswer: Ember.computed('operator', 'level', function() {
switch (this.get('operator')) {
case '+':
switch (this.get('level')) {
case 1:
return 0;
case 2:
return 10;
case 3:
return 20;
case 4:
return 60;
}
case '-':
switch (this.get('level')) {
case 3:
return 10;
case 4:
return 25;
default:
return 0;
}
case '*':
return 1;
case '/':
switch (this.get('level')) {
case 1:
return 1;
break;
case 2:
return 2;
break;
case 3:
return 3;
break;
case 4:
return 3;
break;
}
default:
return 0;
}
}),

maxAnswer: Ember.computed('operator', 'level', function() {
switch (this.get('operator')) {
case '+':
switch (this.get('level')) {
case 1:
return 9;
case 2:
return 20;
case 3:
return 50;
case 4:
return 99;
}
case '-':
switch (this.get('level')) {
case 1:
return 9;
case 2:
return 9;
case 3:
return 20;
case 4:
return 80;
}
case '*':
switch (this.get('level')) {
case 1:
return 10;
case 2:
return 20;
case 3:
return 50;
case 4:
return 99;
}
case '/':
switch (this.get('level')) {
case 1:
return 3;
case 2:
return 9;
case 3:
return 20;
case 4:
return 9;
};
}
}),

_randomBetween(minimum, maximum) {
return Math.floor(Math.random() * (maximum + 1 - minimum) + minimum);
},

generate() {
let termOne;
let termTwoCeiling;
let termTwoFloor;
let termTwo;

termOne = this._randomBetween(this.get('minTermOne'), this.get('maxTermOne'));

switch (this.get('operator')) {
case '+':
termTwoCeiling = this.get('maxAnswer') - termOne;
termTwoFloor = Math.max(this.get('minAnswer') - termOne, 0);
termTwo = this._randomBetween(termTwoFloor, termTwoCeiling);
break;
case '-':
termTwoCeiling = Math.max(termOne - this.get('maxAnswer'), this.get('minTermTwo'));
termTwoFloor = Math.max(termOne - this.get('minAnswer'), this.get('minTermTwo'));
termTwo = this._randomBetween(termTwoFloor, termTwoCeiling);
break;
case '*':
termTwoCeiling = Math.floor(this.get('maxAnswer') / termOne);
termTwoFloor = Math.max(Math.ceil(this.get('minAnswer') / termOne), this.get('minTermTwo'));
termTwo = this._randomBetween(termTwoFloor, termTwoCeiling);
break;
case '/':
termTwoFloor = this.get('minTermTwo');
termTwoCeiling = this.get('maxTermTwo');
termTwo = this._randomBetween(termTwoFloor, termTwoCeiling);
const termOnes = [];
var m = this.get('minAnswer');
while (m * termTwo <= this.get('maxTermOne')) {
termOnes.pushObject(m * termTwo);
m += 1;
}
termOne = termOnes[this._randomBetween(0, termOnes.length - 1)];
}

return Problem.create({
operator: this.get('operator'),
termOne: termOne,
termTwo: termTwo
});
}
});
4 changes: 2 additions & 2 deletions app/models/problem.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import DS from 'ember-data';
import Ember from 'ember';

export default DS.Model.extend({
export default Ember.Object.extend({
termOne: null,
termTwo: null,
operator: null,
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/models/problem-generator-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { moduleFor, test } from 'ember-qunit';

moduleFor('model:problem-generator', 'Unit | problem generator', {
unit: true
});

const operators = ['+','-','*','/'];
const levels = [1,2,3,4];

operators.forEach((operator) => {
levels.forEach((level) => {
test(`${operator} L ${level}`, function(assert) {
var t = 0;
while (t < 100) {
const subject = this.subject();
subject.setProperties({operator: operator, level: level});
const problem = subject.generate();
const terms = [problem.get('termOne'), problem.get('termTwo')];
assert.ok(problem.get('termOne') >= subject.get('minTermOne'), `termOne is ${problem.get('termOne')}, minTermOne is ${subject.get('minTermOne')}. ${terms.join(',')}`);
assert.ok(problem.get('termOne') <= subject.get('maxTermOne'), `termOne is ${problem.get('termOne')}, maxTermOne is ${subject.get('maxTermOne')}. ${terms.join(',')}`);
assert.ok(problem.get('termTwo') >= subject.get('minTermTwo'), `termTwo is ${problem.get('termTwo')}, minTermTwo is ${subject.get('minTermTwo')}. ${terms.join(',')}`);
assert.ok(problem.get('termTwo') <= subject.get('maxTermTwo'), `termTwo is ${problem.get('termTwo')}, maxTermTwo is ${subject.get('maxTermTwo')}. ${terms.join(',')}`);
assert.ok(problem.get('answer') <= subject.get('maxAnswer'), `answer is ${problem.get('answer')}, max answer is ${subject.get('maxAnswer')}. ${terms.join(',')}`);
assert.ok(problem.get('answer') >= subject.get('minAnswer'), `answer is ${problem.get('answer')}, min answer is ${subject.get('minAnswer')}. ${terms.join(',')}`);
t += 1;
}
});
});
});

0 comments on commit 1745830

Please sign in to comment.