Skip to content

Commit e640ba3

Browse files
committed
Overhauled allAtOnce()'s code block & comments; added 2 helper functions to better handle output mssgs
1 parent 35a9562 commit e640ba3

1 file changed

Lines changed: 48 additions & 15 deletions

File tree

Project3_math_operators/JS/main.js

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,53 @@ function division() {
3131
}
3232

3333
// Steps 84, 86 & 92
34+
function firstHelper(result) {
35+
if (result % 2 == 0) {
36+
var result2 = result / 2;
37+
return `(${result} is <em>EVEN</em>, so "${result} &#247 2" = <strong>${result2}</strong>, ${lastHelper(result2)})`;
38+
} else {
39+
var result2 = result * 3 + 1;
40+
return `(${result} is <em>ODD</em>, so "${result} x 3" is ${
41+
result * 3
42+
}, and + 1 = <strong>${result2}</strong>, ${lastHelper(result2)})`;
43+
}
44+
}
45+
function lastHelper(result2) {
46+
if (result2 % 2 == 0) {
47+
return `...which is <em>EVEN</em>, so again, "${result2} &#247 2" = <strong>${result2 / 2}</strong>...`;
48+
} else {
49+
return `...which is <em>ODD</em>, so again we do "${result2} x 3", getting ${
50+
result2 * 3
51+
}, and adding 1 again = <strong>${result2 * 3 + 1}</strong>...`;
52+
}
53+
}
54+
3455
function allAtOnce() {
35-
var randNumb = Math.floor(Math.random() * 4) + 1; // Random integer between 1 & 3
36-
document.getElementById("RandNumb").innerHTML = `<strong>${randNumb}<strong>`; // >>>>> DOM is updated with the chosen random number
56+
var randNumb = Math.floor(Math.random() * 51) + 2; // Random integer between 2 & 50
57+
document.getElementById(
58+
"RandNumb",
59+
).innerHTML = `<li>A number between 2 and 50 was randomly chosen: <br><strong>${randNumb}</strong></li><ul id="Evens"></ul><ul id="Odds"></ul><br><li id="Results"></li>`; // >>>>> DOM is updated with the chosen random number & prepped to display function's next output.
3760
var result = randNumb; // Declare & assign a 2nd variable equal to the random number
3861
while (result != 1) {
39-
// While the 2nd variable is NOT 1...
62+
// As long as the 2nd variable is NOT 1...
4063
if (result % 2 == 0) {
41-
// Check if the 2nd variable is even
42-
var result = randNumb / 2; // Halve random number & reassign to the 2nd variable
43-
document.getElementById("Evens").innerHTML = `${result}`; // >>>>> DOM is updated with result from Line 40, GOTO to Line 38
64+
// ...check if the 2nd variable is even.
65+
var result = randNumb / 2; // If the random number IS even, halve it & reassign the result to the 2nd variable.
66+
document.getElementById("Evens").innerHTML = `<li>If even, &#247 2:<br>${result}</li>`; // >>>>> Update DOM with previous line's result.
67+
document.getElementById("Odds").innerHTML = `<li><s>If odd, x3 and then +1:</s></li>`; // User needs to see this to follow function's purpose/logic!
4468
} else {
4569
var result = randNumb * 3 + 1; // If 2nd variable is ODD, multiply by 3 and add 1
46-
document.getElementById("Odds").innerHTML = `${result}`; // >>>>> DOM is updated with result from Line 43, GOTO Line 38
70+
document.getElementById("Odds").innerHTML = `<li>If odd, x 3 and then +1:<br>${result}</li>`; // >>>>> Update DOM with previous line's result.
71+
document.getElementById("Evens").innerHTML = `<li><s>If even, &#247 2:</s></li>`; // User needs to see this to follow function's purpose/logic!
4772
}
48-
document.getElementById("Results").innerHTML = `<strong>${result}<strong>`; // >>>>> Once 2nd variable == 1, update DOM & END WHILE LOOP!!
49-
result = 1;
73+
document.getElementById(
74+
"Results",
75+
).innerHTML = `Keep repeating these steps with the results from above:<br>${firstHelper(
76+
result,
77+
)}<br><br>...and you'll eventually reach the number "1"!<br>(This math gem works regardless of <em>HOW LARGE</em> your starting number is!)`; // >>>>> GOTO while loop's entrance
78+
result = 1; // Thanks to Ben, was added to END WHILE LOOP!
5079
}
51-
}
80+
} // Function doesn't continuously calculate down to 1, yet.
5281

5382
// Steps 88 & 92:
5483
function negation() {
@@ -63,21 +92,25 @@ function increment() {
6392
newRandNum++;
6493
document.getElementById(
6594
"Increment",
66-
).innerHTML = `The value ${randNum} was <em>incremented</em> (by one) to ${newRandNum}.`;
95+
).innerHTML = `The value ${randNum} was <em>incremented</em> (by one) to <strong>${newRandNum}</strong>.`;
6796
}
6897
function decrement() {
6998
var randNumb = Math.floor(Math.random() * 101) + 1; // Random integer between 1 & 100
7099
var newRandNumb = randNumb; // Same as above; "--" INCREASED randNumb by 1, while "++" DECREASED it; same solution
71100
newRandNumb--;
72101
document.getElementById(
73102
"Decrement",
74-
).innerHTML = `The value ${randNumb} has been <em>decremented</em> (by one) to ${newRandNumb}.`;
103+
).innerHTML = `The value ${randNumb} has been <em>decremented</em> (by one) to <strong>${newRandNumb}</strong>.`;
75104
}
76105

77106
// Step 93
78107
function mathObjectMethod() {
79-
var randBase = Math.floor(Math.random() * 11) + 1; // Random integer (for the base) between 1 & 10
108+
var randBase = Math.floor(Math.random() * 16) + 1; // Random integer (for the base) between 1 & 15
80109
var randExp = Math.floor(Math.random() * 4) + 1; // Random integer (for the exponent) between 1 & 3
81-
document.getElementById("MathObjectMethod").innerHTML =
82-
`A base of ${randBase}, raised to the power of ${randExp} = ` + Math.pow(randBase, randExp);
110+
document.getElementById(
111+
"MathObjectMethod",
112+
).innerHTML = `A base of <u>${randBase}</u>, raised to the power of <u>${randExp}</u>, equals <strong>${Math.pow(
113+
randBase,
114+
randExp,
115+
)}</strong>.`;
83116
}

0 commit comments

Comments
 (0)