-
-
Save kamilogorek/ab14da9f84de14452a62 to your computer and use it in GitHub Desktop.
arrays-article-code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function areAllValuesEqualOne (input) { | |
var flag = true; | |
for (var i = 0; i < input.length; i++) { | |
if (input[i] !== 1) flag = false; | |
} | |
return flag; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function areAllValuesEqualOne (input) { | |
return input.every(function (value, i) { | |
return value === 1; | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var arr = [0, 1, 2, 3]; | |
for (var i = 0, len = arr.length; i < len; i++) { | |
// perform some operation on arr[i]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[0, 1, 2, 3].forEach(function (value, i) { | |
// perform some operation on a value; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getMaximumStringLength (list) { | |
return list.reduce(function (acc, value) { | |
return value.length > acc ? value.length : acc; | |
}, 0); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var foo = [0, 1, 2, 3]; | |
var bar = foo.map(function (value, i) { | |
return value + 5; | |
}); | |
console.log(bar); // [5, 6, 7, 8] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function doesAnyValueEqualsOne (input) { | |
return input.some(function (value, i) { | |
return value === 1; | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_.map(myArray, function (value, i) { ... }); | |
_.filter(myArray, function (value, i) { ... }); | |
_.reduce(myArray, function (acc, value, i) { ... }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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