Професионална програма
Loading...
+ Нов въпрос
Axiomatik avatar Axiomatik 430 Точки

Hmm, nice code but unfortunately quite a few things were missing :

Line 44 : You did not include a validation when the required sum is collected, which should break the while-loop .

Line 45-46 : Average sum should be divided by cardCount and cashCount and not by “ 2 “, which will lead to wrong results in the hidden tests.

Refactored code (100%):

 

function solve(input) {

let sum = Number(input[0]);

let command = input;

//let product = Number(input[1]);

let index = 1;

let total = 0;

let totalCard = 0;

let totalCash = 0;

let cashCount = 0;

let cardCount = 0;

 

while (index < input.length) {

 

if (command === "End") {

console.log("Failed to collect required money for charity.");

break;

}

 

let currentProduct = Number(input[index]);

 

if (index % 2 === 0) {

if (currentProduct < 10) {

console.log("Error in transaction!");

} else {

totalCard += currentProduct;

cardCount++;

total += currentProduct;

console.log("Product sold!");

}

 

} else {

if (currentProduct > 100) {

console.log("Error in transaction!");

} else {

totalCash += currentProduct;

cashCount++;

total += currentProduct;

console.log("Product sold!");

}

}

 

// total += currentProduct;

 

if (total >= sum) {

console.log("Average CS: " + (totalCash / cashCount).toFixed(2));

console.log("Average CC: " + (totalCard / cardCount).toFixed(2));

break;

}

index++;

command = input[index];

// currentProduct = Number(input[index]);

}

 

// if (total >= sum) {

// console.log("Average CS: " + (totalOdd / 2).toFixed(2));

// console.log("Average CC: " + (totalEven / 2).toFixed(2));

 

// } else {

// console.log("Failed to collect required money for charity.");

// }

}

solve(['500', '120', '8', '63', '256', '78', '317']);

//solve(['600','86','150','98','227','End']);

0
inaivanova1990 avatar inaivanova1990 2 Точки

Thank you so much.. I've been struggling to this code for a while. I didn't realised that I need a counter

0