Skip to content

Commit 203d263

Browse files
committed
done
1 parent 2d03378 commit 203d263

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

ArgsPassed.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//!!!!!!!!! Write a function argumentsLength that returns the count of arguments passed to it.
2+
// Input: args = [5]
3+
// Output: 1
4+
// Explanation:
5+
// argumentsLength(5); // 1
6+
7+
// One value was passed to the function so it should return 1.
8+
9+
/**
10+
* @param {...(null|boolean|number|string|Array|Object)} args
11+
* @return {number}
12+
*/
13+
14+
15+
// program
16+
var argumentsLength = function(...args) {
17+
return args.length
18+
};

FnCall.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Given a function fn, return a new function that is identical to the original function except that it ensures fn is called at most once.
2+
3+
// The first time the returned function is called, it should return the same result as fn.
4+
// Every subsequent time it is called, it should return undefined.
5+
6+
7+
// Example 1:
8+
9+
// Input: fn = (a,b,c) => (a + b + c), calls = [[1,2,3],[2,3,6]]
10+
// Output: [{"calls":1,"value":6}]
11+
// Explanation:
12+
// const onceFn = once(fn);
13+
// onceFn(1, 2, 3); // 6
14+
// onceFn(2, 3, 6); // undefined, fn was not called
15+
16+
17+
/**
18+
* @param {Function} fn
19+
* @return {Function}
20+
*/
21+
// program
22+
23+
24+
var once = function (fn) {
25+
let flag = false;
26+
return function (...args) {
27+
if (flag === false) {
28+
flag = true
29+
return fn(...args)
30+
} else {
31+
return undefined
32+
}
33+
}
34+
};

FunctionComposition.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Given an array of functions [f1, f2, f3, ..., fn], return a new function fn that is the function composition of the array of functions.
2+
3+
// The function composition of [f(x), g(x), h(x)] is fn(x) = f(g(h(x))).
4+
5+
// The function composition of an empty list of functions is the identity function f(x) = x.
6+
7+
// You may assume each function in the array accepts one integer as input and returns one integer as output.
8+
9+
var compose = function(functions) {
10+
11+
return function(x) {
12+
let result =x
13+
for(let i=functions.length-1;i>=0;i--){
14+
result =functions[i](result)
15+
}
16+
return result
17+
}
18+
};

0 commit comments

Comments
 (0)