You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Project3_math_operators/JS/main.js
+48-15Lines changed: 48 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -31,24 +31,53 @@ function division() {
31
31
}
32
32
33
33
// Steps 84, 86 & 92
34
+
functionfirstHelper(result){
35
+
if(result%2==0){
36
+
varresult2=result/2;
37
+
return`(${result} is <em>EVEN</em>, so "${result} ÷ 2" = <strong>${result2}</strong>, ${lastHelper(result2)})`;
38
+
}else{
39
+
varresult2=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
+
functionlastHelper(result2){
46
+
if(result2%2==0){
47
+
return`...which is <em>EVEN</em>, so again, "${result2} ÷ 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
+
34
55
functionallAtOnce(){
35
-
varrandNumb=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
+
varrandNumb=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.
37
60
varresult=randNumb;// Declare & assign a 2nd variable equal to the random number
38
61
while(result!=1){
39
-
// While the 2nd variable is NOT 1...
62
+
// As long as the 2nd variable is NOT 1...
40
63
if(result%2==0){
41
-
// Check if the 2nd variable is even
42
-
varresult=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
+
varresult=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, ÷ 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!
44
68
}else{
45
69
varresult=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, ÷ 2:</s></li>`;// User needs to see this to follow function's purpose/logic!
47
72
}
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!
50
79
}
51
-
}
80
+
}// Function doesn't continuously calculate down to 1, yet.
52
81
53
82
// Steps 88 & 92:
54
83
functionnegation(){
@@ -63,21 +92,25 @@ function increment() {
63
92
newRandNum++;
64
93
document.getElementById(
65
94
"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>.`;
67
96
}
68
97
functiondecrement(){
69
98
varrandNumb=Math.floor(Math.random()*101)+1;// Random integer between 1 & 100
70
99
varnewRandNumb=randNumb;// Same as above; "--" INCREASED randNumb by 1, while "++" DECREASED it; same solution
71
100
newRandNumb--;
72
101
document.getElementById(
73
102
"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>.`;
75
104
}
76
105
77
106
// Step 93
78
107
functionmathObjectMethod(){
79
-
varrandBase=Math.floor(Math.random()*11)+1;// Random integer (for the base) between 1 & 10
108
+
varrandBase=Math.floor(Math.random()*16)+1;// Random integer (for the base) between 1 & 15
80
109
varrandExp=Math.floor(Math.random()*4)+1;// Random integer (for the exponent) between 1 & 3
0 commit comments