+ 1

pls tell me how the code work, especially this sentence: val=(arg[i]).call(window,val);🧐🧐🧐array looks like a function here

function pipe(...arg){ let len=arg.length; let val=arg[0]; for(let i=1;i<len;i++){ val=(arg[i]).call(window,val); return val; }} pipe("5a0",parseInt,console.log);

23rd Jul 2022, 1:32 PM
Luise Chen
Luise Chen - avatar
4 Answers
+ 4
The pipe function takes multiple arguments, the first one is supposed to be a value, and the rest should be functions which process the value in turn, and pass the result to the next function. arg is an array (because of the ... spread operator all argument of pipe are stored in this array). The for loop goes through the function arguments (starting from index 1). arg[i].call(window, val) Here the function is invoked. Its first parameter "window" is the context where the function is executed, then val is the argument. so function call for the first loop iteration will translate to this: window.parseInt("5a0") https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call
23rd Jul 2022, 1:47 PM
Tibor Santa
Tibor Santa - avatar
+ 3
Just letting you know, the for...loop.was missing a closing curly bracket for its body, after the line where <val> was assigned new value. Also you have a typo in `parseInt`, you should be using uppercase letter i not lowercase letter L parseInt -> 'parse' string into 'Int'eger
23rd Jul 2022, 1:57 PM
Ipang
+ 2
Tibor Santa really thanksđŸ‘đŸ»
23rd Jul 2022, 2:08 PM
Luise Chen
Luise Chen - avatar
+ 1
Ipang thanks for correcting😊
23rd Jul 2022, 2:16 PM
Luise Chen
Luise Chen - avatar