0
// Produce the element with largest sum of each integer in the string.
var phoneList1 = ["111-111-1113", "111-111-1113", "111-111-1111", "111-111-1112"];
for (var i = 0; i < phoneList1.length; i++) { // Iterating through the array
phoneList1[i] = phoneList1[i].replace(/-/g, ''); // Removing the dashes (-)
phoneList1[i] = parseInt(phoneList1[i], 10); // Converting to integers
}
這就是我所得到的。如何生成字符串中每個整數的總和最大的元素?
給定一組電話號碼,我怎樣才能找到列表中的項目,當字符串中的每個數字加在一起時,是列表中所有項目中最大的。
例子:"111-111-1111" = 10
您能否澄清此方法的功能?當你解析電話號碼時,你的意思是「連續整數最大的元素」?您的樣本輸入的預期輸出是什麼? – Nate
你想要的輸出是什麼? ** 111-111-1113 **或** 1111111113 **或其他東西? – mmuzahid
按** - **分割電話號碼,並將數組中的每個元素轉換爲數字並進行總和。如果總和大於先前的值,則將其存儲爲下一次迭代的結果。 – mmuzahid