Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You have a number of options here. The most straight forward is as Amit suggested in the comment, just return the total from each of the methods. This may or may not work depending on how you plan on using these methods. Something like the following should work for your purposes.</p> <pre><code>function ToyBox() { this.total = 0; } ToyBox.prototype.addToy1 = function() { var i = document.getElementById("qty_toy1"); var qtytoy1 = i.options[i.selectedIndex].text; var qtytoy1tot = qtytoy1 * 26.99; this.total += qtytoy1tot; var generateTableToy1="&lt;table width=\"210px\" border=\"0\"&gt;" +" &lt;tr&gt;" +" &lt;td width=\"140\"&gt;Optimus Prime&lt;/td&gt;" +" &lt;td width=\"25\"&gt;x " + qtytoy1 + "&lt;/td&gt;" +" &lt;td width=\"45\"&gt;&amp;pound;" + qtytoy1tot + "&lt;/td&gt;" +" &lt;/tr&gt;" +"&lt;/table&gt;"; document.getElementById("toy1_add").innerHTML=generateTableToy1; }; ToyBox.prototype.addToy2 = function() { var i = document.getElementById("qty_toy2"); var qtytoy2 = i.options[i.selectedIndex].text; var qtytoy2tot = qtytoy2 * 14.39; this.total += qtytoy2tot; var generateTableToy2="&lt;table width=\"210px\" border=\"0\"&gt;" +" &lt;tr&gt;" +" &lt;td width=\"140\"&gt;Bumblebee&lt;/td&gt;" +" &lt;td width=\"25\"&gt;x " + qtytoy2 + "&lt;/td&gt;" +" &lt;td width=\"45\"&gt;&amp;pound;" + qtytoy2tot + "&lt;/td&gt;" +" &lt;/tr&gt;" +"&lt;/table&gt;"; document.getElementById("toy2_add").innerHTML=generateTableToy2; }; var toyBox = new ToyBox(); toyBox.addToy1(); toyBox.addToy2(); console.log(toyBox.total); </code></pre> <p>Keep in mind, you really just have one function here, and you should pass in the variable parts in as parameters to the function, but I think that may be beyond the scope of this question. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload