1. 해커랭크
Day 1. Plus Minus
function plusMinus(arr) {
let positiveCount = 0;
let negativeCount = 0;
let zeroCount = 0;
for(let i=0; i<arr.length; i++){
if(arr[i]>0){
positiveCount++
}
if(arr[i]<0){
negativeCount++
}
if(arr[i] === 0){
zeroCount++
}
}
const positiveRatio = (positiveCount/arr.length).toFixed(6);
const negativeRatio = (negativeCount/arr.length).toFixed(6);
const zeroRatio = (zeroCount/arr.length).toFixed(6);
console.log(positiveRatio)
console.log(negativeRatio)
console.log(zeroRatio)
}
const num = 1.23456789;
const roundedNum = num.toFixed(6);
console.log(roundedNum); // "1.234568"
arr[i]
를 출력하기console.log(arr[1]) console.log(arr[2])
Day 1. Time Conversion
function timeConversion(s) {
let convertedTime = 0;
const splitS = s.split(':');
if(s.includes('AM')){
if(splitS[0] === '12'){
convertedTime = '00'+':'+splitS[1]+':'+splitS[2].replace('AM','')
}
else{
return s.replace('AM','')
}
}
if(s.includes('PM')){
const turnTo24 = 12+Number(splitS[0])
if(0<splitS[0]<12){
convertedTime = turnTo24+':'+splitS[1]+':'+splitS[2].replace('PM','')
}
if(splitS[0] === '12'){
return s.replace('PM','')
}
}
return convertedTime
}
function timeConversion(s) {
let convertedTime = 0;
const frontTime = s.slice(0,2);
if(s.includes('AM')){
if(frontTime === '12'){
convertedTime = '00'+s.slice(2,8)
}
else{
return s.slice(0,8)
}
}
if(s.includes('PM')){
const turnTo24 = 12+Number(frontTime)
if(0<frontTime<12){
convertedTime = turnTo24+s.slice(2,8)
}
if(frontTime === '12'){
return s.slice(0,8)
}
}
return convertedTime
}