-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
127 lines (91 loc) · 3.28 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
'use strict'
console.log('firing up')
const sortAlpha = (string) => {
return string.split('').sort().join('');
}
const multiplyPolynomials = (polyA, polyB) => {
let result = []
polyA.forEach((polyATerm) => {
polyB.forEach((polyBTerm) => {
result.push(multiplyPolynomialTerms(polyATerm, polyBTerm))
})
})
result = result.reduce((processedResult, term) => {
const coeficient = term.match(/\d+/g) === null ? 1 : Number(term.match(/\d+/g).join(''))
const rawTerm = term.match(/[a-zA-Z]+/g).join('')
if (processedResult.hasOwnProperty(rawTerm)) {
processedResult[rawTerm].coeficient += coeficient
return processedResult
}
processedResult[rawTerm] = {
coeficient: coeficient
}
return processedResult
}, {})
return result
}
const formatPolynomial = (polynomial) => {
const poly = []
for (let term in polynomial) {
const coeficient = polynomial[term].coeficient === 1 ? '' : polynomial[term].coeficient
poly.push(coeficient + term)
}
return poly
}
const binomialPower = (binomial, power) => {
if (power > -1) {
let compositePoly = binomial
let result = []
result.push(['1'])
if (power >= 1) {
result.push(binomial)
}
if (power > 1) {
for (let i = power - 1; i > 0; i--) {
compositePoly = formatPolynomial(multiplyPolynomials(binomial, compositePoly))
result.push(compositePoly)
}
}
return result
}
return -1
}
const multiplyPolynomialTerms = (termA, termB) => {
console.log(termA, termB)
const coeficientA = termA.match(/\d+/g) === null ? 1 : Number(termA.match(/\d+/g).join(''))
const coeficientB = termB.match(/\d+/g) === null ? 1 : Number(termB.match(/\d+/g).join(''))
const rawTermA = termA.match(/[a-zA-Z]+/g).join('')
const rawTermB = termB.match(/[a-zA-Z]+/g).join('')
return (coeficientA * coeficientB) + sortAlpha(rawTermA + rawTermB)
}
const extractBinomialCoeficients = (term) => {
return term.match(/\d+/g) === null ? 1 : Number(term.match(/\d+/g).join(''))
}
const binomial = ['x', 'y']
const trinomial = ['xx', '2xy', 'yy']
const tetranomial = ['xxx', '3xxy', '3xyy', 'yyy']
const fivePowerPoly = ['xxxxx', '5xxxxy', '10xxxyy', '10xxyyy', '5xyyyy', 'yyyyy']
//console.log(formatPolynomial(multiplyPolynomials(binomial, trinomial)))
//console.log(formatPolynomial(multiplyPolynomials(binomial, tetranomial)))
//console.log(binomialPower(binomial, 6))
//console.log(multiplyPolynomials(binomial, fivePowerPoly))
//console.log(sumPolynomialTerms('2xy', '2xxy'));
const buildTriangle = () => {
const lines = document.getElementById('lines').value
if (lines > -1) {
const poweredBinomial = binomialPower(binomial, lines)
const $triangle = document.getElementById('triangle')
poweredBinomial.forEach((line) => {
const triangleLine = document.createElement('li')
triangleLine.className = 'line'
line.forEach((element) => {
const elementContent = document.createElement('span')
elementContent.className = 'element'
elementContent.appendChild(document.createTextNode(extractBinomialCoeficients(element)))
triangleLine.appendChild(elementContent)
})
$triangle.appendChild(triangleLine)
})
}
}
document.getElementById('build').addEventListener('click', buildTriangle)