Skip to content

Instantly share code, notes, and snippets.

@westc
Last active September 13, 2019 14:31
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 westc/b93e3d6df9070b4f5fee28a9331c626d to your computer and use it in GitHub Desktop.
Save westc/b93e3d6df9070b4f5fee28a9331c626d to your computer and use it in GitHub Desktop.
// Allows for an array or an array like structure to be stepped through in the order specified
// after each iteration.
function step(array, callback, opt_stepValue, opt_startIndex) {
opt_stepValue = ~~opt_stepValue || 1;
var count = array.length;
var i = (opt_startIndex == undefined || isNaN(~~opt_startIndex))
? opt_stepValue < 0
? count - 1
: 0
: opt_startIndex;
i = (~~i % count + count) % count;
for (var newStep, result = []; 0 <= i && i < array.length; i += opt_stepValue) {
newStep = callback(array[i], i, array);
result.push(array[i]);
opt_stepValue = newStep != undefined ? ~~newStep || 0 : opt_stepValue;
}
return result;
}
// CODE BREAK \\
step([1,2,3,4,5,6,7,8,9], function(value, index, array) {
if (index % 2 === 0) {
array[index] = String.fromCharCode(value + 64);
}
});
// CODE BREAK \\
step([1,2,3,4,5,6,7,8,9], function(value, index, array) {
if (index % 2 === 0) {
array[index] = String.fromCharCode(value + 64);
return 2;
}
});
// CODE BREAK \\
step([1,2,3,4,5,6,7,8,9], function(value, index, array) {
if (index % 2 === 0) {
array[index] = String.fromCharCode(value + 64);
}
}, -2, -1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment