Created
January 14, 2014 15:27
-
-
Save areagray/8420073 to your computer and use it in GitHub Desktop.
Coderbyte: Using the JavaScript language, have the function CountingMinutesI(str) take the str parameter being passed which will be two times (each properly formatted with a colon and am or pm) separated by a hyphen and return the total number of minutes between the two times. The time will be in a 12 hour clock format. For example: if str is 9:…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Coderbyte Challenge | |
function CountingMinutesI(str) { | |
var when, | |
offset, | |
mins, | |
segments = [], | |
ampm, | |
hours, | |
totalMins=[], | |
diff; | |
//parse parameters | |
str=str.split('-'); | |
for (var i = 0; i<str.length; i++) { | |
//grab am pm suffix | |
ampm = str[i].slice(str[i].length-2); | |
str[i]= str[i].substr(0, str[i].length-2); | |
//split the rest | |
segments = str[i].split(':'); | |
hours= parseInt(segments[0]); | |
mins=parseInt(segments[1]); | |
//normalize mins | |
if ((hours==12) && (ampm=='am')){ | |
console.log('zeroing hours'); | |
hours=0; | |
} | |
if ((ampm=='pm') && (hours != 12)){ | |
hours = hours+12; | |
} | |
combomins=((hours*60)+mins); | |
totalMins.push((hours * 60) + mins); | |
} | |
//account for next day | |
if (totalMins[1]<totalMins[0]){ | |
totalMins[1] += 1440; | |
} | |
return totalMins[1]-totalMins[0]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
using System;
class MainClass {
public static string StringChallenge(string str) {
}
static void Main() {
}
}