Question: I have a question regarding this snippet of code. 

```js
var names = ["John", "Jen", "Tony"];

var obj = {
 prob: function () {
  for (var i = 0; i < names.length; i++) {
   document.write(names[i]);
   console.log(names[i]);
  }
 },
 trim: names.forEach(function(name) {
  document.write(name)
  console.log(name)
 })
}

console.log("*********")
console.log(obj.prob());
console.log("*********")
```

If I run this code in my console I will get this:

```
John
Jen
Tony
*********
John
Jen
Tony
undefined
*********

```

That means that before my prob function that I call my trim function runs. Why is that? I didn't call it? Can I save it as a method on object and call it later when I need it?

Answer: 
The `names.forEach` is called trying to assign the return value to `trim`. Putting it inside a function should work.

```js
var names = ["John", "Jen", "Tony"];

var obj = {
 prob: function () {
  for (var i = 0; i < names.length; i++) {
   document.write(names[i]);
   console.log(names[i]);
  }
 },
 trim: function () {names.forEach(function(name) {
  document.write(name)
  console.log(name)
 })}
}

console.log("*********")
console.log(obj.prob());
console.log("*********")
console.log(obj.trim());
```
You just have to wrap your `each` function inside `function`

```js
var names = ["John", "Jen", "Tony"];

var obj = {
 prob: function () {
  for (var i = 0; i < names.length; i++) {
   document.write(names[i]);
   console.log(names[i]);
  }
 },
 trim: function(){ names.forEach(function(name) {
        alert();
  document.write(name)
  console.log(name)
 })}
}

console.log("*********")
console.log(obj.prob());
console.log("*********")
```