Making of ChaiMaster Tournament by Hitesh Chaudhary using Javascript Arrays Methods
How Tournament started using Javascript Arrays
One day Hitesh Sir was taking sip of tea while watching Laughter Chef , while taking cup of tea a thought came into in his mind of arranging some non technical competition and he thought what could be better than arranging best tea maker competition as he is tea lover . He will also get to taste different flavour of tea and ultimately declare one as winner , He named competition ChaiMaster Tournament.
Storing Contestant in Arrays
To begin with Tournament , hitesh sir shortlisted two Contestant who makes different tea for tournament.He wanted to store their data with their specialities at one place ,he knew it could be done with help of Javascript Arrays, so he stored their information in array named contestants. Arrays Indexing starts from 0 to size-1.
let contestants = ["Anirudh-makes fantastic masala tea", "Mukul - Expert in making oolong tea "];
1.Push()
Hitesh Sir stored both contestant at one place in an array , but he thought there will be less competition between only two contestant , so he introduced new contestant Akash now question before him was how will he store akash with other two . Arrays.push() came as saviour for hitesh sir which adds element at last of array.
let contestants = ["Anirudh - makes fantastic masala tea", "Mukul - Expert in making oolong tea "];
contestants.push("Akash - Specialized in making kashmiri kahwa");
console.log(contestants);
// output
/*["Anirudh - makes fantastic masala tea", "Mukul - Expert in making oolong tea",
"Akash -Specialized in making kashmiri kahwa "] */
Hitesh sir says let’s begin the battle of becoming ultimate tea master
2.Concat()
Tournament has begun but hitesh sir’s team found two more contenstants , Hitesh sir decided to introduce them with direct entry in show , he added their data in seperate array named wildCard and then wanted to merge both arrays , there comes concat method of array which allows to merge two arrays,we have to just pass MainArray name and array which we want to concat . Concat method adds 2nd arrays element at last of first array.
let contestants = [
"Anirudh - makes fantastic masala tea",
"Mukul - Expert in making oolong tea",
"Akash - Specialized in making kashmiri kahwa"
];
let wildCard = [
"Tejas - Master of ginger tea",
"Ajit - King of making orange tea"
];
let newContestants = contestants.concat(wildCard);
console.log(newContestants);
/* output
["Anirudh - makes fantastic masala tea", "Mukul - Expert in making oolong tea",
"Akash -Specialized in making kashmiri kahwa","Tejas - Master of ginger tea" ,
"Ajit - King of making orange tea"]
3.Pop()
Hitesh Sir was not fan of orange tea ,so he decided to eliminate Ajit from show using pop method.Pop method does not take any input it removes last element.
newContestants .pop();
console.log(newContestants);
//outputs
// ["Anirudh - makes fantastic masala tea", "Mukul - Expert in making oolong tea",
// "Akash -Specialized in making kashmiri kahwa","Tejas - Master of ginger tea" ]
4.unshift()
There needs to be judges in competition so hitesh sir decided to call his friends to judge show along with him ,but he wanted to sit after them so he used unshift method instead of push which adds element at start.
let judges = ["Hitesh Sir"];
judges.unshift("Manu Arora","Piyush garg");
console.log(judges);
//output
// ["Manu Arora","Piyush garg","Hitesh Sir"]
Hitesh Sir :“This panel of judges looks like they are judging some coding project rather than tea competition”
5.findIndex()
While judging Akash judges wanted to give him score but for that they needed his index number . findIndex() method takes callbback function and returns first index where element matches.
let index = newContestants.findIndex(contestant => contestant === "Akash - Specialized in making kashmiri kahwa");
console.log(index)
//output
// 2
6. map()
Hitesh sir thought of giving every contenstant some title , he used map function and split function to retrieve the first word from all the contestant strings by separating them on ‘-’ and add “ The ChaiMasters” ahead of all the names.
let contestantNicknames = newContestants.map(newContestant => newContestant.split(" - ")[0] + " The ChaiMasters");
console.log(contestantNicknames);
// output
// [
// 'Anirudh The ChaiMasters',
// 'Mukul The ChaiMasters',
// 'Akash The ChaiMasters',
//'Tejas The ChaiMasters']
7.filter()
Hitesh sir and judges now decided to filter contestants based on low performance and they decided to remove contestant who make ginger tea as hitesh sir is not fan of it. filter method came into action which takes callback function to filter element.
let uniqueTalents = newContestants.filter(newContestant => !newContestant.includes(" Master of ginger tea"));
console.log(uniqueTalents);
//output
//[
// 'Anirudh - makes fantastic masala tea',
// 'Mukul - Expert in making oolong tea',
//'Akash - Specialized in making kashmiri kahwa'
//]
8.slice()
Judges discovered very good contestant at last moment ,they wanted him to enter show as wildcard directly into semis , so hitesh sir used splice method which can add,remove,update element by providing startindex,deletecount and items.
uniqueTalents.splice(0, 0, "Samay - Does Comedy while making tea");
console.log(uniqueTalents);
//outputs[
// 'Samay - Does Comedy while making tea',
// 'Anirudh - makes fantastic masala tea',
// 'Mukul - Expert in making oolong tea',
//'Akash - Specialized in making kashmiri kahwa'
//]
9.Length
It is an endgame now , hitesh sir wanted to check no of contestant left in TeaMaster tournament , so they can conduct finale, Arrays.length() method returns number of elements in an array.
NoOfContestants = uniqueTalents.length();
console.log(NoOfContestants);
//output
//4
10.Sort
It was a grand finale of ChaiMasters season 1 ,scores of all contestants were evaluated by judges . Hitesh sir decided an algorithm for winner declaration he was going to arrange all contestant in their alphabhetical order using sort method and then use math.random function to choose one index number it’s not an array method but hitesh sir decided to use it Contestant at that index number will be winner. It was all on random function to decide ultimate tea maker.
semifinaleContestants = ["Samay","Anirudh","Akash","Tejas"];
finaleContestants = semifinaleContestants.sort();
console.log(finaleContestants)
//output
//[ 'Akash', 'Anirudh', 'Samay', 'Tejas' ]
Declaring winner
let semifinaleContestants = ["Samay", "Anirudh", "Akash", "Tejas"];
let finaleContestants = semifinaleContestants.sort();
let randomIndex = Math.floor(Math.random() * 4);
console.log(finaleContestants[randomIndex]);
// output will be different for everyone , they were top 4 and all are deserving
Hitesh Sir says Everyone among four is deserving to win , so i was not able to decide all are champs for me. Winner will be decided in your own system , just copy and paste this code and decide your own winner, and let us know winner in comments.
This is the end ofChaiMaster S-1 and the winner in my system is Samay , Samay is the ultimate Tea Maker.
I hope you enjoyed this blog and understood different Array method in simple language.See you in next season of ChaiMasters! till then keep coding while enjoying cup of tea. Thank you for Reading!