Coding course for kids: This coding exercise will help you to learn how to work with JavaScript array, array is a collection variable, whenever we want to store multiple similar type of values in one variable we use array.
For example if we want to create team, one team can have multiple players, so team can be a array variable.
Let's get familiar with JavaScript coding syntax with real-time example.
Here are the details of the task; we perform each step as described bellow.
Before you start this task, make sure you have gone though the basic coding syntax tutorial (if you are completely new to coding).
Open your Visual Studio Code or anyother SDK you have in your local machine.
First we create team and add all our friends, then we count, how many players are there in the team (like we do in school or playground).
To count how many players in team we also use .length
property, like in example below we have used team.length
var teamA = new Array(); teamA.push("Rina"); teamA.push("Tina"); teamA.push("Bina"); teamA.push("Aana"); teamA.push("Rani"); console.log("Total players in team A "+ team.length);
Learning: Learning: how to create an array and add new element to it, how to find length of an array
Now we have to create another team just like above example.
var teamB = new Array(); teamB.push("Bhumi"); teamB.push("Shilpa"); teamB.push("Anu"); teamB.push("Bunty"); teamB.push("Babli"); console.log("Total players in team B "+ team.length);
You probably have seen many sports on television; before game starts, they display all players name on display board, now you need to do the same.
console.log("Team A Players"); for(i=0;i<teamA.length;i++) { console.log(teamA[i]); }
Rina Tina Bina Aana Rani
console.log("Team B Players"); for(i=0;i<teamB.length;i++) { console.log(teamB[i]); }
Bhumi Shilpa Anu Bunty Babli
Now as per the task (instruction above), we need to remove one player from each team from 3rd and 2nd position respectively.
console.log("Team A"); /* remove 3rd player Bina which is in index 2, remove just once. */ teamA.splice(2,1) /* now again display the list */ for(i=0;i<teamA.length;i++) { console.log(teamA[i]); }
Rina Tina Aana Rani
console.log("Team B"); /* remove 2nd player Bina which is in index 1, remove just once. */ teamB.splice(1,1) /* now again display the list */ for(i=0;i<teamB.length;i++) { console.log(teamB[i]); }
Bhumi Anu Bunty Babli
Learning: how to remove one element from an array using splice method, we can remove an item using their index number from an array.
arrayVar.splice(elementIndex, noOftimes)
Now open your VS Code editor and start practicing JavaScript coding, or look at our next tutorial.
This task will give a hands-on experience to kids that help understanding how to write program in JavaScript apart from making them familiar with following JavaScript coding syntax.
Please share this tutorial with your friends and family, we develop more free tutorials for your kids, help us to help them better!
More coding tasks for kids: