일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- SpringBoot
- stylesheet
- 자바스크립트
- 구글맵스
- Linux
- button
- 랜덤번호
- 스프링부트
- fragment
- Kotlin
- scrollview
- 리액트
- 오버라이딩
- 안드로이드
- Hook
- Android
- JavaScript
- React
- nodejs
- JS
- Javscript
- TextView
- 랜덤넘버
- RecyclerView
- TypeScript
- 코틀린
- Java
- npm
- array
- GoogleMaps
- Today
- Total
타닥타닥 개발자의 일상
자바 스크립트 if 조건문으로 함수 만들어서 원하는 결과 출력하기 본문
문제) 돌고래팀과 코알라팀이 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
'코딩 기록 > JavaScript' 카테고리의 다른 글
배열을 받고 형식을 갖춘 문자로 출력하기 javascript JS (0) | 2022.12.06 |
---|---|
자바스크립트 함수/배열 이용해서 배열에 원하는 값 저장하기 array (0) | 2022.11.16 |
JavaScript 기초 - 변수와 데이터 타입 : let / const / number / string/ boolean / null / undefine / symbol (0) | 2022.08.11 |
오픈 소스 pagination 이용하여 웹페이지 페이징하기 / 페이지 나누기 / javascript (0) | 2022.02.25 |
CGV 웹사이트 크롤링해서 HighChart 이용한 예매율 순위표 만들기 / j soup 이용 (0) | 2022.02.25 |