hacker rank time conversion js
'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) {
// Write your code here
let format = s.slice(8,10);
let [hours,minutes,seconds] = s.split(":").map(Number);
seconds = s[6]+s[7]
let startTime = s.slice(0,2);
if(startTime == "12"){
if(format === "AM"){
hours = "00";
}else{
hours = "12";
}
}else if(format == "PM"){
hours = hours+12
}else{
hours = hours
}
// The padStart() method is used to add padding to the left of a string to make it a certain length.
hours = hours.toString().padStart(2, '0')
minutes = minutes.toString().padStart(2, '0')
seconds = seconds.toString().padStart(2, '0')
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();
}
Comments
Post a Comment