Last active
November 16, 2015 18:28
-
-
Save superherointj/11d1451a5123b2ca82a3 to your computer and use it in GitHub Desktop.
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
// 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? | |
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 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