-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add problem generator that can handle levels and disable on button wh…
…en on
- Loading branch information
1 parent
384e4c0
commit 1745830
Showing
4 changed files
with
306 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}); | ||
}); | ||
}); |