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.

5
  • Could you add all the method body? Commented Jul 17, 2014 at 14:15
  • You cannot have a return not in a function body, and you are adding the indexes, not the elements to newArray Commented Jul 17, 2014 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. Commented Jul 17, 2014 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 = [];. Commented Jul 17, 2014 at 14:18
  • @GeorgeLucas since there is no method, as kennebec said you can not add a return statement except in a method. Commented Jul 17, 2014 at 14:20

3 Answers 3

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
            });
Sign up to request clarification or add additional context in comments.

4 Comments

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
Array.prototype.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 ecma-international.org/ecma-262/5.1/#sec-15.4.4.20
But I try native functions because when there are things done for me, why should I not use them.
Yes, by all means use it, I was just pointing out some info for the OP. :)
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

6 Comments

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.
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 stackoverflow.com/questions/359494/…
Other things to notice where the var keyword. and yes declaring the newArray outside is important otherwise each iteration would create the variable anew.
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?
Without var the variable becomes a global variable, not good practice. this question may help you there stackoverflow.com/questions/1470488/…
|
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; 
            }, []);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.