Sum of the array in JavaScript


Hi, everyone, so today We are going to find out sum of the given array. So we want to create a program in Javascript? Which will find out sum of the given array. we can have array like this in Javascript. So this is the Javascript array, and we want to find out sum of this array. how we can find out? like we can have a Variable as s, initially we we can have s as 0 So we will traverse through the array with i index and we will Go to each Element, and we will add each element within s. we will start with our first index which is number 1 so we will come here and we will add with s so it will become 1 so Again, we will go to second number, and we will add here So it will become 3 now we will go to next Index and it will become 6 Again, we will go to next index so now we will add 4 With 6 so it will become 10 Now we will add 5, then it will become 15 so like this we can traverse whole array and we can add numbers so we will write a function to find out sum of the given array. we right function with function keyword then name of the function so we can have name as sum. and we have to provide a parameter which will accept argument as an array, and for find out some of the function We have to call this function also like sum() And we have to provide arguments, so argument will be only. [1,2,3,4,5] We can provide any number of elements in the array. So we have a variable called s. initially it will be zero. we will traverse through the whole array. We will try to add all the elements with s so we will use for loop and we will use I index for traversing through the array and we only traverse from first index to last index. so last index will be array.length-1 Like this we will traverse and while traversing at each index we will add each number with s. we will add all the elements with s. so Here we are adding all the elements in s after completing the for loop we will have Some of the all elements of array with s variable. so we can print this variable also with console.log we will print any variable or strings on the screen, so we will print s So now we will run this program See output, it is printing 15. So what is happening here ah? So at line number 11 we are calling Sum function, and we are passing argument as an array, so [1,2,3,4,5] it is your array and we are passing this as an argument. since we are calling this function So we will go to line number three so here function is defined with function Keyword and sum is function name. This is an array. array is a parameter here, and we are capturing this this array argument in this array parameter We have s variable. initially it will be zero then we are applying a for loop to traversing from each element of this given array and while traversing we are adding each element in s and After completing the for loop we will have sum of all the elements in the array in s variable and line number 8 we are printing s variable Okay instead of printing at line number 8. we can return sum of the array also. So how we can improve this program? now we will try to return in this value. So how we can return anything from a function So we have to use return Keyword and we will return this sum of the array and right now what will happen at line number 11 whenever we call this function it will go to line number 3 and It will process With the function body and at line number 8 it will return 15 so here whenever function will Come again at line number 11 after processing this function, so it will have 15 value So we have to capture this 15 value in some other variable So we have to use another variable like we can use var So with var keyword we defined variable in Javascript, so we can have returned_val as variable name. so returnedVal will have 15. so we can print returnedVal here console.log then returnedVal Now we will run this program see It is printing 15 So what we are doing here, at line number 11 we are calling this function we are passing this array as an argument and in the function we are Traversing through the whole area and we are returning sum of the whole elements we are capturing this sum in return value variable and line number 12 We are printing this returned value so how we can Make this program much better Like there is no need to define another variable returnedVal At the end we are printing this returned value. so we can directly use console.log there itself we will call this function from the console.log only we will call this function inside console.log only And here we will call this function Now we will run this program, and we will see the output So it is printing 15 here

5 Comments

Add a Comment

Your email address will not be published. Required fields are marked *