0

So I'm trying to make a Buzzfeed style quiz but i'm having trouble retrieving the values from each answer choice and adding them together for var 'score'. My script has a bunch of half functions that don't work. I was just trying to brain storm. Should I use an array or are inputs okay? I'm pretty new at this so forgive me if you see some laughable errors. Thanks! :)

`

<!DOCTYPE html>
<html>
<head>
    <link href="main.css" rel="stylesheet" >

<script>    

    var score = 0; 
    var a1, a2, a3, a4; 

function next(){

    a1 = document.getElementById("shirt").value;
    a2 = document.getElementById("zodiac").value;
    a3 = document.getElementById("saturday").value;
    a4 = document.getElementById("hunger").value;

    score = a1+a2+a3+a4;  

  if(score<=3) {

    return("You should eat a banana.")
} 
}    

function alertFunction(){
        alert(score);
    }

function checkRadio(){
   if(document.getElementById('name')=="shirt") {

   }
}    

  console.log(score); 

</script>

<style>

    div {
        border: 1px solid black;
        background-color: #def2ed;
        padding: 20px;    
        margin-left: auto;
    }
</style>
</head> 
<body> 

    <form class = "quiz">


<div id="shirt">

    <h2>Question #1: What color shirt are you currently wearing? </h2>

        <input onlick="checkRadio(this.value, this.name)" id="sh1" type="radio" name="shirt" value="1"> White<br>
        <input onlick="checkRadio(this.value, this.name)" id="sh2" type="radio" name="shirt" value="0"> Yellow<br>
        <input onlick="checkRadio(this.value, this.name)" id="sh3" type="radio" name="shirt" value="3"> Red<br>
        <input onlick="checkRadio(this.value, this.name)" id="sh4" type="radio" name="shirt" value="5"> Other<br>

</div> 


<div id="zodiac">

    <h2>Question #2: What is your Zodiac sign? </h2>

        <input onlick="checkRadio(this.value, this.name)" id="z1" type="radio" name="zodiac" value="1"> Scorpio<br>
        <input onlick="checkRadio(this.value, this.name)" id="z1" type="radio" name="zodiac" value="0"> Capricorn<br>
        <input onlick="checkRadio(this.value, this.name)" id="z1" type="radio" name="zodiac" value="3"> Gemini<br>
        <input onlick="checkRadio(this.value, this.name)" id="z1" type="radio" name="zodiac" value="1"> Pisces<br>

</div> 

<div id="saturday">

    <h2>Question #3: What do you see yourself doing on a Saturday? </h2>

        <input onlick="checkRadio(this.value, this.name)" id="s1" type="radio" name="saturday" value="3"> Sleeping until noon<br>
        <input onlick="checkRadio(this.value, this.name)" id="s2" type="radio" name="saturday" value="1"> Hitting the gym<br>
        <input onlick="checkRadio(this.value, this.name)" id="s3" type="radio" name="saturday" value="5"> Shopping <br>
        <input onlick="checkRadio(this.value, this.name)" id="s4" type="radio" name="saturday" value="1"> Hanging out with friends<br>

</div> 

 <div id="hunger">

    <h2>Question #4: How hungry are you? </h2>

        <input onlick="checkRadio(this.value, this.name)" id="h1" type="radio" name="hunger" value="0"> Not really hungry at the moment<br>
        <input onlick="checkRadio(this.value, this.name)" id="h2" type="radio" name="hunger" value="1"> I'm a little peckish<br>
        <input onlick="checkRadio(this.value, this.name)" id="h3" type="radio" name="hunger" value="0"> I'm pretty hungry, now that you mention it<br>
        <input onlick="checkRadio(this.value, this.name)" id="h4" type="radio" name="hunger" value="1"> I'm STARVING!<br>
</div>     


   <button type="button" class="primaryBtn" onClick="alert(score)">Next</button>

</form>  

</body>

</html>`
  • why won't you use jQuery lib? It would make it quite easier i believe :) if you would like to just link the library with `` and let us know - i'll do my best to help you – Damian Doman Apr 17 '17 at 16:53
  • Possible duplicate of [How can I know which radio button is selected via jQuery?](http://stackoverflow.com/questions/596351/how-can-i-know-which-radio-button-is-selected-via-jquery) – Madison Renee Apr 25 '17 at 17:19

2 Answers2

0

try to get the values of each radio this way:

document.querySelector('input[name="shirt"]:checked').value;

So, instead of having an ID, you need to specify a name to each group of radio inputs.

Tulio Faria
  • 874
  • 7
  • 16
0

Now this is the working code for you (for explanation see below):

<!DOCTYPE html>
<html>
<head>
    <link href="main.css" rel="stylesheet" >
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>    

    var score = 0; 
    var a1, a2, a3, a4; 

function count(){

    a1 = $("#shirt input:checked").val();
    a2 = $("#zodiac input:checked").val();
    a3 = $("#saturday input:checked").val();
    a4 = $("#hunger input:checked").val();

    var score = parseInt(a1) + parseInt(a2) + parseInt(a3) + parseInt(a4);  
    alert(score);
  if(score<=3) {

    return("You should eat a banana.")

} 
};    


</script>

<style>

    div {
        border: 1px solid black;
        background-color: #def2ed;
        padding: 20px;    
        margin-left: auto;
    }
</style>
</head> 
<body> 

    <form class = "quiz">


<div id="shirt">

    <h2>Question #1: What color shirt are you currently wearing? </h2>

        <input onlick="checkRadio(this.value, this.name)" id="sh1" type="radio" name="shirt" value="1"> White<br>
        <input onlick="checkRadio(this.value, this.name)" id="sh2" type="radio" name="shirt" value="0"> Yellow<br>
        <input onlick="checkRadio(this.value, this.name)" id="sh3" type="radio" name="shirt" value="3"> Red<br>
        <input onlick="checkRadio(this.value, this.name)" id="sh4" type="radio" name="shirt" value="5"> Other<br>

</div> 


<div id="zodiac">

    <h2>Question #2: What is your Zodiac sign? </h2>

        <input onlick="checkRadio(this.value, this.name)" id="z1" type="radio" name="zodiac" value="1"> Scorpio<br>
        <input onlick="checkRadio(this.value, this.name)" id="z1" type="radio" name="zodiac" value="0"> Capricorn<br>
        <input onlick="checkRadio(this.value, this.name)" id="z1" type="radio" name="zodiac" value="3"> Gemini<br>
        <input onlick="checkRadio(this.value, this.name)" id="z1" type="radio" name="zodiac" value="1"> Pisces<br>

</div> 

<div id="saturday">

    <h2>Question #3: What do you see yourself doing on a Saturday? </h2>

        <input onlick="checkRadio(this.value, this.name)" id="s1" type="radio" name="saturday" value="3"> Sleeping until noon<br>
        <input onlick="checkRadio(this.value, this.name)" id="s2" type="radio" name="saturday" value="1"> Hitting the gym<br>
        <input onlick="checkRadio(this.value, this.name)" id="s3" type="radio" name="saturday" value="5"> Shopping <br>
        <input onlick="checkRadio(this.value, this.name)" id="s4" type="radio" name="saturday" value="1"> Hanging out with friends<br>

</div> 

 <div id="hunger">

    <h2>Question #4: How hungry are you? </h2>

        <input onlick="checkRadio(this.value, this.name)" id="h1" type="radio" name="hunger" value="0"> Not really hungry at the moment<br>
        <input onlick="checkRadio(this.value, this.name)" id="h2" type="radio" name="hunger" value="1"> I'm a little peckish<br>
        <input onlick="checkRadio(this.value, this.name)" id="h3" type="radio" name="hunger" value="0"> I'm pretty hungry, now that you mention it<br>
        <input onlick="checkRadio(this.value, this.name)" id="h4" type="radio" name="hunger" value="1"> I'm STARVING!<br>
</div>     


   <button type="button" class="primaryBtn" onClick="count();">Next</button>

</form>  

</body>

</html>

First of all, I linked jquery to enable jQuery selectors, which are much easier for a begginer to learn in my opinion. The syntax being $('SELECTOR_NAME').method(); i just called the checked input field (input:checked) for each of the divs (#shirt, #zodiac etc.) to get their values with val() method.

Then, in order to add them i used parseInt(variable) since they were treated as strings (for example 5 + 1+ 3+ 1 would be 5131 not 10). parseInt makes them numbers, so they add really cool now.

Damian Doman
  • 522
  • 8
  • 19
  • @Madison Renee I'm glad I could help! Society here would help you out most of the time so make sure you come back to us here and then :-) also, if my answer was of much help AND I explained the matter in depth enough for you to understand the bugs in code you can pick it as best answer using this tick left to answer - this would help other users that come by looking for similar issue to be worked around to find the right answer. Have a nice day! – Damian Doman Apr 17 '17 at 20:00
  • @Madison Renee please show me what did u change in it so I could see any issues, also you can use F12 shortcut in browser And in developers windows console check of there are any errors related. – Damian Doman Apr 18 '17 at 03:03