Я пытаюсь создать калькулятор бюджета, который принимает пользовательский ввод для различных переменных и возвращает новое значение после расчета.
Я пытался использовать тег ввода для получения значений для различных переменных и вычисленияРезультат с использованием функции JavaScript, которая отображает, возвращает результат в качестве значения другого входного тега.Я новичок в программировании, поэтому я не уверен, что это правильный путь.
var inc, rent, vloan, hins, vins, cc, loan, food, gas, entertainment, spending, result;
function calculate() {
inc = document.getElementById("income").value;
rent = document.getElementById("rent").value;
vloan = document.getElementById("vloan").value;
hins = document.getElementById("hinsurance").value;
vins = document.getElementById("vinsurance").value;
cc = document.getElementById("creditcard").value;
loan = document.getElementById("loan").value;
food = document.getElementById("food").value;
gas = document.getElementById("gas").value;
entertainment = getElementById("entertainment").value;
spending = getElementById("spending").value;
return document.getElementById("result").value = inc - rent - vloan - hins - vins - cc - loan - food - gas - entertainment - spending;
}
<div id="form2">
Enter your income:<br>
<input type="text" id="income"><br> Mortgage/Rent:<br>
<input type="text" id="rent"><br> Vehicle Loan:<br>
<input type="text" id="vloan"><br> Home Insurance:<br>
<input type="text" id="hinsurance"><br> Vehicle Insurance:<br>
<input type="text" id="vinsurance"><br> Credit Card:<br>
<input type="text" id="creditcard"><br> Other Loans:<br>
<input type="text" id="loan"><br> Groceries:<br>
<input type="text" id="food"><br> Gas/Public Transport:<br>
<input type="text" id="gas"><br> Cable/Internet:<br>
<input type="text" id="entertainment"><br> Spending Money:<br>
<input type="text" id="spending"><br>
<button id="cb" onclick="calculate()">Calculate</button>
<div id="answer">
<p>It is suggested that 20% of your income be allocated towards a savings account</p>
<input id="result" type="text" value="">
</div>
</div>
Я ожидал, что результат после арифметической операции будет отображаться в последнем поле ввода с тегом id "result"