-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
80 lines (60 loc) · 2.02 KB
/
main.js
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//Esercizio bowling replicato in classe
//Global Tag Declaration
const bowling = {
//Current players
"players" : [],
//Set players score randomly
"setScore" : function() {
this.players.forEach( (nPlayer) => {
for (let i = 0; i < 10; i++) {
nPlayer.scores.push(random1_10());
}
}
)
},
//Set player's total score
"setTotalScore" : function() {
this.players.forEach( (nPlayer) =>
nPlayer.totalScore = nPlayer.scores.reduce( (acc,cur) => acc + cur , 0 )
);
},
//Declare games winner
"declareWinner" : function(){
let winnerScore = 0;
let winnerName = "";
this.players.forEach( (nPlayer) => {
if (nPlayer.totalScore > winnerScore){
winnerScore = nPlayer.totalScore;
winnerName = nPlayer.name;
}
}
)
console.log(`The winner is: ${winnerName} with ${winnerScore} points.`)
},
//Create a new player (call it how many times how many players you have)
"createNewPlayer": function(){
let newPlayerName = prompt("insert new player name");
this.players.push( {"name" : newPlayerName , "scores" : [] , "totalScore" : 0} )
},
//Create players ranking
"ranking" : function() {
let rankedPlayers = this.players.map( (pl) => ({...pl}) );
rankedPlayers.sort( (a , b) => b.totalScore - a.totalScore );
console.log(this.players);
for (let i = 0; i < rankedPlayers.length; i++) {
console.log(`Position: ${i + 1} - Name: ${rankedPlayers[i].name} - Points: ${rankedPlayers[i].totalScore}`);
}
}
} //bowling
//Main
bowling.createNewPlayer();
bowling.createNewPlayer();
bowling.createNewPlayer();
bowling.setScore();
bowling.setTotalScore();
bowling.declareWinner();
bowling.ranking();
//Functions
function random1_10() {
return Math.round( Math.random() * (10 - 0) + 0 );
}