Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Counting elements of an array using a recursive function in JS?
The recursive function calls itself with some base condition. Let’s say the following is our array with marks −
var listOfMarks=[56,78,90,94,91,82,77];
Following is the code to get the count of array elements −
Example
function countNumberOfElementsUsingRecursive(listOfMarks) {
if (listOfMarks.length == 0) {
return 0;
}
return 1 +
countNumberOfElementsUsingRecursive(listOfMarks.slice(1));
}
var listOfMarks=[56,78,90,94,91,82,77];
console.log("The array=");
console.log(listOfMarks);
var numberOfElements=countNumberOfElementsUsingRecursive(listOfMarks);
console.log("The Number of elements = "+numberOfElements);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo110.js.
Output
This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo110.js The array=[ 56, 78, 90, 94, 91, 82, 77 ] The Number of elements = 7
Advertisements
