AlprTbk adlı üyeden alıntı: mesajı görüntüle
var urunFiyatlari = [300,200,600,800,900,1500,1600,100,50]
var indirim = 0
urunFiyatlari.enumerated().forEach { (index,element) in
if index % 3 == 2 {
indirim += element
print(indirim)
}
}
Eğer doğru anladıysam yukarıdaki yazdığım kod işinizi görecektir.
3. ürün fiyatı = 600
6. ürün fiyatı = 1500
9.ürün fiyatı = 50
çıktı = 2150
Kod bu
element koyunca type hatası alıyorum.
/// Third item free when you buy any two deodorants (product codes 65 & 66)
class BuyTwoGetThirdFreeOffer :Offer {

init(){
super.init(name: "3 for 2 on Deodorants")
applicableProductIds = [65,66];
}
override func isApplicableTo(list: [ShoppingItem]) -> Bool {
return list.reduce(false) { $0 || applicableProductIds.contains($1.productId) }
}

override func valueOfDiscountFrom(list: [ShoppingItem]) -> Int {
//order the list by price before discounting every third item
var a = applicableProducts(list: list)
a = a.sorted { $0.priceInPence > $1.priceInPence }
var price: Int = 0
for product in a {
a.enumerated().forEach { (index,element) in
if index % 3 == 2{

price += product.priceInPence
}



}

}
return price
}
}
Testi de bu hocam
price değişkenin 219 438 538 olması gerekiyor ürün dizideki ürün sayısına göre
 func testBuyTwoGetThirdFree5Items(){
        addMultiple(prodId: 14, quantity: 2) //Lynx Africa    319p
        addMultiple(prodId: 15, quantity: 3) //Dove deodorant 219p
        XCTAssertEqual(receipt.discount(),219,"Buy two get third free correct with 5 items");
    }
    
    func testBuyTwoGetThirdFree6ItemsCheapestFree(){
        addMultiple(prodId: 15, quantity: 4) //Dove deodorant 219p
        addMultiple(prodId: 14, quantity: 2) //Lynx Africa    319p
        XCTAssertEqual(receipt.discount(),438,"Buy two get third free correct with 6 items (4/2)");
    }
    
    func testBuyTwoGetThirdFree6ItemsOneOfEachPriceFree(){
        addMultiple(prodId: 15, quantity: 3) //Dove deodorant 219p
        addMultiple(prodId: 14, quantity: 3) //Lynx Africa    319p
        XCTAssertEqual(receipt.discount(),538,"Buy two get third free correct with 6 items (3/3)");