js__coffeescript__codeschool/lib/4-1-arrays.js

71 lines
1.9 KiB
JavaScript

var geoLocate, i, index, j, len, len1, loc, location, storeLocations1, storeLocations2, storeLocations3;
storeLocations1 = ['Orlando', 'Winter Park', 'Sanford'];
// Beware, this doesn't compile correctly:
// storeLocations.forEach(location, index) ->
// It uses it as a forEach(location, index) call with a () => {} callback
// So be sure to add the space.
storeLocations1.forEach(function(location, index) {
return console.log(`Location ${index}: ${location}`);
});
// Or use something more idiomatic:
for (index = i = 0, len = storeLocations1.length; i < len; index = ++i) {
location = storeLocations1[index];
console.log(`Location ${index}: ${location}`);
}
for (index = j = 0, len1 = storeLocations1.length; j < len1; index = ++j) {
location = storeLocations1[index];
// List comprehensions
console.log(`Location ${index}: ${location}`);
}
// In this case, the parentheses are essential
storeLocations2 = (function() {
var k, len2, results;
results = [];
for (k = 0, len2 = storeLocations1.length; k < len2; k++) {
loc = storeLocations1[k];
results.push(`${loc}, FL`);
}
return results;
})();
// Without them, the whole "storeLocations = ..." part is run for each loc.
console.log(storeLocations2);
geoLocate = function(loc) {
return `${loc}, USA`;
};
// Applying a filter to a list comprehension
console.log((function() {
var k, len2, results;
results = [];
for (k = 0, len2 = storeLocations1.length; k < len2; k++) {
loc = storeLocations1[k];
if (loc !== 'Sanford') {
results.push(geoLocate(loc));
}
}
return results;
})());
storeLocations3 = (function() {
var k, len2, results;
results = [];
for (k = 0, len2 = storeLocations1.length; k < len2; k++) {
loc = storeLocations1[k];
if (loc !== 'Sanford') {
results.push(loc);
}
}
return results;
})();
console.log(storeLocations3);
//# sourceMappingURL=4-1-arrays.js.map