{"id":14747,"date":"2022-10-08T23:30:15","date_gmt":"2022-10-08T18:00:15","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-submit-button-that-shows-the-score\/"},"modified":"2022-10-08T23:30:15","modified_gmt":"2022-10-08T18:00:15","slug":"solved-submit-button-that-shows-the-score","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-submit-button-that-shows-the-score\/","title":{"rendered":"[Solved] Submit button that shows the score"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-56104219\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"56104219\" data-parentid=\"56103602\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>As mentioned above, your code has some errors but I have written snippets that will achieve your aim with shorter syntax.<\/p>\n<\/p>\n<div class=\"snippet\" data-lang=\"js\" data-hide=\"false\" data-console=\"true\" data-babel=\"false\">\n<div class=\"snippet-code\">\n<pre class=\"snippet-code-js lang-js prettyprint-override\"><code>\/\/Javascript code\r\nlet questionss = [{\r\n    question: \"I am a ?\",\r\n    options: [\"Male\", \"Female\", \"Other\"],\r\n    correctAnswers: 'Male',\r\n  },\r\n  {\r\n    question: \"Football has letters ?\",\r\n    options: [8, 5, 6],\r\n    correctAnswers: 8,\r\n  },\r\n  {\r\n    question: \"VW stands for ?\",\r\n    options: [\"BMW\", \"Volkswagen\", \"Audi\"],\r\n    correctAnswers: 'Volkswagen',\r\n  },\r\n  {\r\n    question: \"What year is it ?\",\r\n    options: [2017, 2015, 2019],\r\n    correctAnswers: 2019,\r\n  }\r\n];\r\n\r\nlet questionText = document.getElementById('cd-questions');\r\nlet optiontext = document.querySelectorAll('.optiontext');\r\nlet options = document.querySelectorAll('.options');\r\nlet nextBtn = document.getElementById('next-btn');\r\nlet currentQuestion = 0;\r\nvar score = 0;\r\nvar checkedStatus = false;\r\n\r\n\r\nsetQuestion(currentQuestion); \/\/ set default question\r\n\r\nnextBtn.addEventListener('click', e =&gt; {\r\n  e.preventDefault();\r\n  if (valForm()) setQuestion(currentQuestion); \/\/validates and next question\r\n});\r\n\r\nfunction setQuestion(currentQuestion) {\r\n  questionText.innerText = questionss[currentQuestion].question; \/\/set current question to the DOM\r\n\r\n  for (let i = 0; i &lt; 3; i++) {\r\n    options[i].value = questionss[currentQuestion].options[i]; \/\/set options  value for current question\r\n\r\n    optiontext[i].innerText = questionss[currentQuestion].options[i]; \/\/set options for current question\r\n\r\n  }\r\n}\r\n\r\nfunction valForm() {\r\n  for (let i = 0; i &lt; 3; i++) {\r\n    if (options[i].checked) {\r\n      let userans = options[i].value;\r\n      if (questionss[currentQuestion].correctAnswers == userans) {\r\n        score++;\r\n      }\r\n\r\n      options[i].checked = false;\r\n      if (currentQuestion &lt; questionss.length - 1) {\r\n\r\n        currentQuestion++;\r\n        if (currentQuestion == questionss.length - 1) {\r\n          nextBtn.innerText=\"Submit\";\r\n        }\r\n      } else {\r\n        alert('Your total score is ' + score);\r\n        currentQuestion = 0;\r\n        nextBtn.innerText=\"Start\";\r\n      }\r\n      return true;\r\n    }\r\n  }\r\n  if (checkedStatus == false) {\r\n    alert('please choose an answer');\r\n    setQuestion(currentQuestion);\r\n  }\r\n\r\n  return false;\r\n}<\/code><\/pre>\n<pre class=\"snippet-code-html lang-html prettyprint-override\"><code>&lt;form&gt;\r\n  &lt;div id=\"cd-questions\"&gt;&lt;\/div&gt;\r\n  &lt;input class=\"options\" name=\"answer\" type=\"radio\" \/&gt;\r\n  &lt;span class=\"optiontext\"&gt;&lt;\/span&gt;\r\n  &lt;input class=\"options\" name=\"answer\" type=\"radio\" \/&gt;\r\n  &lt;span class=\"optiontext\"&gt;&lt;\/span&gt;\r\n  &lt;input class=\"options\" name=\"answer\" type=\"radio\" \/&gt;\r\n  &lt;span class=\"optiontext\"&gt;&lt;\/span&gt;\r\n  &lt;div&gt;\r\n    &lt;button id=\"next-btn\"&gt;Next&lt;\/button&gt;\r\n  &lt;\/div&gt;\r\n&lt;\/form&gt;<\/code><\/pre>\n<\/div>\n<\/div><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">1<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Submit button that shows the score <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] As mentioned above, your code has some errors but I have written snippets that will achieve your aim with shorter syntax. \/\/Javascript code let questionss = [{ question: &#8220;I am a ?&#8221;, options: [&#8220;Male&#8221;, &#8220;Female&#8221;, &#8220;Other&#8221;], correctAnswers: &#8216;Male&#8217;, }, { question: &#8220;Football has letters ?&#8221;, options: [8, 5, 6], correctAnswers: 8, }, { question: &#8230; <a title=\"[Solved] Submit button that shows the score\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-submit-button-that-shows-the-score\/\" aria-label=\"More on [Solved] Submit button that shows the score\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[333],"class_list":["post-14747","post","type-post","status-publish","format-standard","hentry","category-solved","tag-javascript"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Submit button that shows the score - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-submit-button-that-shows-the-score\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Submit button that shows the score - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] As mentioned above, your code has some errors but I have written snippets that will achieve your aim with shorter syntax. \/\/Javascript code let questionss = [{ question: &quot;I am a ?&quot;, options: [&quot;Male&quot;, &quot;Female&quot;, &quot;Other&quot;], correctAnswers: &#039;Male&#039;, }, { question: &quot;Football has letters ?&quot;, options: [8, 5, 6], correctAnswers: 8, }, { question: ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-submit-button-that-shows-the-score\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-08T18:00:15+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-submit-button-that-shows-the-score\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-submit-button-that-shows-the-score\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Submit button that shows the score\",\"datePublished\":\"2022-10-08T18:00:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-submit-button-that-shows-the-score\/\"},\"wordCount\":37,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"javascript\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-submit-button-that-shows-the-score\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-submit-button-that-shows-the-score\/\",\"name\":\"[Solved] Submit button that shows the score - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-10-08T18:00:15+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-submit-button-that-shows-the-score\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-submit-button-that-shows-the-score\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-submit-button-that-shows-the-score\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Submit button that shows the score\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Submit button that shows the score - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-submit-button-that-shows-the-score\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Submit button that shows the score - JassWeb","og_description":"[ad_1] As mentioned above, your code has some errors but I have written snippets that will achieve your aim with shorter syntax. \/\/Javascript code let questionss = [{ question: \"I am a ?\", options: [\"Male\", \"Female\", \"Other\"], correctAnswers: 'Male', }, { question: \"Football has letters ?\", options: [8, 5, 6], correctAnswers: 8, }, { question: ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-submit-button-that-shows-the-score\/","og_site_name":"JassWeb","article_published_time":"2022-10-08T18:00:15+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-submit-button-that-shows-the-score\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-submit-button-that-shows-the-score\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Submit button that shows the score","datePublished":"2022-10-08T18:00:15+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-submit-button-that-shows-the-score\/"},"wordCount":37,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["javascript"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-submit-button-that-shows-the-score\/","url":"https:\/\/jassweb.com\/solved\/solved-submit-button-that-shows-the-score\/","name":"[Solved] Submit button that shows the score - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-10-08T18:00:15+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-submit-button-that-shows-the-score\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-submit-button-that-shows-the-score\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-submit-button-that-shows-the-score\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Submit button that shows the score"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/14747","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=14747"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/14747\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=14747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=14747"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=14747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}