Skip to content

Instantly share code, notes, and snippets.

@DmitryOlkhovoi
Created May 3, 2020 10:50
Show Gist options
  • Save DmitryOlkhovoi/d4dcf3eb7193687abf3a6ef34df5633a to your computer and use it in GitHub Desktop.
Save DmitryOlkhovoi/d4dcf3eb7193687abf3a6ef34df5633a to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/mujivut
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
const random = require('./random');
class DNA {
constructor(length, dna = null) {
// Main
this.length = length;
this.genes = dna ? dna.genes : this.getRandomGenes(this.length);
// Additional
this.fitness = 0;
}
getRandomGenes(length) {
return new Array(length)
.fill(0)
.map(() => random.getRandomInt(32, 128));
}
updateLength(length) {
if (length <= this.genes) {
throw 'Error';
}
this.genes = [...this.genes, this.getRandomGenes(length - this.genes.length)];
}
}
module.exports = DNA;
</script>
<script id="jsbin-source-javascript" type="text/javascript">const random = require('./random');
class DNA {
constructor(length, dna = null) {
// Main
this.length = length;
this.genes = dna ? dna.genes : this.getRandomGenes(this.length);
// Additional
this.fitness = 0;
}
getRandomGenes(length) {
return new Array(length)
.fill(0)
.map(() => random.getRandomInt(32, 128));
}
updateLength(length) {
if (length <= this.genes) {
throw 'Error';
}
this.genes = [...this.genes, this.getRandomGenes(length - this.genes.length)];
}
}
module.exports = DNA;</script></body>
</html>
const random = require('./random');
class DNA {
constructor(length, dna = null) {
// Main
this.length = length;
this.genes = dna ? dna.genes : this.getRandomGenes(this.length);
// Additional
this.fitness = 0;
}
getRandomGenes(length) {
return new Array(length)
.fill(0)
.map(() => random.getRandomInt(32, 128));
}
updateLength(length) {
if (length <= this.genes) {
throw 'Error';
}
this.genes = [...this.genes, this.getRandomGenes(length - this.genes.length)];
}
}
module.exports = DNA;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment