0

I have a iterateThroughElements function that accepts an array or an object as the first argument. There is an optional function as the second argument.

If the user did not pass in the function second parameter, it will just iterate through the element and console log all of the elements.

If the user pass in the function it will do the exactly what the function does.

Here's my codes:

function filterArgReturnEvenElements(el) {
  return (typeof el === 'number') && !(el % 2);
}

function returnKeyWithArrayValue(arg, key){
  return arg[key] instanceof Array;
}


function iterateThroughElements(arg, myFunc){
  // if arg is an Array
  if (arg instanceof Array) {
   // if function exist then call specific function or callback
    if(myFunc && typeof(myFunc) == "function"){
      return arg.filter(myFunc);
    // else if function doesnt exist, iterate through array and console.log all the items
    }else{
       arg.forEach(item => console.log(item));
    }
  } 
    // if arg is an Object
    else if(arg instanceof Object) {
   // if function exist then call specific function or callback
    if(myFunc&& typeof(myFunc) == "function"){
    return Object.keys(arg).filter(key => myFunc(arg, key));
      // else if function doesnt exist, iterate through object and console.log all the items
    }else{
      for(key in arg){
      if(arg.hasOwnProperty(key)){
        console.log(key + " : " + arg[key]);
      }
    }
    }
}

let arr = [1, 2, 3, 4, 5, 7];
let obj = {
  firstname: 'mik',
  lastname: 'lasner',
  age: 21,
  favorite: ['hiking', 'surfing', 'beach']
};

let output = iterateThroughElements(arr, filterArgReturnEvenElements);
console.log(output);

let output2 = iterateThroughElements(obj, returnKeyWithArrayValue);
console.log(output2);

let output3 = iterateThroughElements(arr);
console.log(output);

let output4 = iterateThroughElements(obj);
console.log(output2);

As you can see above the filterArgReturnEvenElements function will return the items on the array that are even numbers.

While the returnKeyWithArrayValue function will return the key in an object that has an array value.

Right now my codes doesn't work. Where do you think did I go wrong? Please help!

IS THERE A BETTER WAY TO DO THIS?

2
  • "Right now my codes doesn't work" - this is a useless statement without more clarification. What specifically doesn't work? Please describe the desired vs. obtained result. The only error I see is let output4 = ...; console.log(output2) (i.e. you never actually display output4, which is undefined anyway). Commented Aug 7, 2017 at 2:12
  • Which one of your four output is incorrect? What is the expected output and what are you getting now? Commented Aug 7, 2017 at 2:14

1 Answer 1

1

Hi one bracket was missed and also callBack variable is not defined.

function filterArgReturnEvenElements(el) {
  return (typeof el === 'number') && !(el % 2);
}

function returnKeyWithArrayValue(arg, key){
  return arg[key] instanceof Array;
}


function iterateThroughElements(arg, myFunc){
  // if arg is an Array
  if (arg instanceof Array) {
    console.log("its an array");
   // if function exist then call specific callback
    if(myFunc && typeof(myFunc) == "function"){
      return arg.filter(myFunc);
    // else if function doesnt exist, iterate through array and console.log all the items
    }else{
       arg.forEach(item => console.log(item));
    }
  } 
    // if arg is an Object
    else if(arg instanceof Object) {
    // if function exist then call specific callback
    if(myFunc && typeof(myFunc) == "function"){
    return Object.keys(arg).filter(key => myFunc(arg, key));
      // else if function doesnt exist, iterate through object and console.log all the items
    }else{
      for(key in arg){
      if(arg.hasOwnProperty(key)){
        console.log(key + " : " + arg[key]);
      }
    }
    }
}
}
let arr = [1, 2, 3, 4, 5, 7];
let obj = {
  firstname: 'mik',
  lastname: 'lasner',
  age: 21,
  favorite: ['hiking', 'surfing', 'beach']
};
console.log("it works");
let output = iterateThroughElements(arr, filterArgReturnEvenElements);
console.log(output);

let output2 = iterateThroughElements(obj, returnKeyWithArrayValue);
console.log(output2);

let output3 = iterateThroughElements(arr);
console.log(output);

let output4 = iterateThroughElements(obj);
console.log(output2);
Sign up to request clarification or add additional context in comments.

3 Comments

I think this one is the best answer. Is there a better way to do this? or using if statements is fine?
@james This looks fine, in my opinion, since we need the "if" to distinguish between array and object.
@JamesHedegon can this issue be closed?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.