diff --git a/Maths Hero/scripts/updated-maths-engine.js b/Maths Hero/scripts/updated-maths-engine.js new file mode 100644 index 0000000..ceca00c --- /dev/null +++ b/Maths Hero/scripts/updated-maths-engine.js @@ -0,0 +1,94 @@ + +//---------------- MATHS ENGINE -------------- + +// These need re-scoping when the math engine is working +var ranNum1, ranNum2, sum; +var currentSymbol; +var userAnswer; + +//----------------------------------------------------------------------------------------------------- +// randomly generate the first and second numbers +function generateTheNumbers() { + + // Generate the first random number for the sum + ranNum1 = Math.floor(Math.random()*12); + if (ranNum1 == 0) { + ranNum1 = 1; + } + console.log("(generateTheNumbers) First random number: " + ranNum1); + + // Generate the second random number for the sum + ranNum2 = Math.floor(Math.random()*12); + if (ranNum2 == 0) { + ranNum2 = 1; + } + console.log("(generateTheNumbers) Second random number: " + ranNum2); + + // Check if the first number is bigger than the second, if so flip them + if (ranNum1 < ranNum2) { + console.log("(generateTheNumbers) ranNum1 (" + ranNum1 + ") is LESS than ranNum2 (" + ranNum2 + ") - flipping"); + ranNum2 = [ranNum1, ranNum1 = ranNum2][0]; // switch values + console.log("(generateTheNumbers) RanNum1 is now (" + ranNum1 + ") and ranNum2 is now (" + ranNum2 + ")"); + } else { + console.log("(generateTheNumbers) Numbers are the same or ranNum1 is GREATER than ranNum2"); + } + console.log("---------------"); + + // Return the values so they can be reached outside of this function + return ranNum1, ranNum2; +} + +//----------------------------------------------------------------------------------------------------- +// randomly choose one of the operators +function generateTheOperatorSymbol() { + var operatorSymbols = ["+", "-", "x", "/"]; + var operatorIndex = Math.floor(Math.random()*(operatorSymbols.length)); + console.log("(generateTheOperatorSymbol) The operatorIndex is: " + operatorIndex); + currentSymbol = operatorSymbols[operatorIndex]; + console.log("(generateTheOperatorSymbol) The current symbol is: " + currentSymbol); + + console.log("---------------"); + + //return currentSymbol; +} + +//----------------------------------------------------------------------------------------------------- +function createAndRunTheSum() { + console.log("(createAndRunTheSum) ranNum1 is (" + ranNum1 + ") and ranNum2 is (" + ranNum2 + ")"); + console.log("(createAndRunTheSum) The current symbol is: " + currentSymbol); + if (currentSymbol === "+") { + sum = ranNum1 + ranNum2; + } else if (currentSymbol === "-") { + sum = ranNum1 - ranNum2; + } else if (currentSymbol === "x") { + sum = ranNum1 * ranNum2; + } else if (currentSymbol === "/") { + sum = (ranNum1 / ranNum2).toFixed(0); + remainder = (ranNum1 % ranNum2); + } else { + console.log("(createAndRunTheSum) Looks like an issue with the symbol"); + } + + console.log("(createAndRunTheSum) Sum is: " + ranNum1 + " " + currentSymbol + " " + ranNum2 + " = " + sum); + + if (currentSymbol === "/") { + console.log("(createAndRunTheSum) The answer is: " + sum + " with a remainder of " + remainder); + } + + console.log("---------------"); +} + +//----------------------------------------------------------------------------------------------------- +// TEMPORARY HACK until we get the user entering the sum +function testForUserAnswer() { + userAnswer = sum; + console.log("(testForUserAnswer) The User Answer is (the same as sum): " + userAnswer); +} + +//----------------------------------------------------------------------------------------------------- + +// Run each function in order +generateTheNumbers(); +generateTheOperatorSymbol(); +createAndRunTheSum(); +testForUserAnswer(); \ No newline at end of file diff --git a/README.md b/README.md index ace8676..14a432d 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ If there's anything that you think could be improved due to misunderstanding, th # The 1000 Things Structure of the repo Things are as *folder > main js file*, along with the supporting HTML and CSS files. +(8) Maths Hero > updated-maths-engine.js
(7) Game Sounds > soundPlayer.js
(6) Name Changer > namechange.js
(5) Message Changer > messageChanger.js
@@ -24,7 +25,7 @@ Structure of the repo Things are as *folder > main js file*, along with the supp (2) Date and Time > myYearAndDate.js
(1) Date and Time > myDayFunction.js -**Only 993 to go... o_O** +**Only 992 to go... o_O** # Courses and Sources **[https://www.sololearn.com/Course/JavaScript/](https://www.sololearn.com/Course/JavaScript/)**