File tree Expand file tree Collapse file tree 1 file changed +68
-0
lines changed Expand file tree Collapse file tree 1 file changed +68
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Question Link: https://leetcode.com/problems/return-length-of-arguments-passed/?envType=study-plan-v2&envId=30-days-of-javascript
2
+ // Solution Link: https://leetcode.com/problems/return-length-of-arguments-passed/solutions/5434940/3-javascript-easy-solution-using-one-line-for-loop-and-foreach-loop/
3
+
4
+ /*
5
+ 2703. Return Length of Arguments Passed
6
+
7
+ Write a function argumentsLength that returns the count of arguments passed to it.
8
+
9
+ Example 1:
10
+ Input: args = [5]
11
+ Output: 1
12
+ Explanation:
13
+ argumentsLength(5); // 1
14
+ One value was passed to the function so it should return 1.
15
+
16
+ Example 2:
17
+ Input: args = [{}, null, "3"]
18
+ Output: 3
19
+ Explanation:
20
+ argumentsLength({}, null, "3"); // 3
21
+ Three values were passed to the function so it should return 3.
22
+
23
+ Constraints:
24
+ args is a valid JSON array
25
+ 0 <= args.length <= 100
26
+ */
27
+
28
+
29
+ /**
30
+ * @param {...(null|boolean|number|string|Array|Object) } args
31
+ * @return {number }
32
+ */
33
+
34
+ /*
35
+ // Approach 1: Using for Loop - TC = O(n), SC = O(1)
36
+ var argumentsLength = function(...args) {
37
+ let count = 0;
38
+
39
+ for (let i = 0; i < args.length; i++) {
40
+ count++;
41
+ }
42
+
43
+ return count;
44
+ };
45
+ */
46
+
47
+ // Approach 2: Using forEach Loop - TC = O(n), SC = O(1)
48
+ var argumentsLength = function ( ...args ) {
49
+ let count = 0 ;
50
+
51
+ args . forEach ( ( num ) => {
52
+ count ++ ;
53
+ } ) ;
54
+
55
+ return count ;
56
+ } ;
57
+
58
+
59
+ /*
60
+ // Approach 3: Using Predefined `length` Method - TC = O(n), SC = O(1)
61
+ var argumentsLength = function(...args) {
62
+ return args.length;
63
+ };
64
+ */
65
+
66
+ /**
67
+ * argumentsLength(1, 2, 3); // 3
68
+ */
You can’t perform that action at this time.
0 commit comments