Work through the W3Schools AJAX tutorial and then the W3Schools JQuery tutorial.
A more advance AJAX tutorial is available that walks you through applying AJAX with JQuery. Work through these exercises.
Create a new web page, called
calculator.html, from your
You will need to use the regular expression methods to parse, or break down, the string into its components, and type conversion to extract the numbers. When doing the parsing, remember to respect the precedence of the arithmetic operators. For example, if the user enters:
15 + 4 x 3it should give the answer 27, not 57.
You should not assume any particular convention for whitespace. So for example
15+4 x 3should give the same result.
The algorithm proceeds as follows:
Pop the top item off the input stack. If it is a number push it onto the preorder stack. Otherwise find the first occurrence of the lowest precedence operator in the string, slice the string and push its two arguments onto the input stack (second argument first), and push the operator onto the preorder stack. Continue until the input stack is empty.
input stack preorder stack 1x2+3x4+5 empty 3x4+5, 1x2 + 3x4+5, 2, 1 +, x 3x4+5, 2 +, x, 1 3x4+5 +, x, 1, 2 5, 3x4 +, x, 1, 2, + ... empty +, x, 1, 2, +, x, 3, 4, 5
Pop the top item off the preorder stack. If it is a number, push it onto the answer stack. If it is a binary operator, pop two items off the answer stack, perform the operation, and push the answer onto the answer stack. Continue until the preorder stack is empty.
preorder stack answer stack +, x, 1, 2, +, x, 3, 4, 5 empty +, x, 1, 2, +, x, 3, 4 5 +, x, 1, 2, +, x, 3 5, 4 +, x, 1, 2, +, x 5, 4, 3 +, x, 1, 2, + 5, 12 +, x, 1, 2 17 ... empty 19