Skip to content

Instantly share code, notes, and snippets.

@MohamedGamil
Last active April 17, 2022 06:04
Show Gist options
  • Save MohamedGamil/bea816f2f84a5c473c41066c7d7996c7 to your computer and use it in GitHub Desktop.
Save MohamedGamil/bea816f2f84a5c473c41066c7d7996c7 to your computer and use it in GitHub Desktop.
HackerRank / Time Conversion
'use strict';
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
/*
* Complete the 'timeConversion' function below.
*
* The function is expected to return a STRING.
* The function accepts STRING s as parameter.
*/
function timeConversion(s) {
const isPM = !!s.match(/(PM)/ig);
let [hours, minutes, seconds] = s.replace(/(AM|PM)/ig, '').split(':');
if (isPM && ~~hours < 12) {
hours = ~~hours + 12;
}
if (!isPM && ~~hours === 12) {
hours = 0;
}
if (~~hours < 10) {
hours = `0${~~hours}`
}
return `${hours}:${minutes}:${seconds}`;
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const s = readLine();
const result = timeConversion(s);
ws.write(result + '\n');
ws.end();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment