Calc.js

Size
3,338 Kb
Views
8,096

How do I make an calc.js?

What is a calc.js? How do you make a calc.js? This script and codes were developed by Томаш Хамлай on 11 November 2022, Friday.

Calc.js Previews

Calc.js - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>calc.js</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
</head>
<body> <script src="js/index.js"></script>
</body>
</html>

Calc.js - Script Codes JS Codes

var Calculator = {};
! function(e) {	"use strict";	function t(e) {	this._sender = e, this._listeners = []	}	function i() {	var e = this,	i = {};	this._definition = 0, this._expression = "", this.expressionChanged = new t(this), this.expressionEvaluated = new t(this), this.definitionChanged = new t(this),	function(e, t, n) {	return n.expr = e, n.def = t, i = n	}(this._expression, this._definition, i), this.resetExpression = function() {	e._expression = i.expr	}, this.setExpression = function(t) {	var i = "",	n = "";	t.operation && "=" !== t.operation && (i = " " + t.operation + " "), t.value && (n = i + t.value), e._expression += n, e.expressionChanged.notify({})	}, this.setDefinition = function(t) {	e._definition = t.substring(0, 9), e.definitionChanged.notify({	value: t	}), e.resetExpression()	}, this.evaluateExpression = function() {	function t(e) {	return "+" === e || "-" === e ? 1 : "×" === e || "÷" === e ? 2 : -1	}	function i(e, t) {	var i = parseFloat(e.pop()),	n = parseFloat(e.pop());	e.push(a[t](n, i).toString())	}	function n(e) {	for (var n = [], s = [], u = e.split(" "), r = u.length, o = 0; o < r; o++) {	var l = u[o];	if (l in a) {	for (var c = l; !n.empty() && t(n.last()) >= t(c);) i(s, n.last()), n.pop();	n.push(c)	} else s.push(l)	}	for (; !n.empty();) i(s, n.last()), n.pop();	return s.last()	}	Array.prototype.last || (Array.prototype.last = function() {	return this[this.length - 1]	}), Array.prototype.empty || (Array.prototype.empty = function() {	return 0 === this.length	});	var a = {	"+": function(e, t) {	return e + t	},	"-": function(e, t) {	return e - t	},	"×": function(e, t) {	return e * t	},	"÷": function(e, t) {	return e / t	}	},	s = e._expression,	u = n(s);	e.setDefinition(u)	}	}	function n(e, i) {	function n(e) {	"." === e ? 0 === r._input.length ? r._input.value = "0." : r._input.value.indexOf(".") === -1 && (r._input.value += ".") : "0" === e ? 0 === r._input.value.length || 0 === parseFloat(r._input.value + 0, 10) && r._input.value.indexOf(".") === -1 ? r._input.value = 0 : r._input.value += e : r._input.value = parseFloat(r._input.value + e), r._input.value.length > 10 && (r._input.value = r._input.value.slice(r._input.value.length - 11, r._input.value.length - 1))	}	function a(e) {	function t(e) {	switch (e) {	case 8:	return console.log("backspase pressed"), "clear-entry";	case 13:	return "evaluate";	case 42:	return "multiply";	case 43:	return "add";	case 45:	return "substract";	case 47:	return "devide";	default:	return "put"	}	}	e.preventDefault(), e.returValue = !1;	var i = e.keyCode,	n = "",	a = "",	u = "";	null !== e.charCode ? n = e.charCode : null !== e.which ? n = e.which : null !== e.keyCode && (n = e.keyCode), a = t(i), u = String.fromCharCode(n), s(e, a, u)	}	function s(e, t, i) {	t in l && l[t](t, e), o.indexOf(i) > -1 && n(i)	}	function u(e) {	var t = e.target,	i = t.getAttribute("data-action") || "put",	n = t.value;	s(e, i, n)	}	var r = this,	o = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "."],	l = {	"all-clear": function(e) {	r.funcKeyPressed.notify({	action: e	})	},	"clear-entry": function(e) {	r.funcKeyPressed.notify({	action: e	})	},	copy: function(e, t) {	r.funcKeyPressed.notify({	action: e,	evt: t	})	},	"change-sign": function(e) {	r.funcKeyPressed.notify({	action: e	})	},	evaluate: function(e) {	r.funcKeyPressed.notify({	action: e	})	},	add: function(e) {	r.funcKeyPressed.notify({	action: e	})	},	substract: function(e) {	r.funcKeyPressed.notify({	action: e	})	},	multiply: function(e) {	r.funcKeyPressed.notify({	action: e	})	},	devide: function(e) {	r.funcKeyPressed.notify({	action: e	})	}	};	this._model = e, this._ui = document.getElementById(i), this._display = this._ui.getElementsByClassName("screen")[0], this._input = this._display.firstElementChild, this._model.expressionEvaluated.attach(function(e, t) {	r._input.value = t.definition	}), this._model.definitionChanged.attach(function(e, t) {	r._input.value = t.value	}), this._model.expressionChanged.attach(function() {	r._input.value = "0"	}), this._ui.addEventListener("click", u), this._ui.addEventListener("keypress", a), this.funcKeyPressed = new t(this)	}	function a(e, t) {	function i(e) {	var t = e.target,	i = t.getAttribute("data-target"),	a = i ? document.querySelector(i) : null;	if (a && a.select) {	a.select();	var s = !1;	try {	s = document.execCommand("copy")	} catch (e) {	s = !1	}	if (s) {	var u = a.value;	a.blur(), n._view._input.value = "Copied!", setTimeout(function() {	n._view._input.value = u	}, 1500)	} else alert("please press Ctrl/Cmd+C to copy")	}	}	this._model = e, this._view = t;	var n = this;	this._view.funcKeyPressed.attach(function(e, t) {	var a, s;	switch (t.action) {	case "all-clear":	n._model.setExpression({	operation: "",	value: ""	});	break;	case "change-sign":	n._view._input.value *= -1;	break;	case "clear-entry":	n._view._input.value = n._view._input.value.slice(0, n._view._input.value.length - 1);	break;	case "copy":	i(t.evt);	break;	case "add":	s = n._view._display.getAttribute("data-before") || "", a = n._view._input.value, n._model.setExpression({	operation: s,	value: a	}), n._view._display.setAttribute("data-before", "+");	break;	case "substract":	s = n._view._display.getAttribute("data-before") || "", a = n._view._input.value, n._model.setExpression({	operation: s,	value: a	}), n._view._display.setAttribute("data-before", "-");	break;	case "multiply":	s = n._view._display.getAttribute("data-before") || "", a = n._view._input.value, n._model.setExpression({	operation: s,	value: a	}), n._view._display.setAttribute("data-before", "×");	break;	case "devide":	s = n._view._display.getAttribute("data-before") || "", a = n._view._input.value, n._model.setExpression({	operation: s,	value: a	}), n._view._display.setAttribute("data-before", "÷");	break;	case "evaluate":	s = n._view._display.getAttribute("data-before") || "", a = n._view._input.value, n._model.setExpression({	operation: s,	value: a	}), n._model.evaluateExpression(), n._view._display.setAttribute("data-before", "=")	}	})	}	t.prototype = {	attach: function(e) {	this._listeners.push(e)	},	notify: function(e) {	var t;	for (t = 0; t < this._listeners.length; t += 1) this._listeners[t](this._sender, e)	}	}, e.initialize = function() {	var e = new i,	t = new n(e, "calculator");	new a(e, t)	}
}(Calculator);
Calc.js - Script Codes
Calc.js - Script Codes
Home Page Home
Developer Томаш Хамлай
Username TomashKhamlai
Uploaded November 11, 2022
Rating 3
Size 3,338 Kb
Views 8,096
Do you need developer help for Calc.js?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Томаш Хамлай (TomashKhamlai) Script Codes
Create amazing web content with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!