0

Given an array in JavaScript, I would like to select only the elements that are even. For example

array = [1,2,3,4,5,6,7,8,9,10]

It would return

[2,4,6,8,10]

Here is my code

array = [1,2,3,4,5,6,7,8,9,10]
for (i = 0; i < array.length; i++) {
  newArray = [];
  if (i[0] % 2 == 0 {
    newArray.push(i)
   return newArray;}}

=> Illegal return statement

I have no idea what I'm doing wrong, but any help would be appreciated. Thank you.

George Lucas
  • 197
  • 1
  • 8
  • Could you add all the method body? – tmarwen Jul 17 '14 at 14:15
  • You cannot have a return not in a function body, and you are adding the indexes, not the elements to newArray – kennebec Jul 17 '14 at 14:16
  • To tmarwen - That wasn't a method. But that was all the code I had. In JavaScript, I think you don't have to close loops(if for, etc.). I thought that logically that code would work but I guess not. – George Lucas Jul 17 '14 at 14:18
  • Matching up parentheses () and braces {} is very important in programming. Right now your return statement is inside the () of your `if`. And the only line inside your `for` is `newArray = [];`. – Russell Zahniser Jul 17 '14 at 14:18
  • @GeorgeLucas since there is no method, as kennebec said you can not add a return statement except in a method. – tmarwen Jul 17 '14 at 14:20

3 Answers3

3

Better you use native filter method. It will filter those elements which will match a criteria given in a callback.

var evens = array.filter(function(element){
               return element%2==0
            });
Mritunjay
  • 25,338
  • 7
  • 55
  • 68
  • Nice, it worked perfectly and is a lot easier than my method. I am trying to learn JavaScript from Ruby so it's a bit of a challenge. But your help is greatly appreciated. Thank you – George Lucas Jul 17 '14 at 14:16
  • 1
    [`Array.prototype.filter`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) is much more concise in my opinion, and would be my preference. But is must be noted that this is only available in ECMA5, though shims are available. Also, the native `for` loop has better performance accross all environments as it has to do less work, see spec http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.20 – Xotic750 Jul 17 '14 at 14:35
  • But I try native functions because when there are things done for me, why should I not use them. – Mritunjay Jul 17 '14 at 14:37
  • Yes, by all means use it, I was just pointing out some info for the OP. :) – Xotic750 Jul 17 '14 at 14:39
1

Your code corrected, you were getting close. Hopefully you can see where you were going wrong.

Javascript

var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    newArray = [],
    i;

for (i = 0; i < array.length; i++) {
    if (array[i] % 2 === 0) {
        newArray.push(array[i]);
    }
}

console.log(newArray);

On jsFiddle

Xotic750
  • 22,914
  • 8
  • 57
  • 79
  • I see. So, I needed to declare newArray OUTSIDE of the for loop instead of inside it. And also, instead of using the == operator I need to use the === operator to compare equality of both value and type. Out of curiosity though, why is that needed? Also is there a need to declare an i variable twice? Since we're already declaring it inside the for loop, setting i equal to 0. – George Lucas Jul 17 '14 at 14:25
  • In this case you could use `==` or `===`, but it is good practice to use `===`. There is good information here on SO about the different comparison methods. One such question http://stackoverflow.com/questions/359494/does-it-matter-which-equals-operator-vs-i-use-in-javascript-comparisons – Xotic750 Jul 17 '14 at 14:28
  • Other things to notice where the [`var`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var) keyword. and yes declaring the `newArray` outside is important otherwise each iteration would create the variable anew. – Xotic750 Jul 17 '14 at 14:30
  • I see, I also explored the var keyword. I have tried creating variables both with it and without it and it works both ways. Most likely, the convention is to use the var keyword. Is this true? – George Lucas Jul 17 '14 at 14:34
  • Without `var` the variable becomes a `global` variable, not good practice. this question may help you there http://stackoverflow.com/questions/1470488/what-is-the-function-of-the-var-keyword-in-ecmascript-262-3rd-edition-javascript – Xotic750 Jul 17 '14 at 14:37
  • Thank you, I understand now. Thanks for the link, I'll check it out. – George Lucas Jul 17 '14 at 14:40
1

Not as concise as previous answer but also can be used

var array = [1,2,3,4,5,6,7,8,9,10];

var evens = arr.reduce(function(prev, cur) {
             return (cur%2 === 0) ? prev.concat(cur) : prev; 
            }, []);
sergeyz
  • 1,339
  • 10
  • 14