There is an array of calls for calls "is a string with the date and number of seconds spoken." The first 100 (one hundred) minutes per day are evaluated at 1 coin per minute; After 100 minutes in one day, each minute costs 2 coins per minute; All calls are rounded to the nearest minute. For example, 59 s ≈ 1 min, 61 s ≈ 2 min;

I have the answer in the end 502 and should be 106

var calls = ["2014-02-05 60", "2014-02-05 60", "2014-02-05 60", "2014-02-05 6000"],///ответ должен быть 106 data = [], limit = 100, price = 0, priceOfDay = 0, thatDayDuration = 0, minutes, thatDayMinutes, duration, price; calls.forEach(function(e){ var tmp = e.split(" "); data.push({'date' : tmp[0], 'duration' : tmp[1]}); }) data.forEach(function(el1,idx1){ var out = false; data.forEach(function(el2,idx2){ if(el1.date==el2.date && idx1!=idx2){ out = true; thatDayMinutes = Math.ceil(el2.duration/60); } }) if(out){ thatDayDuration += thatDayMinutes; thatDayMinutes = (thatDayDuration > 100) ? ((thatDayDuration - 100)*2)+100 : thatDayDuration; priceOfDay = thatDayMinutes; }else{ duration = Math.ceil(el1.duration/60); minutes = (duration > 100) ? (duration-100)*2+100 : duration; price += minutes; } total = price + priceOfDay; }) console.log(total) 

  • Tell me what is the meaning of the variable out? - HasmikGaryaka
  • You have a contradiction in the condition. If "all calls are rounded to the nearest minute", then 61 seconds should be rounded up to 1 minute, and not to two, because 1 minute 1 second is much closer to 1 minute than to 2 minutes. - Yaant
  • @Yaant In favor of the provider. - HasmikGaryaka

1 answer 1

The first 100 (one hundred) minutes per day are evaluated at 1 coin per minute; After 100 minutes in one day, each minute costs 2 coins per minute.

 const calls = [ "2014-02-05 60", // 1 монета "2014-02-05 60", // 1 монета "2014-02-05 60", // 1 монета "2014-02-05 6000", // 97 монет + 6 монет "2014-02-06 6000", // 100 монет "2014-02-06 50", // 2 монеты ]; // == 208 монет const price = 1; const overLimitPrice = 2; const limit = 100; const aggregated = calls.reduce((aggregated, call) => { const [date, seconds] = call.split(/\s+/); const minutes = Math.ceil(seconds/60); aggregated[date] = minutes + (aggregated[date]||0); return aggregated; }, {}); const total = Object.values(aggregated).map(minutes => { const overLimit = minutes - limit; return (overLimit>0) ? overLimit * overLimitPrice + limit * price: minutes * price; }).reduce((sum, cost) => sum+cost, 0); console.log(total);