Math.random allows us to randomly generate numbers using JavaScript. We can use these to our advantage and make really cool stuff. Recently I have been working on making a fantasy team generator which will basically generate a random fantasy team for us to bet on. I think that this is a very good project. The problem that I have is that I am unable to apply the required logic for it. Let explain this brief.
Points to be noted while making a team: 1.) There can be only 11 players in your team. There 22 players to select from 11 from each team. 2.) You can only have at most 7 players from a single team no more than that. 3.) You can only have 1-4 wicket-keepers, 1-6 batsmen, 1-6 all-rounders, 1-6 bowlers. 4.) You only have a maximum of 100 points each player carries a different point value one player maybe worth 10 points while one can be 7 points. These points are pre-defined. 5.)We want to keep all the above points into consideration while using Math.random() to generate our random team.
The problem I faced: --> Not able to apply logic for the 3rd condition.
We have been given specific ranges for particular roles of players now I have thought of most of the thing but I am not able to figure out how can I tell JavaScript to stay in these ranges while using random method.
Solutions that I thought of: --> Make an array for every possible combination and then randomly select one from the array and then make our team accordingly. for e.g.) const combinations = [..., [2, 1, 2, 6],...]//this can be a possible combination the combination can work because the total no. of players being selected are 11. --> Keep on calling the function until the answer is equal to 11.
What I want as an answer: --> A way to tell JavaScript to stay in those given ranges while using random method the total no. of players should 11.
What I have until now(I am still working on other things but this will help you get an idea of what I am doing):
const bangalore = [
{
Name: "Pavan Deshpande",
Type: "All-rounder",
}// example of how i have made players
//Total 22 players in this team
];
const chennai = [
{
Name: "MS Dhoni",
Type: "Wicket-keeper",
}//an example
];
const delhi = [
{
Name: "Rishab Pant",
Type: "Wicket-keeper",
}//an example
];
function addPlayerToList(listNum) {
switch (document.getElementById("playingTeam" + listNum).value) {
case "bangalore":
for (let player of bangalore) {
document.getElementById(
"playersList" + listNum
).innerHTML += `<li id='playerOfTeam'><button id='selectBtn'>✔</button>${
bangalore.indexOf(player) + 1
}.) ${player.Name}<br />${player.Type}<br /><br /></li>`;
}
break;
case "chennai":
for (let player of chennai) {
document.getElementById(
"playersList" + listNum
).innerHTML += `<li id='playerOfTeam'><button id='selectBtn'>✔</button>${
chennai.indexOf(player) + 1
}.) ${player.Name}<br />${player.Type}<br /><br /></li>`;
}
break;
}
}
document.getElementById("playingTeam1").addEventListener("change", () => {
addPlayerToList("1");
});
document.getElementById("playingTeam2").addEventListener("change", () => {
addPlayerToList("2");
});
html {
scroll-behavior: smooth;
}
* {
margin: 0;
padding: 0;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
body {
overflow-x: hidden;
}
#topChangeTeamSticky {
top: 0;
left: 0;
z-index: 100;
background: chartreuse;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: space-evenly;
-ms-flex-pack: space-evenly;
justify-content: space-evenly;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
position: -webkit-sticky;
position: sticky;
height: 70px;
width: 100%;
border-bottom: 6px solid #0099ff;
-webkit-box-shadow: 1px 1px 20px rgba(0, 0, 0, 0.4);
box-shadow: 1px 1px 20px rgba(0, 0, 0, 0.4);
}
#topChangeTeamSticky .playingTeam {
padding: 0 20px;
border: 0;
outline: 0;
height: 100%;
-webkit-box-flex: 1;
-ms-flex-positive: 1;
flex-grow: 1;
background: transparent;
color: white;
font-family: poppins, sans-serif;
font-size: 20px;
font-weight: 600;
-webkit-transition: all 0.3s ease-in-out;
transition: all 0.3s ease-in-out;
}
#topChangeTeamSticky .playingTeam:hover {
cursor: pointer;
background: #448800;
}
#topChangeTeamSticky .playingTeam option {
background: #3b3b3b;
font-size: 18px;
}
#playersList {
height: auto;
width: auto;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
margin: 30px;
border-radius: 12px;
background: #3b3b3b;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
padding: 20px;
color: white;
font-family: poppins, sans-serif;
font-size: 20px;
font-weight: 600;
}
#playersList #playersList1,
#playersList #playersList2 {
min-width: 30vw;
width: 50%;
margin: 20px;
}
#playersList #playersList1 #playerOfTeam,
#playersList #playersList2 #playerOfTeam {
position: relative;
padding: 20px;
margin: 10px;
outline: 2px solid gray;
height: 100px;
}
#playersList #playersList1 #playerOfTeam button,
#playersList #playersList2 #playerOfTeam button {
border: 0;
outline: 0;
cursor: pointer;
position: absolute;
top: 0;
right: 0;
float: right;
height: 100%;
aspect-ratio: 1/1;
font-family: poppins, sans-serif;
font-size: 30px;
font-weight: 600;
-webkit-transition: all 0.3s ease-in-out;
transition: all 0.3s ease-in-out;
}
#playersList #playersList1 #playerOfTeam button:hover,
#playersList #playersList2 #playerOfTeam button:hover {
background: chartreuse;
color: white;
}
#playersList ul {
display: block;
}
#playersList ul li {
display: block;
}
.alertText {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
color: red;
font-family: poppins, sans-serif;
font-size: 20px;
font-weight: 600;
}
/*# sourceMappingURL=style.css.map */
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="style.css">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fantasy Team Selector</title>
</head>
<body>
<div id="topChangeTeamSticky">
<select class="playingTeam" id="playingTeam1" title="Select your first team">
<option disabled selected>Team 1</option>
<option class="team" value="bangalore" id="team11">Bangalore</option>
<option class="team" value="chennai" id="team12">Chennai</option>
<option class="team" value="delhi" id="team13">Delhi</option>
<option class="team" value="hyderabad" id="team14">Hyderabad</option>
<option class="team" value="kolkata" id="team15">Kolkata</option>
<option class="team" value="mumbai" id="team16">Mumbai</option>
<option class="team" value="punjab" id="team17">Punjab</option>
<option class="team" value="rajasthan" id="team18">Rajasthan</option>
</select>
<select class="playingTeam" id="playingTeam2" title="Select your second team">
<option class="team" disabled selected>Team 2</option>
<option class="team" value="bangalore" id="team21">Bangalore</option>
<option class="team" value="chennai" id="team22">Chennai</option>
<option class="team" value="delhi" id="team23">Delhi</option>
<option class="team" value="hyderabad" id="team24">Hyderabad</option>
<option class="team" value="kolkata" id="team25">Kolkata</option>
<option class="team" value="mumbai" id="team26">Mumbai</option>
<option class="team" value="punjab" id="team27">Punjab</option>
<option class="team" value="rajasthan" id="team28">Rajasthan</option>
</select>
</div>
<p class="alertText">
Please Select Your Players In Lineups
</p>
<div id="playersList">
<ul id="playersList1"></ul>
<ul id="playersList2"></ul>
</div>
<div id="selectedPlayers">
<ul id="selectedPlayersList1"></ul>
<ul id="selectedPlayersList2"></ul>
</div>
<script src="script.js"></script>
</body>
</html>Thanks for reading my query I really appreciate your interest in this. I hope to see your answer to my question. Once again thank you!
Aucun commentaire:
Enregistrer un commentaire