Skip to content

Instantly share code, notes, and snippets.

@iambowen
Last active December 21, 2015 22:38
Show Gist options
  • Save iambowen/6376281 to your computer and use it in GitHub Desktop.
Save iambowen/6376281 to your computer and use it in GitHub Desktop.
Node js implementation
@XuefengWu
Copy link

function range(start, end) {
    var foo = [];
    for (var i = start; i <= end; i++) {
        foo.push(i);
    }
    return foo;
}

var list = range(1, 10);

function isEven(num) {
    return num % 2 == 0
}

function devide(num) {
    return num / 2
}
list.filter(isEven).map(devide).forEach(console.log;})

@zhywang
Copy link

zhywang commented Aug 29, 2013

#!/usr/bin/env node

function printEven(num) {
    if((num%2) == 0)
        console.log(num/2);
}

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

array.forEach(printEven)

@nihaokid
Copy link

Array.range= function(a, b, step){
    var A= [];
    if(typeof a== 'number'){
        A[0]= a;
        step= step || 1;
        while(a+step<= b){
            A[A.length]= a+= step;
        }
    }
    else{
        var s= 'abcdefghijklmnopqrstuvwxyz';
        if(a=== a.toUpperCase()){
            b=b.toUpperCase();
            s= s.toUpperCase();
        }
        s= s.substring(s.indexOf(a), s.indexOf(b)+ 1);
        A= s.split('');        
    }
    return A;
}

var numbers = Array.range(1,10);
var result = numbers.filter(function(number){
    return number%2 == 0
})

for (var i in result){
    console.log(result[i]/2);
}

@johnelf
Copy link

johnelf commented Aug 29, 2013

[1,2,3,4,5,6,7,8,9,10].filter(even_number).map(divide2)

function even_number(element){
 return element % 2 == 0
}

function divide2(element){
 return element / 2;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment