need advice with passing a javascript value into html format so i can show a grade screen of correct answers instead of 'document.write'

Viewed 72

below is the code for the check function that will store the value of the 'c' variable and the document.write will display it in a empty page. i have been trying to find a way to pass this into my html so i can create a grading screen and display the same line i have here into a html format but i cannot get the function or variable to pass through however i try.

function check() {
  let c=0
  const q1=document.quiz.question1.value
  const q2=document.quiz.question2.value
  const q3=document.quiz.question3.value
  const q4=document.quiz.question4.value
  const q5=document.quiz.question5.value
  if(q1==='1') {
    c++
  }
  if(q2==='1') {
    c++
  }
  if(q3==='1') {
    c++
  }
  if(q4==='1') {
    c++
  }
  if(q5==='1') {
    c++
  }
  document.write('<h1>You scored ' + c + ' 
and your average was ' + c/10*100 + '%</h1>')
}

below is the html code i tried but i don't understand how to export the function parameters

<section>
    <h2> Welcome
    <script type="text/javascript">
       document.write('You scored ' + c + 
'and your average was ' + c/10*100 + '%' ")
    </script>
    </h2>
  </section>

i know that this may not be part of the question or may be an additional step, but would it be easier to store that c value into a database and then pull from the database into the html code? i need to be able to store that score along with a name into a database anyways so maybe it would be easier to populate the database with the values and then take them out of there with a select query or handlebar tag (as i have a index.handlebar that i will be displaying the scores on with other data). sorry for the long question but i've been struggling with the database side so need to quickly solve these issues. Any help is greatly appreciated :)

Sorry for the long amounts of code but i wasn't sure what you needed to undertand what im trying to do. The code below saves the value within the radio button and the variable c auto-increments +1 and then the function at the bottom called "check3()" runs the increment for every value and then it runs the calculation. what i want to do is display that same statement calculation in the same page if needed but it will need to have the same css as the page of the current page.

  <main class='questionsContainer'>
    <form name='quiz' id='quiz'>
      <!-- first set -->
      <section id='questions6' style="min-height: 190px">
        <h3>{{this.Question1}}</h3>               
        <!-- first choice -->
        <h3><input type="radio" id="option1" name="question1" value="0">Emacs</h3>

        <!-- second choice -->
        <h3><input type="radio" id="option2" name="question1" value="0">Notepad++</h3>

        <!-- third choice -->
        <h3><input type="radio" id="option3" name="question1" value="1">Vim</h3>

        <!-- fourth choice -->
      <h3><input type="radio" id="option4" name="question1" value="0">Bash</h3><br>
        <button style="position: absolute; bottom: 15px;" type='button' onclick="nextQuestion(6, 7)">next</button>
    </section>

    <!-- second set -->
    <section id='questions7' style="min-height: 190px">
        <h2>{{this.Question2}}</h2>
        <!-- first choice -->
        <h3><input type="radio" id="option1" name="question2" value="1">Nothing</h3>

        <!-- second choice -->
        <h3><input type="radio" id="option2" name="question2" value="0">
          Java Development Kit 1.8 or newer</h3>

        <!-- third choice -->
        <h3><input type="radio" id="option3" name="question2" value="0">Apache Maven</h3>

        <!-- fourth choice -->
        <h3><input type="radio" id="option4" name="question2" value="0">
          Jakarta Enterprise Edition web profile compliant server</h3><br>

        <button style="position: absolute; bottom: 15px; left: 35%;" type='button' onclick="previousQuestion(7, 6)">previous</button>
        <button style="position: absolute; bottom: 15px;" type='button' onclick="nextQuestion(7, 8)">next</button>
    </section>



    <!-- Third set -->
    <section id='questions8' style="min-height: 190px">
        <h2>{{this.Question3}}</h2>
        <!-- first choice -->
        <h3><input type="radio" id="option1" name="question3" value="0">username and IP address</h3>

        <!-- second choice -->
        <h3><input type="radio" id="option2" name="question3" value="0">username and password</h3>

        <!-- third choice -->
        <h3><input type="radio" id="option3" name="question3" value="0">email address and password</h3>

        <!-- fourth choice -->
        <h3><input type="radio" id="option4" name="question3" value="1">username and email address</h3><br>
        <button style="position: absolute; bottom: 15px; left: 35%;" type='button' onclick="previousQuestion(8, 7)">previous</button>
        <button style="position: absolute; bottom: 15px;" type='button' onclick="nextQuestion(8, 9)">next</button>
    </section>

    <!-- fourth set -->
    <section id='questions9' style="min-height: 190px"style="min-height: 190px">
        <h2>{{this.Question4}}</h2>
        <!-- first choice -->
        <h3><input type="radio" id="option1" name="question4" value="0">System</h3>

        <!-- second choice -->
        <h3><input type="radio" id="option2" name="question4" value="0">Local</h3>

        <!-- third choice -->
        <h3><input type="radio" id="option3" name="question4" value="1">User</h3>

        <!-- fourth choice -->
        <h3><input type="radio" id="option4" name="question4" value="0">Global</h3><br>
        <button style="position: absolute; bottom: 15px; left: 35%;" type='button' onclick="previousQuestion(9, 8)">previous</button>
        <button style="position: absolute; bottom: 15px;" type='button' onclick="nextQuestion(9, 10)">next</button>
    </section>

    <!-- fifth set -->
    <section id='questions10' style="min-height: 190px">
        <h2>{{this.Question5}}</h2>
        <!-- first choice -->
        <h3><input type="radio" id="option1" name="question5" value="0">Junio C. Hamano</h3>

        <!-- second choice -->
        <h3><input type="radio" id="option2" name="question5" value="0">James Gosling</h3>

        <!-- third choice -->
        <h3><input type="radio" id="option3" name="question5" value="1">Linus Torvalds</h3>

        <!-- fourth choice -->
        <h3><input type="radio" id="option4" name="question5" value="0">Kohsuke Kawaguchi</h3> <br>
      
        <button style="position: absolute; bottom: 15px; left: 25%;"
        type='button' onclick="previousQuestion(10, 9)">previous</button>
      
        <button style="position: absolute; bottom: 15px; left: 35%;" type='button' 
        onclick="nextQuestion(10, 6)">Back to Question 1</button>

      <input style="position: absolute; bottom: 15px; right:20%;" type="button" 
             value="Check Your Grade!" onclick="check3(check1, check2)">
    </section>
    {{/each}}
    </form>
    </main>
1 Answers

It sounds like you want to take the document.write and display that on the screen.

function myFunction() {
//Create the HTML ELEMENT 
const msgDiv = document.createElement('h1');

// create the message you want to display
 msgDiv.innerHTML = `<span>$ You scored {c} and you average was {c/10*100}%</span>$`;

// Display that message by appending it to an HTML element

 yourHTMLELEMENTWHEREYOUWANTITTODISPLAY.appendChild(msgDiv);

}

//call the function 
myFunction()

//add an eventlistener on function
button.addEventListener('click', myFunction)


You have your quiz wrapped in a form, so you can just add an add event listener on that element. That way when the quiz is at the end and when someone hits submit the other function to display the results can run. Also you want to have the form submit so that they can either click or hit enter so submit. 




 form.addEventListener('submit' (e) => {
   // this stops the page from submitting and going to another page
   e.preventDefault();
   // Add the code from up above. 
   // You don't want your check function to be in this code because it will only run when the form is submitted and you don't want that.
})
Related