타닥타닥 개발자의 일상

자바 스크립트 if 조건문으로 함수 만들어서 원하는 결과 출력하기 본문

코딩 기록/JavaScript

자바 스크립트 if 조건문으로 함수 만들어서 원하는 결과 출력하기

NomadHaven 2022. 11. 16. 22:19

 

 

문제) 돌고래팀과 코알라팀이 3번 경기를 한다. 팀별로 3번 경기의 평균 점수가 계산된다. 오직 상대편 평균점수의 2배를 달성한 팀만 이긴 것으로 간주된다. 그렇지 않을 경우엔 어느 팀도 이길수 없다.

Each team competes 3 times, and then the average of the 3 scores is calculated (so one average score per team)

. A team only wins if it has at least double the average score of the other team. Otherwise, no team wins!

 

Test data:

§ Data 1: Dolphins score 44, 23 and 71. Koalas score 65, 54 and 49

§ Data 2: Dolphins score 85, 54 and 41. Koalas score 23, 34 and 27

 

Your tasks:

 

 

1. 3판 점수의 평균을 구할수 있는, calcAverage라는 애로우 함수를 만들어라.

Create an arrow function 'calcAverage' to calculate the average of 3 scores

 

내가 푼것

//my answer
const calcAverage = (firstRount , secondRound , thirdRound) =>{
    return (firstRount+secondRound+thirdRound)/3
}

답안

//solution
const calcAvg = (a,b,c) =>(a+b+c)/3;

 

2. 위에서 만든 함수를 이용해 두 팀의 평균점수를 구해라.

Use the function to calculate the average for both teams

const dolphinFirstAvg = calcAverage(44,23,71);
const koalasFirstAvg = calcAverage(65,54,49);

const dolphinSecondAvg = calcAverage(85,54,41);
const koalasSecondAvg = calcAverage(23,34,27);

 

 

3. checkWinner라는 함수를 만들고 두팀의 평균점수를 매개변수로( ex.'avgDolphins' & 'avgKolas') 어떤 팀이 이겼고 각 팀의 평균 점수가 얼마인지 출력하라. Example: "Koalas win (30 vs. 13)"

Create a function 'checkWinner' that takes the average score of each team as parameters ('avgDolphins' and 'avgKoalas'), and then logs the winner to the console, together with the victory points, according to the rule above.

Example: "Koalas win (30 vs. 13)"

 

const checkWinner = function(firstTeamName, firstAvg, secondTeamName, secondAvg){
    if(firstAvg>=secondAvg*2){
        console.log(`${firstTeamName} win (${firstAvg}vs${secondAvg})`)
    } else if(secondAvg>firstAvg && secondAvg>=firstAvg*2){
        console.log(`${secondTeamName} win (${secondAvg}vs${firstAvg})`);
    }else {
        console.log('draw!');
    }
}

 

4. Use the 'checkWinner' function to determine the winner for both Data 1 and Data 2

checkWinner('Dolphin',dolphinFirstAvg,'Koalas',koalasFirstAvg);

checkWinner('Dolphin',dolphinSecondAvg,'Koalas',koalasSecondAvg);

 

문제 출처: Udemy 강의 The Complete JavaScript Course 2023 : From Zero to Export

Comments