I'm making a quiz where choosing certain answers will add that answers assigned value to the sum and at the end the sum will be compared against predefined guidelines and you will be given a character based on this sum value.
My problem is that when I use console.log(sum);
to test my sum value for recording inputs correctly it is recording them as undefined12323121
(where the numbers are the point values).
It should just be one number at the end but is instead storing them like it's a string.
My quiz is 10 question with 3 potential answers each where in order of answer the value is 1,2,3. I was using inputs[i].value
to sum+= the value at each answer to get the total at the end.
The output for the quiz's sum in the console is undefined and then the point values rather than the sum of those values, the picture linked has my source code.
<html>
<head>
<title>Which Star Wars Character Are You?</title>
</head>
<style>
body {
background-color: #0000ff ;
}
h1 {
color: black;
text-align: center;
}
p {
font-family: "Arial";
font-size: 25px;
text-align: center;
}
.house {
max-width: 200px;
width: 100%;
}
</style>
<h1>Which Star Wars Character Are You?</h1>
<form>
What color is your Lightsaber?
<br><input type="radio" name="lightsaber" id="red" value="1"> <label for="red">Red</label>
<br><input type="radio" name="lightsaber" id="blue" value="2"> <label for="blue">Blue</label>
<br><input type="radio" name="lightsaber" id="purple" value="3"> <label for="purple">Purple</label>
</form>
<form>
With which organization would you most likely aligned?
<br><input type="radio" name="lightsaber" id="red" value="1"> <label for="red">Empire</label>
<br><input type="radio" name="lightsaber" id="blue" value="2"> <label for="blue">Rebel Alliance</label>
<br><input type="radio" name="lightsaber" id="purple" value="3"> <label for="purple">Neither</label>
</form>
<form>
Do you submit to the Emperor's will?
<br><input type="radio" name="lightsaber" id="red" value="1"> <label for="red">Yes</label>
<br><input type="radio" name="lightsaber" id="blue" value="2"> <label for="blue">No</label>
<br><input type="radio" name="lightsaber" id="purple" value="3"> <label for="purple">Maybe</label>
</form>
<form>
Would you help a friend if it meant being sealed in carbonite?
<br><input type="radio" name="lightsaber" id="red" value="1"> <label for="red">No of course not</label>
<br><input type="radio" name="lightsaber" id="blue" value="2"> <label for="blue">Yes, I want to save my friends</label>
<br><input type="radio" name="lightsaber" id="purple" value="3"> <label for="purple">Depends on the friend</label>
</form>
<form>
Do you like Wookies?
<br><input type="radio" name="lightsaber" id="red" value="1"> <label for="red">Only as a tool to crush my enemies</label>
<br><input type="radio" name="lightsaber" id="blue" value="2"> <label for="blue">Yes I consider the creatures as friends</label>
<br><input type="radio" name="lightsaber" id="purple" value="3"> <label for="purple">I've met a few good Wookies in my time, a few hostile ones too</label>
</form>
<form>
Would you ever construct a Droid Army to usurp the Empire's power?
<br><input type="radio" name="lightsaber" id="red" value="1"> <label for="red">No, the Empire must be upheld, always</label>
<br><input type="radio" name="lightsaber" id="blue" value="2"> <label for="blue">No, using an Army of Droids would make us just as bad</label>
<br><input type="radio" name="lightsaber" id="purple" value="3"> <label for="purple">Yes, absolutely</label>
</form>
<form>
Jar Jar Binks yay or nay?
<br><input type="radio" name="lightsaber" id="red" value="1"> <label for="red">Yay</label>
<br><input type="radio" name="lightsaber" id="blue" value="2"> <label for="blue">My father liked Jar Jar</label>
<br><input type="radio" name="lightsaber" id="purple" value="3"> <label for="purple">Nay, a thousand times, Nay</label>
</form>
<form>
Have you seen the Mr. Plinkett Star Wars Reviews?
<br><input type="radio" name="lightsaber" id="red" value="1"> <label for="red">No</label>
<br><input type="radio" name="lightsaber" id="blue" value="2"> <label for="blue">I've heard of them</label>
<br><input type="radio" name="lightsaber" id="purple" value="3"> <label for="purple">Yes, he is right on all points raised</label>
</form>
<form>
Would you build a DeathStar or something like it to assert dominance over the Galaxy?
<br><input type="radio" name="lightsaber" id="red" value="1"> <label for="red">Yes, maybe even 2 times</label>
<br><input type="radio" name="lightsaber" id="blue" value="2"> <label for="blue">No, the DeathStar is a war machine that must be destroyed</label>
<br><input type="radio" name="lightsaber" id="purple" value="3"> <label for="purple">I would possible use such a machine or vessel for my goals</label>
</form>
<form>
Did you like/know of KOTOR 1 or 2
<br><input type="radio" name="lightsaber" id="red" value="1"> <label for="red">No</label>
<br><input type="radio" name="lightsaber" id="blue" value="2"> <label for="blue">Yes</label>
<br><input type="radio" name="lightsaber" id="purple" value="3"> <label for="purple">I liked KOTO2 but KOTOR 1 had a lame ending.</label>
<br><br><input type="button" id="qButton" value="Choose">
</form>
<p id="QuestionParagraph"></p>
<script>
function getButton() {
var button = document.getElementById('qButton');
button.onclick = getResult;
}
function getResult(){
var sum;
var inputs = document.getElementsByName('lightsaber');
for (i=0;i<inputs.length;i++){
if (inputs[i].checked) {
sum += inputs[i].value;
console.log(sum);
}
}
}
window.onload = getButton;
</script>
</html>
function getButton() {
var button = document.getElementById('qButton');
button.onclick = getResult;
}
function getResult() {
var sum;
var inputs = document.getElementsByName('lightsaber');
for (i = 0; i < inputs.length; i++) {
if (inputs[i].checked) {
sum += inputs[i].value;
console.log(sum);
}
}
}
window.onload = getButton;
body {
background-color: #fff;
}
h1 {
color: black;
text-align: center;
}
p {
font-family: "Arial";
font-size: 25px;
text-align: center;
}
.house {
max-width: 200px;
width: 100%;
}
<h1>Which Star Wars Character Are You?</h1>
<form>
What color is your Lightsaber?
<br>
<input type="radio" name="lightsaber" id="red" value="1">
<label for="red">Red</label>
<br>
<input type="radio" name="lightsaber" id="blue" value="2">
<label for="blue">Blue</label>
<br>
<input type="radio" name="lightsaber" id="purple" value="3">
<label for="purple">Purple</label>
</form>
<form>
With which organization would you most likely aligned?
<br>
<input type="radio" name="lightsaber" id="red" value="1">
<label for="red">Empire</label>
<br>
<input type="radio" name="lightsaber" id="blue" value="2">
<label for="blue">Rebel Alliance</label>
<br>
<input type="radio" name="lightsaber" id="purple" value="3">
<label for="purple">Neither</label>
</form>
<form>
Do you submit to the Emperor's will?
<br>
<input type="radio" name="lightsaber" id="red" value="1">
<label for="red">Yes</label>
<br>
<input type="radio" name="lightsaber" id="blue" value="2">
<label for="blue">No</label>
<br>
<input type="radio" name="lightsaber" id="purple" value="3">
<label for="purple">Maybe</label>
</form>
<form>
Would you help a friend if it meant being sealed in carbonite?
<br>
<input type="radio" name="lightsaber" id="red" value="1">
<label for="red">No of course not</label>
<br>
<input type="radio" name="lightsaber" id="blue" value="2">
<label for="blue">Yes, I want to save my friends</label>
<br>
<input type="radio" name="lightsaber" id="purple" value="3">
<label for="purple">Depends on the friend</label>
</form>
<form>
Do you like Wookies?
<br>
<input type="radio" name="lightsaber" id="red" value="1">
<label for="red">Only as a tool to crush my enemies</label>
<br>
<input type="radio" name="lightsaber" id="blue" value="2">
<label for="blue">Yes I consider the creatures as friends</label>
<br>
<input type="radio" name="lightsaber" id="purple" value="3">
<label for="purple">I've met a few good Wookies in my time, a few hostile ones too</label>
</form>
<form>
Would you ever construct a Droid Army to usurp the Empire's power?
<br>
<input type="radio" name="lightsaber" id="red" value="1">
<label for="red">No, the Empire must be upheld, always</label>
<br>
<input type="radio" name="lightsaber" id="blue" value="2">
<label for="blue">No, using an Army of Droids would make us just as bad</label>
<br>
<input type="radio" name="lightsaber" id="purple" value="3">
<label for="purple">Yes, absolutely</label>
</form>
<form>
Jar Jar Binks yay or nay?
<br>
<input type="radio" name="lightsaber" id="red" value="1">
<label for="red">Yay</label>
<br>
<input type="radio" name="lightsaber" id="blue" value="2">
<label for="blue">My father liked Jar Jar</label>
<br>
<input type="radio" name="lightsaber" id="purple" value="3">
<label for="purple">Nay, a thousand times, Nay</label>
</form>
<form>
Have you seen the Mr. Plinkett Star Wars Reviews?
<br>
<input type="radio" name="lightsaber" id="red" value="1">
<label for="red">No</label>
<br>
<input type="radio" name="lightsaber" id="blue" value="2">
<label for="blue">I've heard of them</label>
<br>
<input type="radio" name="lightsaber" id="purple" value="3">
<label for="purple">Yes, he is right on all points raised</label>
</form>
<form>
Would you build a DeathStar or something like it to assert dominance over the Galaxy?
<br>
<input type="radio" name="lightsaber" id="red" value="1">
<label for="red">Yes, maybe even 2 times</label>
<br>
<input type="radio" name="lightsaber" id="blue" value="2">
<label for="blue">No, the DeathStar is a war machine that must be destroyed</label>
<br>
<input type="radio" name="lightsaber" id="purple" value="3">
<label for="purple">I would possible use such a machine or vessel for my goals</label>
</form>
<form>
Did you like/know of KOTOR 1 or 2
<br>
<input type="radio" name="lightsaber" id="red" value="1">
<label for="red">No</label>
<br>
<input type="radio" name="lightsaber" id="blue" value="2">
<label for="blue">Yes</label>
<br>
<input type="radio" name="lightsaber" id="purple" value="3">
<label for="purple">I liked KOTO2 but KOTOR 1 had a lame ending.</label>
<br>
<br>
<input type="button" id="qButton" value="Choose">
</form>
<p id="QuestionParagraph"></p>