Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function _Formula(_exponent, _checkbox) {
- this.exponent = Number(_exponent);
- this.line_break = _checkbox;
- this.getPascal = function() {
- var array = [1];
- for (var i = 0; i < this.exponent; i++) {
- array.push(array[i] * (this.exponent - i) / (i + 1));
- }
- this.array = array;
- };
- this.breakLine = function() {
- if (this.line_break)
- this.answer += "<br>";
- };
- this.getAnswer = function() {
- this.getPascal();
- this.answer = "(a + b)" + this.exponent.toString().sup() + " = ";
- this.breakLine();
- if (this.exponent === 0)
- this.answer += 1;
- if (this.exponent > 0) {
- this.answer += "a";
- if (this.exponent > 1)
- this.answer += this.exponent.toString().sup();
- this.answer += " + ";
- this.breakLine();
- for (var i = 1; i < this.exponent; i++) {
- this.answer += this.array[i] + "a";
- if (this.exponent - i > 1)
- this.answer += (this.exponent - 1).toString().sup();
- this.answer += "b";
- if (i > 1)
- this.answer += i.toString().sup();
- this.answer += " + ";
- this.breakLine();
- }
- this.answer += "b";
- if (this.exponent > 1)
- this.answer += this.exponent.toString().sup();
- }
- return this.answer;
- };
- }
- function isNumber(n) {
- return !isNaN(parseFloat(n)) && isFinite(n) && (n >= 0) && (n % 1 === 0);
- }
- window.onload = function() {
- var input = document.getElementById('exponent');
- var checkbox = document.getElementById('line_break');
- var result = document.getElementById('result');
- input.oninput = checkbox.onclick = function() {
- if (!isNumber(input.value))
- return result.innerHTML = "The exponent must be non-negative integer!";
- var formula = new _Formula(input.value, checkbox.checked);
- result.innerHTML = formula.getAnswer();
- };
- };
Advertisement
Add Comment
Please, Sign In to add comment