I'm trying to simulate a simple evolution thing using binary code. Each individual in the "gene pool" has their own unique string in binary code.
In order to have them mate, I need to take half of one string, and the other half of another and merge them. The problem is, I don't know how to split a string in two equal halves, and they are totally random.
Help would be appreciated, thanks!
Well I would get the total length of the string and get the first half by doing
myString.slice(firstIndex, secondIndex)
I would make the firstIndex be 0 and the secondIndex equal to myString.length / 2. That will return the first half of the string.
The second half of the string would be
myString.slice(myString.length / 2, myString.length)
So, all together:
const partOne = myString.slice(0, myString.length / 2)
const partTwo = myString.slice(myString.length / 2, myString.length)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With