Skip to content

Instantly share code, notes, and snippets.

@superherointj
Last active November 16, 2015 18:28
Show Gist options
  • Save superherointj/11d1451a5123b2ca82a3 to your computer and use it in GitHub Desktop.
Save superherointj/11d1451a5123b2ca82a3 to your computer and use it in GitHub Desktop.
// Hello. I'm learning. Thank you for sharing your insights.
var sUtil = require('sUtils.js');
var obj = {
a:[1,2],
b:[3,4],
}
// (1) Check if is array. If it is array, then apply function to it's value.
// Imperative
if (Array.isArray(obj.a)) { = sUtil.bitmaskArrayToDecimal(obj.a); }
if (Array.isArray(obj.b)) { = sUtil.bitmaskArrayToDecimal(obj.b); }
// Functional style BUT it's wrong and fails to work :(
[ obj.a, obj.b ].forEach(function(o){
if (Array.isArray(o)) {
o = sUtil.bitmaskArrayToDecimal(o); // The value doesn't get attributed to source 'obj'.
}
});
// If I wanted a new array i'd use .map() but what I need is to change previous data. "o =" doesn't cut it!
// How can I fix the more 'functional' code?
var bitmaskArrayToDecimal = export = function (arr) {
var result = 0;
for (var i = 0; i < arr.length; i++) {
result = result | (1 << arr[i] - 1);
};
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment