Create Uefa champions league using array methods


understanding array with a twist using uefa champions league rule and i use my mose used 10 methods
What is an array:
→ JavaScript arrays are one of the most versatile and widely used data structures. They allow you to store, manipulate, and transform data efficiently.
but instande of understand in normal way we can understand this using one of biggest football tournament UEFA champions league .where all teams all insert eleminate a qualify using certain conditions.
so lets gets through it:—-
fist we saw the methods which i used
1. push() - Adds elements to the end of an array.
let teams = ["Barcelona", "Real Madrid", "Bayern Munich"];
teams.push("Manchester City");
console.log(teams);
// the outpus is
["Barcelona", "Real Madrid", "Bayern Munich","Manchester City"]
2. pop() - Removes the last element.
teams.pop()
console.log(teams)
// output
["Barcelona", "Real Madrid", "Bayern Munich"]
3. unshift() - Adds elements at the beginning.
teams.unshift("liverpool")
console.log(teams)
//output
["liverpool","Barcelona", "Real Madrid", "Bayern Munich"]
4. shift() - Removes the first element.
teams.shift()
console.log(teams)
//output
["Barcelona", "Real Madrid", "Bayern Munich"]
5. map() - Transforms elements.
let teamlengths=teams.map(team=>team.length);
console.log(teamlenghts);
//output
[9,11,13]
6. filter() - Filters elements based on a condition.
let longNames = teams.filter(team => team.length > 10);
console.log(longNames);
//output
["Real Madrid", "Bayern Munich"]
7. reduce() - Accumulates values.
reduce() method in JavaScript is used to accumulate values from an array into a single result.
assume teams=[“Real Madrid”,”Bayern Munich”]
iteratuion | sum | team.length | sum |
1 | 0 | 11 //string size of real madrid | 11 |
2 | 11 | 13 | 24 |
let totalLength = teams.reduce((sum, team) => sum + team.length, 0);
console.log(totalLength);
//output
24
8. for() - Loops through elements.
for(let i=0;i<=teams.lenght;i++){
console.log(teams[i]);
}
9. some() - Checks if any element meets a condition
- the some method in javascript at least one of the element matches the given condition and give true or it gives fals
let hasShortName = teams.some(team => team.length < 7);
console.log(hasShortName);
//output
true
10. every() - Checks if all elements meet a condition.
- just like some methods in the every() method if all element in array matches the condition then only it gets true else return false
let everyname=teams.every(team=>team.length>5);
condole.log(everyname)
//output
true
so we know the method now use this method we can play champions leaguge tournament
I added the rules and the code and the output for better understanding
1 : first step is to initialize our team for group stage
let teams=["Barcelona", "Real Madrid", "Bayern Munich", "Manchester City", "Liverpool", "Juventus", "PSG", "Chelsea"]
2: eleminate the teams whos length are < 8 charecters
let qualifiedteams=teams.filter(team=>team.length>=8)
console.log(qualifiedteams)
//output
Qualified Teams: ["Barcelona", "Real Madrid", "Bayern Munich", "Manchester City", "Liverpool"]
3:Reduce the teams to the top 4 based on name length based on the array iterate for four loops and then elemts are pooped
while (qualifiedTeams.length > 4) {
qualifiedTeams.pop();
}
console.log("Top 4 Teams:", qualifiedTeams);
//output
Qualified Teams: ["Barcelona", "Real Madrid", "Bayern Munich", "Manchester City", "Liverpool"]
Step 3: The champion is the one with the longest name and this is the final round
let champion = qualifiedTeams.reduce((a, b) => (a.length > b.length ? a : b));
console.log("Champion:", champion);
// output
Champion: "Manchester City"
understand the reduce method again:
the reduce() method compares two elements at a time and keeps the one with the longest name.
iterattion | a(current team) | b(next team) | Condition (a.length>b.length) | kept team |
1 | Barcalona | Real Madrid | false | Real Madrid |
2 | Real Madrid | Bayern Munich | false | Bayern Munich |
3 | Bayern Munich | Manchester City | false | Manchester City |
Please read the article and share to everyone
my linkdean profile : Linkdin
my github: Github
Subscribe to my newsletter
Read articles from Srinjoy Ghosh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
