Javascript not reading array items beginning with 0 -
i want give genetic algorithms chance can't seem find solution problem.
this code:
var encodings = { 0000: 0, 0001: 1, 0010: 2, 0011: 3, 0100: 4, 0101: 5, 0110: 6, 0111: 7, 1000: 8, 1001: 9, 1010: "+", 1011: "-", 1100: "*", 1101: "/" }; var chromosome = ""; (var = 0; < 36; i++) { chromosome += math.round(math.random()); } var chromarray = chromosome.match(/.{1,4}/g); document.write(chromarray + "<br>"); (var o = 0; o < 9; o++) { document.write(encodings[chromarray[o]]); }
if run code, see there lot of undefineds in output. cause this?
thanks!
you should convert keys of object strings
it should be:
var encodings = { "0000": 0, "0001": 1, "0010": 2, "0011": 3, "0100": 4, "0101": 5, "0110": 6, "0111": 7, "1000": 8, "1001": 9, "1010": "+", "1011": "-", "1100": "*", "1101": "/" }; var chromosome = ""; (var = 0; < 36; i++) { chromosome += math.round(math.random()); } var chromarray = chromosome.match(/.{1,4}/g); document.write(chromarray + "<br>"); (var o = 0; o < 9; o++) { document.write(encodings[chromarray[o]]); }
Comments
Post a Comment