Guest User

Untitled

a guest
Jun 13th, 2016
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.39 KB | None | 0 0
  1. /**
  2. * Created by Asher on 5/10/2016.
  3. */
  4. /*jshint strict: false */
  5.  
  6. $(document).ready(function(){
  7. $.ajax({
  8. url: '/alreadyLoginCheck',
  9. type: 'GET',
  10. success: function (data) {
  11. if(data == 'true') {
  12. document.getElementById("buttons").style.display = "block";
  13. document.getElementById('virtual screen').style.display = "none";
  14. }
  15. }
  16. });
  17. });
  18.  
  19. function doCalc()
  20. {
  21. document.getElementById("photo").style.display = "none";
  22. var divHere = document.getElementById("here");
  23.  
  24. //remove previous tabs
  25. while (divHere.firstChild) {
  26. divHere.removeChild(divHere.firstChild);
  27. }
  28.  
  29. $.ajax({
  30. url: '/calc/value/',
  31. type: 'GET',
  32. success: function (data) {
  33. console.log(data);
  34. var num = document.createElement("input");
  35. num.setAttribute("id","num");
  36. num.setAttribute("class", "text num");
  37. num.setAttribute("type", "text");
  38. num.value = data;
  39. divHere.appendChild(document.createElement("br"));
  40. divHere.appendChild(num);
  41. }
  42. });
  43.  
  44. divHere.appendChild(document.createElement("br"));
  45. divHere.appendChild(document.createElement("br"));
  46.  
  47. createMathButton("appendOne()", "1");
  48. createMathButton("appendTwo()", "2");
  49. createMathButton("appendThree()", "3");
  50. divHere.appendChild(document.createElement("br"));
  51. createMathButton("appendFour()", "4");
  52. createMathButton("appendFive()", "5");
  53. createMathButton("appendSix()", "6");
  54. divHere.appendChild(document.createElement("br"));
  55. createMathButton("appendSeven()", "7");
  56. createMathButton("appendEight()", "8");
  57. createMathButton("appendNine()", "9");
  58. divHere.appendChild(document.createElement("br"));
  59. createMathButton("plusFunc()", "+");
  60. createMathButton("appendZero()", "0");
  61. createMathButton("minusFunc()", "-");
  62. createMathButton("multFunc()", "*");
  63. divHere.appendChild(document.createElement("br"));
  64. createMathButton("divFunc()", "/");
  65. createMathButton("eqFunc()", "=");
  66. createMathButton("appendPoint()", ".");
  67. createMathButton("clearFunc()", "Clear");
  68.  
  69. }
  70. function appendPoint(){
  71. document.getElementById("num").value += ".";
  72. }
  73. function appendZero(){
  74. document.getElementById("num").value += 0;
  75. }
  76. function appendOne(){
  77. document.getElementById("num").value += 1;
  78. }
  79. function appendTwo(){
  80. document.getElementById("num").value += 2;
  81. }function appendThree(){
  82. document.getElementById("num").value += 3;
  83. }
  84. function appendFour(){
  85. document.getElementById("num").value += 4;
  86. }
  87. function appendFive(){
  88. document.getElementById("num").value += 5;
  89. }
  90. function appendSix(){
  91. document.getElementById("num").value += 6;
  92. }
  93. function appendSeven(){
  94. document.getElementById("num").value += 7;
  95. }
  96. function appendEight(){
  97. document.getElementById("num").value += 8;
  98. }
  99. function appendNine(){
  100. document.getElementById("num").value += 9;
  101. }
  102. function createMathButton(type, symbol) {
  103. var mButton = document.createElement("input");
  104. mButton.setAttribute("class", "math");
  105. mButton.setAttribute("type", "button");
  106. mButton.setAttribute("value", symbol);
  107. mButton.setAttribute("onclick", type);
  108. var divHere = document.getElementById("here");
  109. divHere.appendChild(mButton);
  110. divHere.appendChild(document.createTextNode(" "));
  111. }
  112.  
  113.  
  114.  
  115. var option = [];
  116. function plusFunc(){
  117. var x = document.getElementById("num").value;
  118. option.push(Number(x));
  119. document.getElementById("num").value = "";
  120. option.push("+");
  121. }
  122. function minusFunc(){
  123. var x = document.getElementById("num").value;
  124. option.push(Number(x));
  125. document.getElementById("num").value = "";
  126. option.push("-");
  127. }
  128.  
  129. function multFunc(){
  130. var x = document.getElementById("num").value;
  131. option.push(Number(x));
  132. document.getElementById("num").value = "";
  133. option.push("*");
  134. }
  135.  
  136. function divFunc(){
  137. var x = document.getElementById("num").value;
  138. option.push(Number(x));
  139. document.getElementById("num").value = "";
  140. option.push("/");
  141. }
  142.  
  143. function eqFunc(){
  144. option.push(Number(document.getElementById("num").value));
  145. var total = option[0];
  146.  
  147. if(option.length > 3 || option.length === 3 && isNaN(total) === false) {
  148. for (var i = 1; i < option.length - 1; i += 2) {
  149. if (option[i] === "+") {
  150. total += option[i + 1];
  151. } else if (option[i] === "-") {
  152. total -= option[i + 1];
  153. } else if (option[i] === "*") {
  154. total *= option[i + 1];
  155. } else if (option[i] === "/") {
  156. total /= option[i + 1];
  157. }else{
  158. alert("Invalid sequence of numbers and operators");
  159. clearFunc();
  160. break;
  161. }
  162. }
  163. document.getElementById("num").value = total;
  164. $.ajax({
  165. url: '/calc/value/' + total,
  166. type: 'POST',
  167. success: function (data) {
  168.  
  169. }
  170. });
  171. option = [];
  172. }else{
  173. alert("Invalid sequence of numbers and operators");
  174. clearFunc();
  175. }
  176. }
  177.  
  178. function clearFunc() {
  179. option = [];
  180. document.getElementById("num").value = "";
  181. $.ajax({
  182. url: '/calc/value/' + 0,
  183. type: 'POST',
  184. success: function (data) {
  185.  
  186. }
  187. });
  188. }
  189.  
  190. function postToServer() {
  191. var user = document.getElementById("user").value;
  192. console.log("in postTo");
  193. var pass = document.getElementById("pass").value;
  194.  
  195. var userInfo = {
  196. 'username' : $('input[id=user]').val(),
  197. 'userPassword' : $('input[id=pass]').val()
  198. };
  199. $.ajax({
  200. url: "/login",
  201. type: "POST",
  202. datatype: "json",
  203. data: userInfo,
  204. success: function(data){
  205. if(data.loggedIn === 'true') {
  206. document.getElementById('virtual screen').style.display = "none"
  207. document.getElementById('buttons').style.display = "block";
  208. //in case this isn't the first time the user is trying to login.
  209. document.getElementById('incorrect').style.display = "none";
  210. } else {
  211. document.getElementById('incorrect').style.display = "block";
  212. console.log("WRONG USERNAME OR PASSWORD");
  213. }
  214.  
  215. }
  216.  
  217. });
  218. }
  219.  
  220. function doProfile(){
  221. var b = document.getElementById("here");
  222. while (b.firstChild) {
  223. b.removeChild(b.firstChild);
  224. }
  225.  
  226. $.ajax({
  227. url: '/quotes/random',
  228. type: 'GET',
  229. success: function (data) {
  230. $('#here').text(data);
  231. }
  232. });
  233. document.getElementById("photo").style.display = "block";
  234. }
  235.  
  236.  
  237. function doReadme(){
  238. document.getElementById("photo").style.display = "none";
  239. var b = document.getElementById("here");
  240. while (b.firstChild) {
  241. b.removeChild(b.firstChild);
  242. }
  243. var nodeToAppend = document.createElement('P');
  244. var nodeToAppend2 = document.createElement('P');
  245. nodeToAppend.appendChild(document.createTextNode("The hardest part was the calculator"));
  246. nodeToAppend2.appendChild(document.createTextNode("The nicest part was creating this feature"));
  247. b.appendChild(nodeToAppend);
  248. b.appendChild(nodeToAppend2);
  249. }
Add Comment
Please, Sign In to add comment