-1

I am a junior developer who has been coding for 4 weeks.

I'm working on a JavaScript method.

I'll show you the code I used first.


_.each = function (collection, iteratee) {
  if(Array.isArray(collection)===true){
    for(let i=0;i<collection.length;i++){
      iteratee(collection[i],i,collection)
    }
  }else{
  let objvalues= Object.values(collection)
  let objkeys = Object.keys(collection)
  for(let i=0;i<objvalues.length;i++){
    iteratee(objvalues[i],objkeys[i],collection)
  }
}
};
_.includes = function (arr, target) {
    let result
    _.each(arr, function(a){
      if(a === target) 
        result = true
      if (a !== target)
        result = false
      
    })
    return result;
};

It's a condition.

If the _.include method matches the value found by the element in the array, the true must be returned.

If the element in the array does not match the value you are looking for, you must return false.

I made the _include method.

If the element in the array does not match the value you are looking for, the return to false is successful.ten thousand

If the element in the array matches the value you are looking for, you must return true

This is where you fail.

It seems that the ture cannot be returned and only false is returned.

How should I handle this?

hyeon0416
  • 1
  • 1
  • How have you defined `_`? – trincot Sep 28 '20 at 14:15
  • 1
    have you tried using a [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems)? – Phu Ngo Sep 28 '20 at 14:16
  • my comment might be off but I suggest you to use JavaScript's array methods like includes() and forEach() instead of making your own implementation. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach – Antenaina Sep 28 '20 at 14:20

2 Answers2

1

The problem is here:

_.each(arr, function(a){
  if(a === target) 
    result = true
  if (a !== target)
    result = false
})

You reassign result on every iteration. As a result, the only iteration that matters for the final value of result is the last iteration.

Instead, initialize result to false, and reassign to true when the target is found:

const _ = {};
_.each = function(collection, iteratee) {
  if (Array.isArray(collection) === true) {
    for (let i = 0; i < collection.length; i++) {
      iteratee(collection[i], i, collection)
    }
  } else {
    let objvalues = Object.values(collection)
    let objkeys = Object.keys(collection)
    for (let i = 0; i < objvalues.length; i++) {
      iteratee(objvalues[i], objkeys[i], collection)
    }
  }
};
_.includes = function(arr, target) {
  let result = false;
  _.each(arr, function(a) {
    if (a === target)
      result = true
  })
  return result;
};

console.log(
  _.includes([1, 2, 3], 2)
);

It'd be cleaner to break the loop once a match is found, but your _each isn't set up for that:

const _ = {};
_.each = function(collection, iteratee) {
  if (Array.isArray(collection) === true) {
    for (let i = 0; i < collection.length; i++) {
      iteratee(collection[i], i, collection)
    }
  } else {
    let objvalues = Object.values(collection)
    let objkeys = Object.keys(collection)
    for (let i = 0; i < objvalues.length; i++) {
      iteratee(objvalues[i], objkeys[i], collection)
    }
  }
};
_.includes = function(arr, target) {
  for (const a of arr) {
    if (a === target)
      return true
  }
  return false;
};

console.log(
  _.includes([1, 2, 3], 2)
);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • Thank you very much. Thanks to you, I was able to solve the problem. I could see for sure where the problem was. – hyeon0416 Sep 28 '20 at 14:38
0

In addition to what CertainPerfomance already said, you could see how Array.includes was implemented to get an inspiration, here is the specifications from TC39.