Coding tutorial for kids: These tasks will help you to improve functional thinking and logic building ability of your kids, also help them to get familiar with JavaScript coding syntax with real-time example.
Before you start this task, make sure you have gone though the basic coding syntax tutorial (if you are completely new to coding).
Here are the coding exercise for kids.
First, Open your Visual Studio Code or anyother SDK you have in your local machine.
Create a file with name “fund-distribution.js” (you can give any name)
Inside code, whenever we write something with double forward slash (// comment)
, or within (/* comment */)
are comment, means non-functional text, not code
Create a team, write all your friends name (replace the existing name), you can increase the list if you have more friends, notice index number will always starts from zero (0), not one (1).
/*creating array and adding element using index, index starts with zero.*/ var team = new Array(); team[0] = "Akshay"; team[1] = "Arnold"; team[2] = "Shiva"; team[3] = "Amir"; team[4] = "Monica"; console.log("Team Members:"+ team.toString());
Learning: How to define array and add element using array index number.
Create a cashbox, assign zero (0), initially no money.
Whenever we declare a variable in JavaScript, we use either var or let, those are keywords.
// declaring a variable and assigning a value var cashBox=0;
Learning: How to define a variable and assign value at the same time.
VariableName =Value
Let’s assume we have 50 donors, who are contributing to fund raising; assuming everyone giving amount between 1 to 99 INR (or any currency).
var numberOfDonors =50; // creating array of fix number var donors = new Array(numberOfDonors); var receivedMoney; for(i=0;i<=donors.length;i++) { receivedMoney=Math.floor(Math.random() * 100); console.log(receivedMoney); cashBox = cashBox+ receivedMoney; } console.log("Total Collection : "+ cashBox);
Learning: How to define array with fix number of element, declaring variable, using for loop, JavaScript math function, increment the value of any variable.
Now we know how much total fund we have for distribution.
Notice, here again we create a list of people, means list object, like earlier list, but this time we create list object differently.
var needyPeople = new Array(); needyPeople.push("John"); needyPeople.push("Jadu"); needyPeople.push("Jatin"); needyPeople.push("Roshan"); needyPeople.push("Rubi"); needyPeople.push("Rahul"); needyPeople.push("Vivek"); needyPeople.push("Sajid"); needyPeople.push("Samir"); needyPeople.push("Santu"); // keep on adding people to the list console.log("total needy people "+ needyPeople.length);
Learning: How to create array and add element using push method in JavaScript.
So now we know how much total fund we have and how many needy people are there, let’s calculate how much money each person will get.
var totalNeedyPeople=needyPeople.length; console.log("total needy people "+ totalNeedyPeople); // so each person will get var eachPersonMoney = eval(cashBox / totalNeedyPeople); console.log("each person will get "+ eachPersonMoney);
Learning: Finding array length property, how to use eval function and how to divide a number by another number.
Now we know how much each person will get, and the list of needy people, simply distribute among them.
// simple for loop example for(i=0;i<=needyPeople.length;i++) { console.log("Hello "+ needyPeople[i] + ", please accept " + eachPersonMoney + " Rs."); }
Learning: How use for loop to extract value from array using index number.
Already we have team variable, which has all team member name stored.
// using forEach loop, calling a function team.forEach(congratulateMember) function congratulateMember(member, index) { console.log("Congratulation "+ member); }
Learning: How to write a function and call from array foreach loop in JavaScript.
This simple exercise will help kids to develop their logical thinking and understanding the coding flow apart from following technical aspect.
Please share this tutorial with your friends and family, we develop more free tutorials for your kids, help us to help them better!
Trying following programming tasks