我正在寫一個簡單的計算器,它可以隨時在HTML頁面上累積連續的數字流,限制爲100個節點。在html中總結選定的值
我可以選擇與當前頁面上的節點:
var els = document.querySelectorAll('span.class');
var len = els.length;
arr = [];
for (var i=0; i < len; i++) {
var no = els[i].innerHTML;
arr.push(no);
}
console.log(no);
這裏就是我一直在探索,也許你可以提出一個簡單的或改進的方法是什麼?
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.
我一直有與循環比較值的麻煩。 2個數組有重疊值的拼接函數嗎?
在下面的例子中,所需的陣列將是: [1,2,3,2,1,8,7]
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<arr1.length; i++)
{
for (j=0; j<arr2.length; j++)
{
// number exists in both arrays
// next number is next index
if (arr1[i] == arr2[j] && arr1[i+1] == arr2[j+1] && 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<arr5.length; k++)
{
total += parseInt(arr5[k]);
}
console.log(total);
有沒有辦法接受的值一次一個? – Matt
Comeon,作爲100+代表用戶,你如何在不顯示代碼/努力的情況下提出問題? – Praveen
除非您向我們展示您的一些代碼,否則我們無法理解您的問題。 –