Skip to content

Instantly share code, notes, and snippets.

@eriktrom
Created October 23, 2014 21:53
Show Gist options
  • Save eriktrom/cc5c56be2abf890ac880 to your computer and use it in GitHub Desktop.
Save eriktrom/cc5c56be2abf890ac880 to your computer and use it in GitHub Desktop.
Which version reveals itself as correct immediately? Which is most profitable to use consistently?
// Version 1
var numbers1 = [];
for (var i = 1; i <= 100; i++) {
numbers1.push(i);
}
console.log("numbers1", numbers1);
// Version 2
var numbers2 = [];
for (var i = 100; i > 0; i--) {
numbers2.push(i);
}
console.log("numbers2", numbers2);
// Version 3 - pragmatic programming
var _ = require('lodash');
var numbers3 = _.range(1, 101);
console.log("numbers3", numbers3);
// Version 4
var numbers4 = [];
for (var i = 1; i < 101; i++) {
numbers4.push(i);
}
console.log("numbers4", numbers4);
// Ignore this one, it's just meant to contrast w/ the others
// Version 5 - Example of obvious beauty via ruby
// numbers5 = (1..100).map { |num| num }
// puts "numbers5 #{numbers5}"
@kiwiupover
Copy link

I think you have to have library knowledge to get #3
What is a range is it does it return an array. I think in an app that uses alot of lodash #3 is the simplest but straight JS #1 is the cononical

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