Skip to content

Instantly share code, notes, and snippets.

@kamilogorek
Created January 13, 2015 01:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamilogorek/ab14da9f84de14452a62 to your computer and use it in GitHub Desktop.
Save kamilogorek/ab14da9f84de14452a62 to your computer and use it in GitHub Desktop.
arrays-article-code
function areAllValuesEqualOne (input) {
var flag = true;
for (var i = 0; i < input.length; i++) {
if (input[i] !== 1) flag = false;
}
return flag;
}
function areAllValuesEqualOne (input) {
return input.every(function (value, i) {
return value === 1;
});
}
function isEven (number) {
return number % 2 === 0;
}
[0, 1, 2, 3, 4, 5].filter(isEven); // [0, 2, 4]
function getEveryThirdValue (value, i) {
return (i + 1) % 3 === 0;
}
[0, 1, 2, 3, 4, 5].filter(getEveryThirdValue); // [2, 5]
function flatten (input) {
return input.reduce(function (acc, value, i) {
return acc.concat(Array.isArray(value) ? flatten(value) : value);
}, []);
}
console.log(flatten([1, 2, [3, 4,[5, 6], 7], 8, 9])); // [1, 2, 3, 4, 5, 6, 7, 8, 9];
var arr = [0, 1, 2, 3];
for (var i = 0, len = arr.length; i < len; i++) {
// perform some operation on arr[i];
}
[0, 1, 2, 3].forEach(function (value, i) {
// perform some operation on a value;
});
function getMaximumStringLength (list) {
return list.reduce(function (acc, value) {
return value.length > acc ? value.length : acc;
}, 0);
}
var foo = [0, 1, 2, 3];
var bar = foo.map(function (value, i) {
return value + 5;
});
console.log(bar); // [5, 6, 7, 8]
var getMoviesWithRatingOver2 = function (movies) {
return getMoviesWithRatingOverN.call(null, movies, 2);
};
var getMoviesWithRatingOver3 = function (movies) {
return getMoviesWithRatingOverN.call(null, movies, 3);
};
var getMoviesWithRatingOver4 = function (movies) {
return getMoviesWithRatingOverN.call(null, movies, 4);
};
function getMoviesWithRatingOverN (movies, n) {
return movies.filter(function (movie) {
return movie.rating > n;
});
}
var results = getMoviesWithRatingOverN([
{name: 'a', rating: 2 },
{name: 'b', rating: 3.5 },
{name: 'c', rating: 4.1 },
{name: 'd', rating: 5 },
{name: 'e', rating: 3.4 }
], 4);
console.log(results); // [{name: 'c', rating: 4.1 }, {name: 'd', rating: 5 }]
[1, 2, 3, 4, 5].reduce(function (acc, value, i) {
return acc + value;
});
// returns 15, as 1 became starting value
[1, 2, 3, 4, 5].reduce(function (acc, value, i) {
return acc + value;
}, 10);
// returns 25, as 10 was starting value
[['car', 'Chevrolet'], ['model', 'Camaro'], ['color', 'yellow']].reduce(function (acc, value, i) {
acc[value[0]] = value[1];
return acc;
}, { name: 'Bumblebee' });
// other data types can be used as an accumulator as well
// {name: 'Bumblebee', car: 'Chevrolet', model: 'Camaro', color: 'yellow'}
function doesAnyValueEqualsOne (input) {
return input.some(function (value, i) {
return value === 1;
});
}
_.map(myArray, function (value, i) { ... });
_.filter(myArray, function (value, i) { ... });
_.reduce(myArray, function (acc, value, i) { ... });
function isFormFilled (form) {
// get all inputs and selects and merge them into single array. Because querySelectorAll is returning nodeList, which has to be transformed into array to perform any array-based methods on it, we've to use slice method on our query results.
var slice = Array.prototype.slice; // just a quick shorthand
var inputs = slice.call(form.querySelectorAll('input')).concat(slice.call(form.querySelectorAll('select')));
return inputs.map(function (input) {
return input.value;
}).every(function (value) {
return value !== '' && value !== '0';
});
}
function getTotalVotesNumber (movies) {
return movies.map(function (movie) {
return parseInt(movie.votes, 10);
}).reduce(function (acc, vote) {
return acc + vote;
}, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment