22< html lang ="de ">
33< head >
44 < meta charset ="UTF-8 ">
5+ < meta name ="viewport " content ="width=device-width, initial-scale=1, shrink-to-fit=no ">
56 < title > Math.js Gleichungsrechner</ title >
67 < script src ="https://cdnjs.cloudflare.com/ajax/libs/mathjs/11.11.0/math.min.js "> </ script >
78 <!-- Carefully take https://mathjs.org/examples/algebra.js.html as a documentation.-->
1819 # output {
1920 margin-top : 20px ;
2021 padding : 10px ;
21- background : # f4f4f4 ;
2222 border-radius : 6px ;
2323 white-space : pre-wrap;
2424 }
@@ -38,9 +38,11 @@ <h2>Math.js Gleichungsrechner</h2>
3838 < p > Beispiel: < code > 2x + 7 = 3x + 8</ code > </ p >
3939
4040 < input type ="text " id ="equation " placeholder ="Gib eine Gleichung ein, z.B. 2x + 7 = 3x + 8 " size ="40 ">
41- < button onclick ="calculate() "> Berechnen</ button >
41+ < button onclick ="calculate() "> Berechnen</ button > < br >
42+ < button onclick ="wertetabelle() "> Wertetabelle</ button >
4243
4344 < div id ="output "> </ div >
45+ < div id ="wertetabelle "> </ div >
4446
4547 < script >
4648 function calculate ( ) {
@@ -79,6 +81,36 @@ <h2>Math.js Gleichungsrechner</h2>
7981 output . innerText = "Fehler: " + err . message ;
8082 }
8183 }
84+
85+ function round ( value ) {
86+ return math . round ( value * 1000 ) / 1000 ;
87+ }
88+ function wertetabelle ( ) {
89+ const input = document . getElementById ( "equation" ) . value ;
90+ const [ lhs , rhs ] = input . split ( '=' ) ;
91+ const theMathFunction = math . simplify ( lhs . toString ( ) ) . toString ( ) ;
92+ let stellen = "<table><tr><th>x = </th>" ;
93+ let werte = "<tr><td>f(x) = </td>" ;
94+ let stellenPart2 = "<table><tr><th>x = </th>" ;
95+ let wertePart2 = "<tr><td>f(x) = </td>" ;
96+ for ( i = - 12 ; i < 1 ; i ++ ) {
97+ let y0 = "<td>" + round ( math . evaluate ( theMathFunction , { x : i / 2.0 } ) ) . toString ( ) + "</td>" ;
98+ let x0 = "<th>" + ( i / 2.0 ) . toString ( ) + "</th>" ;
99+ stellen += x0 ;
100+ werte += y0 ;
101+ }
102+ for ( i = 1 ; i < 13 ; i ++ ) {
103+ let y0 = "<td>" + round ( math . evaluate ( theMathFunction , { x : i / 2.0 } ) ) . toString ( ) + "</td>" ;
104+ let x0 = "<th>" + ( i / 2.0 ) . toString ( ) + "</th>" ;
105+ stellenPart2 += x0 ;
106+ wertePart2 += y0 ;
107+ }
108+ stellen += "</tr>\n" ;
109+ werte += "</tr></table><br>\n" ;
110+ stellenPart2 += "</tr>\n" ;
111+ wertePart2 += "</tr></table>\n" ;
112+ document . getElementById ( "wertetabelle" ) . innerHTML = stellen + werte + stellenPart2 + wertePart2 ;
113+ }
82114 </ script >
83115</ body >
84116</ html >
0 commit comments