Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80698 views
1
var QuadraticCalculator = React.createClass({
2
getInitialState: function() {
3
return {
4
a: 1,
5
b: 3,
6
c: -4
7
};
8
},
9
10
/**
11
* This function will be re-bound in render multiple times. Each .bind() will
12
* create a new function that calls this with the appropriate key as well as
13
* the event. The key is the key in the state object that the value should be
14
* mapped from.
15
*/
16
handleInputChange: function(key, event) {
17
var partialState = {};
18
partialState[key] = parseFloat(event.target.value);
19
this.setState(partialState);
20
},
21
22
render: function() {
23
var a = this.state.a;
24
var b = this.state.b;
25
var c = this.state.c
26
var x1 = (-b + Math.sqrt(Math.pow(b, 2) - 4 * a * c)) / (2 * a);
27
var x2 = (-b - Math.sqrt(Math.pow(b, 2) - 4 * a * c)) / (2 * a);
28
return (
29
<div>
30
<strong>
31
<em>ax</em><sup>2</sup> + <em>bx</em> + <em>c</em> = 0
32
</strong>
33
<h4>Solve for <em>x</em>:</h4>
34
<p>
35
<label>
36
a: <input type="number" value={a} onChange={this.handleInputChange.bind(null, 'a')} />
37
</label>
38
<br />
39
<label>
40
b: <input type="number" value={b} onChange={this.handleInputChange.bind(null, 'b')} />
41
</label>
42
<br />
43
<label>
44
c: <input type="number" value={c} onChange={this.handleInputChange.bind(null, 'c')} />
45
</label>
46
<br />
47
x: <strong>{x1}, {x2}</strong>
48
</p>
49
</div>
50
);
51
}
52
});
53
54
React.render(
55
<QuadraticCalculator />,
56
document.getElementById('container')
57
);
58
59