에러&&공부노트

[PCCP 기출문제] 1번 / 동영상 재생기

역발산기개세 2024. 12. 2. 18:04

풀이

// "mm:ss" 형식의 시간 문자열을 초 단위로 변환
function timeToSeconds(timeStr) {
    const [minutes, seconds] = timeStr.split(":").map(Number);
    return minutes * 60 + seconds;
}

// 초 단위를 "mm:ss" 형식의 시간 문자열로 변환
function secondsToTime(totalSeconds) {
    const minutes = Math.floor(totalSeconds / 60) % 60;
    const seconds = totalSeconds % 60;
    return `${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
}

function solution(video_len, pos, op_start, op_end, commands) {
    // 시간들을 초 단위로 변환
    const videoLengthSeconds = timeToSeconds(video_len);
    const posSeconds = timeToSeconds(pos);
    const startSeconds = timeToSeconds(op_start);
    const endSeconds = timeToSeconds(op_end);
    
    let currentSeconds = posSeconds;
    
    // 오프닝 구간인 경우 오프닝이 끝나는 위치로 이동
    if (currentSeconds >= startSeconds && currentSeconds <= endSeconds) {
        currentSeconds = endSeconds;
    }
    
    commands.forEach(command => {
       switch(command){
           case "prev":
               if (currentSeconds < 10) {
                    currentSeconds = 0;
                } else {
                    currentSeconds = currentSeconds - 10;
                }
                break;
           case "next":
                if(videoLengthSeconds - currentSeconds < 10) {
                    currentSeconds = videoLengthSeconds;
                } else {
                    currentSeconds = currentSeconds + 10;
                }
                break;
           default:
             break;
       } 
        
        // 오프닝 구간인 경우 오프닝이 끝나는 위치로 이동
        if (currentSeconds >= startSeconds && currentSeconds <= endSeconds) {
            currentSeconds = endSeconds;
        }
    });
        
    return secondsToTime(currentSeconds);
}