Simplifying Counter of Checked Checkboxes Is the following code a proper solution to my problem? I have a feeling it can be simplified quite a bit.


  If one of the first four checkboxes is checked the sum is 1; if two 1.5; if three 2; if four 2.5. If the bar5 is checked then it's 1 + calculated value of first four (if any is checked). Afterwards, multiply by 100.




function checkboxCount() {
 var form = document.forms["calculator"];
 var bar5 = form.elements["bar5"];
 var foo = document.querySelectorAll("input[name=foo]");
 count = 0;
 
 for (var i = 0; i &lt; foo.length; i++) {
  if (foo[i] &amp;&amp; foo[i].checked == true) {
   count++;
  }
 }
 if (count == 1 &amp;&amp; bar5.checked == true) {
  count = 1;
 } else if (count == 1) {
  count = 1;
 }
 if (count == 2 &amp;&amp; bar5.checked == true) {
  count = 2;
 } else if (count == 2) {
  count = 1.5;
 }
 if (count == 3 &amp;&amp; bar5.checked == true) {
  count = 2.5;
 } else if (count == 3) {
  count = 2;
 }
 if (count == 4 &amp;&amp; bar5.checked == true) {
  count = 3;
 } else if (count == 4) {
  count = 2.5;
 }
 if (count == 5 &amp;&amp; bar5.checked == true) {
  count = 3.5;
 }
 return count;
}

function calculateTotalCost() {
 var totalCost = 0;
 var printCost = document.getElementById('calculatedCost');
 totalCost = 100 * checkboxCount();

 printCost.innerHTML = "$" + totalCost;
}
&lt;form id="calculator"&gt;

&lt;fieldset&gt;
&lt;label&gt;&lt;input type="checkbox" name="foo" id="bar1" onclick="calculateTotalCost()"&gt;Bar 1&lt;/label&gt;
&lt;label&gt;&lt;input type="checkbox" name="foo" id="bar2" onclick="calculateTotalCost()"&gt;Bar 2&lt;/label&gt;
&lt;label&gt;&lt;input type="checkbox" name="foo" id="bar3" onclick="calculateTotalCost()"&gt;Bar 3&lt;/label&gt;
&lt;label&gt;&lt;input type="checkbox" name="foo" id="bar4" onclick="calculateTotalCost()"&gt;Bar 4&lt;/label&gt;
&lt;label&gt;&lt;input type="checkbox" name="foo" id="bar5" onclick="calculateTotalCost()"&gt;Bar 5&lt;/label&gt;
&lt;/fieldset&gt;

&lt;span id="calculatedCost"&gt;&lt;/span&gt;
&lt;/form&gt;
The formula can be simplified:


for the first four checkboxes, if any of the items are checked, multiple the number checked by 0.5 and add 0.5
Then add 1 if the fifth checkbox is checked.


So it might be simpler to just have a reference to the first for checkboxes and then the 5th checkbox. For the first for, one can use document.getElementsByName('foo') to get a NodeList, and then put those nodes in an array using Array.from() and use Array.slice() to get the first four checkboxes.

var firstFourBars = Array.from(document.getElementsByName('foo')).slice(0, 4);


For the record - I did consider using a CSS selector identical to that of the answer by @200_success (i.e. input[name=foo]:not(#bar5):checked) but because the NodeList returned by document.querySelectorAll() is not live, that DOM query would need to be run each time. See point #3 for more details.
Event delegation can be used to avoid adding a click handler to each checkbox in the HTML. The code below utilizes document.addEventListener() to call the calculateTotalCost whenever a click occurs. Because of this change, that function receives a reference to the event as the first argument. This allows changes based on any click anywhere but conditional logic dependent on event.target could be added.
DOM references can be stored in variables at the start of the script, so as to avoid multiple lookups (e.g. var printCost)
One other simplification:


var totalCost = 0;
var printCost = document.getElementById('calculatedCost');
totalCost = 100 * checkboxCount();



There is little point in setting totalCost to 0 and then assigning it 100 * checkboxCount(). Just assign it the latter product:

var totalCost = 100 * checkboxCount();





document.addEventListener('DOMContentLoaded', function() { //Wait for DOM to be ready
  //cache DOM references
  var printCost = document.getElementById('calculatedCost');
  var firstFourBars = Array.from(document.getElementsByName('foo')).slice(0, 4);
  var bar5 = document.forms["calculator"].elements["bar5"];

  document.body.addEventListener('click', calculateTotalCost);
  // helper function for filter
  function isChecked(checkbox) {
    return checkbox.checked;
  }

  function checkboxCount() {
    var count = firstFourBars.filter(isChecked).length;
    if (count) {
      count = count * 0.5 + 0.5;
    }
    if (bar5.checked) {
      count += 1;
    }
    return count;
  }

  function calculateTotalCost(event) {
    //could conditionally execute based on event.target
    var totalCost = 100 * checkboxCount();
    printCost.innerHTML = "$" + totalCost;
  }

});
&lt;form id="calculator"&gt;

  &lt;fieldset&gt;
    &lt;label&gt;&lt;input type="checkbox" name="foo" id="bar1"&gt;Bar 1&lt;/label&gt;
    &lt;label&gt;&lt;input type="checkbox" name="foo" id="bar2"&gt;Bar 2&lt;/label&gt;
    &lt;label&gt;&lt;input type="checkbox" name="foo" id="bar3"&gt;Bar 3&lt;/label&gt;
    &lt;label&gt;&lt;input type="checkbox" name="foo" id="bar4"&gt;Bar 4&lt;/label&gt;
    &lt;label&gt;&lt;input type="checkbox" name="foo" id="bar5"&gt;Bar 5&lt;/label&gt;
  &lt;/fieldset&gt;

  &lt;span id="calculatedCost"&gt;&lt;/span&gt;
&lt;/form&gt;




Edit

I forgot to mention that in the modified code above, it waits for the DOM to be ready before before querying it (notice the first line document.addEventListener('DOMContentLoaded', function() { //Wait for DOM to be ready). Not only does it prevent attempts to access DOM elements before they exist, but the variables are scoped to the callback function instead of the global namespace.