-1
我想擺脫「未定義」,並平均輸出到控制檯的priceList數組值。我使用https://experts.shopify.com/designers網頁在Chrome控制檯中應用此代碼。感謝您的幫助!JavaScript來計算環路輸出的平均值
//selects the all of the designer cards
var experts = document.querySelectorAll('.expert-card__content');
//This tells us how many designer cards there are
var expertsQty = experts.length;
var expertContent =document.getElementsByClassName("expert-list-summary txt--minor");
//expertContent[0].innerText
var i;
var priceMedian;
var prices;
var priceList = 0;
for(i=0; i<experts.length; i++){
//Grabs the content
var str = expertContent[i].innerText;
//This determines the position of the last $ in the text
var strPosition = expertContent[i].innerText.lastIndexOf("$");
//This goes to the position of the $ and shows the 1 word after it.
var expertPrice= str.substring(strPosition + 1);
//iterate through expert content
parseInt(expertPrice);
parseInt(prices);
priceList = prices += expertPrice;
}
您的問題有點含糊。你想知道什麼? –
'parseInt'接受一個字符串並返回一個數字!它應該像這樣使用: 'var number = parseInt(「45」);'! –
加上網站上的數字是這種格式'3,456'這不是一個有效的數字! –