JS10 Day 3: Arrays

개요[ | ]

Day 3: Arrays
해커랭크 10 Days of Javascript
# 문제 비고
2-3 Day e
6 JS10 Day 2: Conditional Statements: If-Else
7 JS10 Day 2: Conditional Statements: Switch
8 JS10 Day 2: Loops
9 JS10 Day 3: Arrays
10 JS10 Day 3: Try, Catch, and Finally
11 JS10 Day 3: Throw

상단
'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });
    
    main();    
});

function readLine() {
    return inputString[currentLine++];
}
구현예시 1
function getSecondLargest(nums) {
    let first = 0;
    for (const num of nums) {
        if (first < num) first = num;
    }
    let second = 0;
    for (const num of nums) {
        if (second < num && num < first) second = num;
    }
    return second;
}
구현예시 2
function getSecondLargest(nums) {
    let first = 0;
    for( let i=0; i<nums.length; i++ ) {
        if( nums[i] > first ) first = nums[i];
    }
    let second = 0;
    for( let i=0; i<nums.length; i++ ) {
        if( nums[i] > second && nums[i] < first ) second = nums[i];
    }
    return second;
}
하단
function main() {
    const n = +(readLine());
    const nums = readLine().split(' ').map(Number);
    
    console.log(getSecondLargest(nums));
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}