From 20bf43241723d1e2cb976caf66339b3435a4ceb1 Mon Sep 17 00:00:00 2001 From: hitler <1131367992@qq.com> Date: Thu, 20 Jun 2019 17:12:04 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=9D=E4=BB=A3=E5=8F=8C=E5=A8=87=E7=AE=80?= =?UTF-8?q?=E9=99=8B=E8=AE=A1=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- soccer-star.js | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 soccer-star.js diff --git a/soccer-star.js b/soccer-star.js new file mode 100644 index 0000000..a2cb19f --- /dev/null +++ b/soccer-star.js @@ -0,0 +1,94 @@ +//常量 +const TOP_LEAGUE_CHAMPION = 10; +const UEFA_CHAMPIONS_LEAGUE_CHAMPION = 20; + +const WORLD_CUP = [20, 10, 5]; + +const EUROPEAN_CUP = [10, 5, 2]; + +const AMERICA_CUP = [5, 2]; + +const DEADLINE = 2018; + +class SoccerStar { + //类 + constructor(obj) { + this.name = obj.name; + this.isEuropean = obj.isEuropean; //布尔 + this.leagueChampionCount = obj.leagueChampionCount; + this.championsChampionCount = obj.championsChampionCount; + this.wordlCupCount = obj.wordlCupCount; + this.europeanCupCount = obj.europeanCupCount; + this.americaCupCount = obj.americaCupCount; + } + + calClubPoints() { + //函数 + return ( + this.leagueChampionCount * TOP_LEAGUE_CHAMPION + + this.championsChampionCount * UEFA_CHAMPIONS_LEAGUE_CHAMPION + ); + } + + calNationalPoints() { + if (this.isEuropean) { + return ( + this._calPoints(this.wordlCupCount, WORLD_CUP) + + this._calPoints(this.europeanCupCount, EUROPEAN_CUP) + ); + } else { + return ( + this._calPoints(this.wordlCupCount, WORLD_CUP) + + this._calPoints(this.americaCupCount, AMERICA_CUP) + ); + } + } + + _calPoints(count, point) { + //私有属性 + let points = 0; //变量 + if (Array.isArray(count) && Array.isArray(point)) { + count.forEach((n, i) => { + points += point[i] * n; + }); + } else { + points = point * count; + } + return points; + } +} + +const PORTUGAL_SUPERSTAR = new SoccerStar({ + name: "Cristiano Ronaldo", + isEuropean: true, + leagueChampionCount: 5, + championsChampionCount: 5, + wordlCupCount: [0, 0, 1], + europeanCupCount: [1, 1, 1], + americaCupCount: null +}); + +const ARGENTINA_SUPERSTAR = new SoccerStar({ + name: "Lionel Messi", + isEuropean: false, + leagueChampionCount: 4, + championsChampionCount: 9, + wordlCupCount: [0, 1, 0], + europeanCupCount: null, + americaCupCount: [0, 3] +}); + +console.log( + ` + ${ + PORTUGAL_SUPERSTAR.name + } -- 俱乐部成绩得分:${PORTUGAL_SUPERSTAR.calClubPoints()}--国家队成绩得分:${PORTUGAL_SUPERSTAR.calNationalPoints()} + ` +); +console.log( + ` + ${ + ARGENTINA_SUPERSTAR.name + } -- 俱乐部成绩得分:${ARGENTINA_SUPERSTAR.calClubPoints()}--国家队成绩得分:${ARGENTINA_SUPERSTAR.calNationalPoints()} + ` +);