λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°

개발/Spring

JavaScript 예제 μ½”λ“œ (axios/restAPI/HackerRank)

https://hianna.tistory.com/407

 

[Javascript] λ°°μ—΄ map() ν•¨μˆ˜λ‘œ λ³€κ²½λœ μƒˆλ‘œμš΄ λ°°μ—΄ μƒμ„±ν•˜κΈ°

map() ν•¨μˆ˜λŠ” 배열을 λ‹€λ£° λ•Œ 맀우 자주, μœ μš©ν•˜κ²Œ μ‚¬μš©λ˜λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€. λ°°μ—΄μ˜ map() ν•¨μˆ˜λŠ”, 배열을 μˆœνšŒν•˜λ©΄μ„œ 각 element의 값을 λ³€κ²½ν•˜μ—¬ μƒˆλ‘œμš΄ 배열을 λ§Œλ“€μ–΄ μ€λ‹ˆλ‹€. map() ν•¨μˆ˜λ‘œ μƒˆλ‘œμš΄ λ°°μ—΄

hianna.tistory.com

 

const axios = require('axios');

async function getTotalGoals(team, year) {
    try{
        var ans = 0, totalPage = 1, t = 1;
        while (t <= totalPage){
            const res1 = await axios.get('https://jsonmock.hackerrank.com/api/football_matches/', {
                params: {
                    year : year,
                    team1 : team,
                    page : t
                }
            });
            totalPage = res1.data.total_pages;
            for (var item of res1.data.data){
                ans = ans + parseInt(item.team1goals);
            }
            t+=1;
        }
        
        t=1;
        while (t <= totalPage){
            const res2 = await axios.get('https://jsonmock.hackerrank.com/api/football_matches/', {
                params: {
                    year : year,
                    team2 : team,
                    page : t
                }
            });
            totalPage = res2.data.total_pages;
            for (var item of res2.data.data){
                ans = ans + parseInt(item.team2goals);
            }
            t+=1;
        }
        return ans;
    } catch (error) {
        console.error(error);
    }
}

async function main() {
    const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const team = readLine();

    const year = parseInt(readLine().trim(), 10);

    const result = await getTotalGoals(team, year);

    ws.write(result + '\n');

    ws.end();
}

 

const axios = require('axios');
async function getWinnerTotalGoals(competition, year) {
    try {
        const getWinner = await axios.get('http://jsonmock.hackerrank.com/api/football_competitions/',{
            params : {
                name : competition,
                year : year
            }
        });
        const winner = getWinner.data.data[0].winner
        //console.log(winner);
        
        let t = 1, totalPage = 1, ans =0; 
        while (t <= totalPage){
            const getContests1 = await axios.get('http://jsonmock.hackerrank.com/api/football_matches/', {
                params : {
                    competition : competition,
                    year : year,
                    team1 : winner,
                    page : t
                }
            });
            totalPage = getContests1.data.total_pages;
            t+=1;
            //console.log(getContests1.data.data);
            for (let item of getContests1.data.data) {
                //console.log(item);
                ans += parseInt(item.team1goals);
            }
        }
        //console.log(ans);
        t = 1;
        totalPage = 1;
        while (t <= totalPage){
            const getContests2 = await axios.get('http://jsonmock.hackerrank.com/api/football_matches/', {
                params : {
                    competition : competition,
                    year : year,
                    team2 : winner,
                    page : t
                }
            });
            totalPage = getContests2.data.total_pages;
            t+=1;
            //console.log(getContests2.data.data);
            for (let item of getContests2.data.data) {
                //console.log(item);
                ans += parseInt(item.team2goals);
            }
        }
        
        return ans;
        
    } catch(error){
        console.error(error);
    }
}

 

const axios = require('axios');
async function getNumDraws(year) {
    try {
        var goal = 0, ans = 0;
        while (goal <= 10) {
            const matches1 = await axios.get('http://jsonmock.hackerrank.com/api/football_matches/', {
                params : {
                    year : year,
                    team1goals : goal,
                    team2goals : goal
                }
            });
            goal += 1;
            //console.log(matches1.data.total);
            ans += matches1.data.total;
        }
        return ans;
    } catch(error){
       console.error(error) 
    }   
}
λ°˜μ‘ν˜•