mirror of
https://github.com/Xevion/power-math.git
synced 2025-12-06 15:15:55 -06:00
create own client side arithmetic functions
This commit is contained in:
51
client/src/arithmetic.js
Normal file
51
client/src/arithmetic.js
Normal file
@@ -0,0 +1,51 @@
|
||||
import utils from './utils.js';
|
||||
|
||||
export default {
|
||||
methods: {
|
||||
// Generate a addition problem
|
||||
addition(mult = 1) {
|
||||
let a = utils.methods.getRandomInt(5 * mult, 100 * mult);
|
||||
let b = utils.methods.getRandomInt(5 * mult, 100 * mult);
|
||||
return {
|
||||
text: `${a} + ${b}`,
|
||||
answer: a + b
|
||||
}
|
||||
},
|
||||
subtraction(mult = 1) {
|
||||
let a = utils.methods.getRandomInt(5 * mult, 100 * mult);
|
||||
let b = utils.methods.getRandomInt(5 * mult, 100 * mult);
|
||||
if (Math.random() > 0.5) {
|
||||
return {
|
||||
text: `${a} - ${b}`,
|
||||
answer: a - b,
|
||||
}
|
||||
}
|
||||
else {
|
||||
return {
|
||||
text: `-${a} + ${b}`,
|
||||
answer: -a + b
|
||||
}
|
||||
}
|
||||
},
|
||||
multiplication(mult = 1) {
|
||||
let a = utils.methods.getRandomInt(3 * mult, 30 * mult);
|
||||
let b = utils.methods.getRandomInt(3 * mult, 15 * mult);
|
||||
return {
|
||||
text: `${a} \\times ${b}`,
|
||||
answer: a * b
|
||||
}
|
||||
},
|
||||
square_root(mult = 1) {
|
||||
let a = utils.methods.getRandomInt(2 * mult, 20 * mult);
|
||||
return {
|
||||
text: `\\sqrt{${a * a}}`,
|
||||
answer: a
|
||||
}
|
||||
},
|
||||
getProblem: function () {
|
||||
let possible = [this.multiplication, this.square_root];
|
||||
let index = utils.methods.getRandomInt(0, possible.length);
|
||||
return possible[index]();
|
||||
}
|
||||
}
|
||||
}
|
||||
9
client/src/utils.js
Normal file
9
client/src/utils.js
Normal file
@@ -0,0 +1,9 @@
|
||||
export default {
|
||||
methods: {
|
||||
getRandomInt(min, max) {
|
||||
min = Math.ceil(min);
|
||||
max = Math.floor(max);
|
||||
return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user