Note that there are some explanatory texts on larger screens.

plurals
  1. POSum selected values in html
    text
    copied!<p>I'm writing a simple calculator that adds up a continuous stream of numbers on an HTML page limited to 100 nodes at any time.</p> <p>I can select the nodes on the current page with: </p> <pre><code>var els = document.querySelectorAll('span.class'); var len = els.length; arr = []; for (var i=0; i &lt; len; i++) { var no = els[i].innerHTML; arr.push(no); } console.log(no); </code></pre> <p><br /></p> <p>Here's what I've been exploring, maybe you can suggest a simpler or improved method?</p> <blockquote> <pre><code> 1. Select values, push into an array. 2. Store array in a cookie, or htmllocalstorage 3. every 5 seconds, push new values into 2nd array. 4. Read 1st array and compare to 2nd array. 5. create new array. sum values. store new array. </code></pre> </blockquote> <p>I've been having trouble comparing the values with loops. <strong>Is there a splice function for 2 arrays with overlapping values?</strong></p> <p>In the example below, the desired array would be: [1,2,3,2,1,8,7]</p> <pre><code>var arr1 = ['1','2','3','2','1']; var arr2 = ['3','2','1','8','7']; // the first two values are now truncated off because current line is 102 var arr3 = []; for(i=0; i&lt;arr1.length; i++) { for (j=0; j&lt;arr2.length; j++) { // number exists in both arrays // next number is next index if (arr1[i] == arr2[j] &amp;&amp; arr1[i+1] == arr2[j+1] &amp;&amp; arr1[i+2] == arr2[j+2]) { arr3.push(arr1[i]); //console.log(arr1[i]+ ' '+arr2[j] + 'true ' + arr1[i+1] + ' ' + arr2[j+1]+ arr1[i+2] + ' ' + arr2[j+2]); } else { //arr3 = []; //console.log(arr1[i]+ ' '+arr2[j] + ' false'); } } } var end = arr3[0]-1; var arr4 = arr1.slice(0,end); var arr5 = arr4.concat(arr2); //console.log(arr5); var total=0; for (k=0;k&lt;arr5.length; k++) { total += parseInt(arr5[k]); } console.log(total); </code></pre>
 

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