// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// write your code in JavaScript (Node.js 4.0.0)
function solution(S) {
var lineFormat = "hh:mm:ss,nnn-nnn-nnn";
var lineSeparator = '\n';
var phoneCalls = S.split(lineSeparator);
var dictionary = {}; // elements like {cost: <integer for cents>, duration: <integer for seconds>}
var result = CostOfAllPhoneCalls(phoneCalls);
return result;
function ComputeSeconds(phoneCall) {
var result = 0;
// splitting according to lineFormat
var duration = phoneCall.split(',')[0].split(':');
var hours = parseInt(duration[0], 10);
var minutes = parseInt(duration[1], 10);
var seconds = parseInt(duration[2], 10);
result = hours * 3600 + minutes * 60 + seconds;
return result;
}
function UpdateDictionary(phoneCall, numCents, numSeconds) {
// splitting according to lineFormat
var key = phoneCall.split(',')[1];
if (! dictionary[key]) {
dictionary[key] = {
cost: 0,
duration: 0
};
}
dictionary[key].cost += numCents;
dictionary[key].duration += numSeconds;
}
function CostForLessThanFiveMinutes(numSeconds) {
var result = 3 * numSeconds;
return result;
}
function CostForFiveMinutesOrMore(numSeconds) {
var numMinutes = Math.floor(numSeconds / 60) + (numSeconds % 60 ? 1 : 0);
var result = 150 * numMinutes;
return result;
}
function CostOfOnePhoneCall(phoneCall) {
var result = 0;
var numSeconds = ComputeSeconds(phoneCall);
if (numSeconds < 5*60) {
result = CostForLessThanFiveMinutes(numSeconds);
} else {
result = CostForFiveMinutesOrMore(numSeconds);
}
UpdateDictionary(phoneCall,...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.