Skip to content

Calculator

YuraVolk edited this page Dec 10, 2019 · 3 revisions

Calculator

Calculator is a very useful programm. Even though it is very rarely used in sites, it still may be very useful.

If you want to implement this script by your own, follow these steps:

  1. Create array of operations.
  2. Add event listener for buttons.
  3. Create function of adding symbol, it should add new element to operations list.
  4. Create function of adding fraction, it should make number fractional.
  5. Add function for making number negative positive. It should add or remove '-' before number.
  6. Add function for adding prefix. It should wrap existing operation with passes parameters. For example:

param1 = Math.pow

param2 = ',2'

operations = ["2", '+', "2"]

Result: ["Math.pow(", "2", '+', "2", ",2)"]

  1. Add evaluate function. Make it check for XSS.
  2. Use eval with join;
  3. Add formatting for operations: format string as you want. Print its content to HTML.

Preview

Clone this wiki locally