Я сделал квадратичный решатель уравнений с Javascript и HTML, но когда я нажимаю кнопку «рассчитать», он просто получает значения «a» и «c» и умножается на -1.Квадратичное уравнение в javascript не работает
Я начинаю с Javascript, поэтому я мало знаю об объекте.
Вот код:
var a, b, c, xone, xtwo;
function getValues() {
function getValues() {
if (document.getElementById('signone').value == "+") {
a = document.getElementById('vara').value;
} else {
a = document.getElementById('vara').value * (-1);
}
if (document.getElementById('signtwo').value == "+") {
b = document.getElementById('varb').value;
} else {
b = document.getElementById('varb').value * (-1);
}
if (document.getElementById('signthree').value == "+") {
c = document.getElementById('varc').value;
} else {
c = document.getElementById('varc').value * (-1);
}
}
}
function getSolution() {
xone = ((-1 * b) + Math.sqrt((b * b) - 4 * a * c))/(2 * a);
xtwo = ((-1 * b) - Math.sqrt((b * b) - 4 * a * c))/(2 * a);
}
function showSolution() {
document.getElementById('showone').innerHTML = "x1 = " + xone;
document.getElementById('showtwo').innerHTML = "x2 = " + xtwo;
}
<h1> Quadratic equation calculator </h1>
<p>This calculator is going to find the two values of <i>x</i> of the equation typed.</br>In order to use it properly, you have to fill all of the boxes</br>
and click <q>ok</q>
</p>
</br>
<form>
<select id="signone">
<option value="+">+</option>
<option value="-">-</option>
</select>
<input id="vara" type="text" name="firstvar" placeholder="type the coeficient a" />x2
<select id="signtwo">
<option value="+">+</option>
<option value="-">-</option>
</select>
<input id="varb" type="text" name="secondvar" placeholder="type the coeficient b " />x
<select id="signthree">
<option value="+">+</option>
<option value="-">-</option>
</select>
<input id="varc" type="text" name="thirdvar" placeholder="type the coeficient c" />=0
</form>
</br>
<button type="button" onclick="getValues();getSolution();showSolution();">Calculate</button>
<p id="showone">X1 =</p>
</br>
<p id="showtwo">X2 =</p>
'.value' возвращает строку, а не число. –
Отлаживайте этого прикомандиста. Проверяйте каждую часть за один раз за один раз - выводьте каждый результат шага в console.log/alert it - и убедитесь, что он распечатывает то, что вы хотите. –
@IfTrue нет такой функции. используйте 'parseFloat'. –