My edit of the Mysterious Organism Challenge.
I’ve added control to the objects. It felt too soon to just stop at nothing.
Any tips or flaws ? Lmk !
COPY/PAST this into a file.js (I advise repl.it)
const prompt = require(‘prompt-sync’)({sigint: true});
// Returns a random DNA base
const dnaBases = [‘A’, ‘T’, ‘C’, ‘G’]
const returnRandBase = () => {
const dnaBases = [‘A’, ‘T’, ‘C’, ‘G’]
return dnaBases[Math.floor(Math.random() * 4)]
}
// Returns a random single strand of DNA containing 15 bases
const mockUpStrand = () => {
const newStrand =
for (let i = 0; i < 15; i++) {
newStrand.push(returnRandBase())
}
return newStrand
}
// specimenNum = ID number
// dna = array of dna-strand
function pAequorFactory(specimenNum, dna) {
const organism = {
specimenNum,
dna,
mutate() {
// Get random base (A,T,C,G)
const rnd = Math.floor(Math.random() * 14)
const rndBase = this.dna[rnd]
// This will be used to get new base value
let newBase
do {
// Generate new random base (A,T,C,G)
rndNew = Math.floor(Math.random() * 3)
// Assign new base to newBase based on rndNew
newBase = dnaBases[rndNew]
// Keep doing this until rndBase !== newBase
} while (rndBase === newBase)
// Change the value of the old base to the new one
this.dna[rnd] = newBase
},
compareDNA(objSample) {
// Counter for identical DNA
let counter = 0
// Save this dna to const
const objThis = this.dna
// Compare dna of objThis with objSample
// Check every position in array for identical DNA
for (i = 0; i < 14; i++) {
if (objThis[i] === objSample.dna[i]) {
counter++
}
}
// Declare names
const thisId = this.specimenNum
const sampleId = objSample.specimenNum
// Calculate percentage of identicals
const percent = counter / 15 * 100
// Log message
return (specimen #${thisId} and specimen #${sampleId} have ${percent}% DNA in common.
)
},
willLikelySurvive() {
// Percent of chance to survive (calc at end)
let percent = 0
// Counter for ‘C’ and ‘G’ bases
let counter = 0
// Save this dna to const
const objThis = this.dna
// Compare dna of objThis with objSample
// Check every position in array for identical DNA
for (i = 0; i < 15; i++) {
if (objThis[i] === ‘C’ || objThis[i] === ‘G’) {
counter++
}
}
percent = counter / 15 * 100
return (percent >= 60)
}
}
// return object
return organism
}
// Create 30 instances
const samples =
for (i = 0; i <= 99; i++) {
samples[i] = new pAequorFactory(i, mockUpStrand())
}
// Proof that pAequorFactory works
// Make object examples
//myExample1 = pAequorFactory(0, mockUpStrand())
//myExample2 = pAequorFactory(1, mockUpStrand())
// Log example samples
// console.log('ex1 = ’ + myExample1)
// console.log('ex2 = ’ + myExample2)
// Proof that .mutate() works
/*console.log('before: ’ + myExample1.dna)
myExample1.mutate()
console.log(after: ’ + myExample1.dna)
*/
// Proof that .compareDNA() works
/*console.log('ex1 = ’ + myExample1.dna)
console.log('ex2 = ’ + myExample2.dna)
myExample1.compareDNA(myExample2)
*/
// Proof that .willLikelySurvive() works
//console.log('ex1 = ’ + myExample1.dna)
//console.log('Will survive: ’ + myExample1.willLikelySurvive())
// Proof that instances are made
/*console.log(samples[0].dna)
console.log(samples[25].compareDNA(samples[6]))
console.log(samples[15].willLikelySurvive())
console.log()
*/
// Intro message
message1()
// Receive Input
let action = ‘’
while (action !== ‘QUIT’) {
message2()
}
function processInput() {
// Process input
if (action === ‘log’) {
// Call log input
action = prompt('Sample Number: ')
whiteSpace()
logSample(action)
}
else if (action === ‘mutate’) {
action = prompt('Sample Number: ')
whiteSpace()
mutateSample(action)
}
else if (action === ‘survive’) {
action = prompt('Sample Number: ')
whiteSpace()
surviveSample(action)
}
else if (action === ‘compare’) {
action = prompt('Sample Number 1: ')
let action2
action2 = prompt('Sample Number 2: ')
whiteSpace()
compareSamples(action, action2)
}
else if (action === ‘help’) {
whiteSpace()
logCommands()
}
else {console.log(‘Try again.’)}
// clear
whiteSpace()
}
function logCommands() {
// Log list of commands
console.log(‘log : logs a DNA sample’)
console.log(‘help : a list of commands’)
console.log(‘mutate : mutates a DNA sample’)
console.log(‘survive : logs high or low chance’)
console.log(‘compare : compares a DNA sample to another’)
console.log(‘QUIT : exits program’)
}
function logSample(sampleNumber) {
// Log sample
console.log(samples[sampleNumber])
}
function mutateSample(sampleNumber) {
// Set sample in variable
let dnaStrand = samples[sampleNumber]
// Mutate sample
dnaStrand.mutate()
// Log sample
console.log(‘Mutated Sample:’)
console.log(dnaStrand)
}
function surviveSample(sampleNumber) {
// Set result of function in variable
let survivalChance = samples[sampleNumber].willLikelySurvive()
// Log true or false
console.log('Likely to survive: ’ + survivalChance)
}
function compareSamples(sampleNumber1, sampleNumber2) {
// Set samples in variable
let sample1 = samples[sampleNumber1]
let sample2 = samples[sampleNumber2]
// Log results
console.log(sample1.compareDNA(sample2))
}
function message1() {
console.log(“A mysterious new species has been discovered!”)
console.log(“We can inspect, compare and mutate their DNA.”)
console.log(“Dependant on their DNA they have a chance to survive.”)
console.log(’’)
console.log(“We have created 100 examples of the species.”)
console.log(‘What would you like to do with them?’)
}
function message2() {
console.log(’’)
console.log(‘Type help for a list of actions.’)
console.log(’’)
action = prompt('Action: ')
processInput()
}
function whiteSpace() {
console.log()
console.log()
console.log()
console.log()
console.log()
console.log()
console.log()
console.log()
}