I am developing an HTML questionnaire with the help of ChatGPT and Youtube guides. If I only do simple addition, the output works just fine. But, once I try multi-step division and multiplication, the output breaks. Here is the code:
document.getElementById("survey-form").addEventListener("submit", function(event) {
event.preventDefault();
// Get the values of the input fields
let f1V = parseFloat(document.getElementById("f1").value);
let f2V = parseFloat(document.getElementById("f2").value);
let a1V = parseFloat(document.getElementById("a1").value);
let a2V = parseFloat(document.getElementById("a2").value);
let totalBobot = f1V + f2V;
let Bobot1 = f1V / totalBobot;
let Bobot2 = f2V / totalBobot;
let n1 = a1V * Bobot1;
let n2 = a2V * Bobot2;
let totalValue = n1 + n2;
// How do you add one more IF condition? I need 4
if (totalValue < 10) {
result = "low";
} else if (totalValue < 20) {
result = "medium";
} else if (totalValue < 30) {
result = "high";
} else {
result = "very high";
}
// Also how you print this in a new page? I see chatGPT is using this command
var url = "result.html?name=" + encodeURIComponent(name);
window.location.href = url;
});
<body>
<form action="" id="survey-form">
<div class="form-group">
<p id="quest">1. Criteria Question 1</p>
<label><input type="radio" name="f1" value="1">CR 1</label>
<label><input type="radio" name="f1" value="2">CR 2</label>
<label><input type="radio" name="f1" value="3">CR 3</label>
<label><input type="radio" name="f1" value="4">CR 4</label>
</div>
<div class="form-group">
<p id="quest">2. Criteria Question 2?</p>
<label><input type="radio" name="f2" value="1">CR 1</label>
<label><input type="radio" name="f2" value="2">CR 2</label>
<label><input type="radio" name="f2" value="3">CR 3</label>
<label><input type="radio" name="f2" value="4">CR 4</label>
</div>
<div class="form-group">
<p id="quest">2. Question 2?</p>
<label><input type="radio" name="a1" value="1">Value 1</label>
<label><input type="radio" name="a1" value="2">Value 2</label>
<label><input type="radio" name="a1" value="3">Value 3</label>
<label><input type="radio" name="a1" value="4">Value 4</label>
</div>
<div class="form-group">
<p id="quest">2. Question 2?</p>
<label><input type="radio" name="a2" value="1">Value 1</label>
<label><input type="radio" name="a2" value="2">Value 2</label>
<label><input type="radio" name="a2" value="3">Value 3</label>
<label><input type="radio" name="a2" value="4">Value 4</label>
</div>
<div class="form-group">
<button type="submit" id="submit" class="btn" onclick="">Submit</button>
</div>
</form>
<script src="tes1.js"></script>
</body>
Also if I want to use decimal, how you do it? Do I use parseFloat
? What about the other variable?