Functions

Joined
Oct 5, 2022
Messages
2
Reaction score
0
I can't imagine how to solve this task, can you give me some guidance or an example


Write a function process(arr, f) where arr is an array argument and f() is a callback function that expects an array parameter.
process() returns the result of the function f() applied to the array.

Make calls to process() using different f() functions - for example:
- with a function calculating the sum of the elements of the parameter array


I've only do this but i didn't think im on a right track

let arg = [1,2,3]
function process(arr, f){
alert(f(arr));
}
 
Joined
Mar 11, 2022
Messages
226
Reaction score
32
Don't actually know what you need.

Something like this maybe?
Code:
function process(a,b){
        return b(a);
}
alert(process([20,30,50,10,'apple','peach'],function(s){
        var j=0;
    var str='';
    for(var i=0; i<s.length; i++){
        if(!isNaN(s[i])){j+=s[i]}else{str+=s[i]}
  
    }
    return j+' '+str;

}))

So yes, you are at the right track
 
Last edited:
Joined
Oct 5, 2022
Messages
2
Reaction score
0
Don't actually know what you need.

Something like this maybe?
Code:
function process(a,b){
        return b(a);
}
alert(process([20,30,50,10,'apple','peach'],function(s){
        var j=0;
    var str='';
    for(var i=0; i<s.length; i++){
        if(!isNaN(s[i])){j+=s[i]}else{str+=s[i]}
 
    }
    return j+' '+str;

}))

So yes, you are at the right track
Thanks i find the solution :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top