# My Edit of Mysterious Organism (JavaScript Object Challenge)

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.dna)
console.log(samples.compareDNA(samples))
console.log(samples.willLikelySurvive())
console.log()
*/

// Intro message
message1()

let action = ‘’
while (action !== ‘QUIT’) {
message2()
}

function processInput() {
// Process input
if (action === ‘log’) {
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()
}

Hi Jan, welcome to the forums Would you mind formatting your code so it is easier to read for potential helpers? Thank you! Here’s a post on how to format your code: [How to] Format code in posts
{"ft_lang_label":"__label__en","ft_lang_prob":0.5053346,"math_prob":0.9654548,"size":5657,"snap":"2020-45-2020-50","text_gpt3_token_len":1601,"char_repetition_ratio":0.18344241,"word_repetition_ratio":0.12607099,"special_character_ratio":0.30722997,"punctuation_ratio":0.15359116,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9660206,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-10-25T18:57:15Z\",\"WARC-Record-ID\":\"<urn:uuid:5882f1a5-9cc9-43d9-aff6-fa1bbd209a1b>\",\"Content-Length\":\"24080\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:4b093abf-6d8e-46d5-8c42-bbe315d34d27>\",\"WARC-Concurrent-To\":\"<urn:uuid:4e8d0d82-cad0-4a74-a434-bc4adaf1c051>\",\"WARC-IP-Address\":\"216.218.159.29\",\"WARC-Target-URI\":\"https://discuss.codecademy.com/t/my-edit-of-mysterious-organism-javascript-object-challenge/539831\",\"WARC-Payload-Digest\":\"sha1:HN2S6SGJKGIALFC5H766EST7RH7VPDK5\",\"WARC-Block-Digest\":\"sha1:PVBMK7SVSEZLBWGJ4FW7YN2VQWUYEP3T\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-45/CC-MAIN-2020-45_segments_1603107889651.52_warc_CC-MAIN-20201025183844-20201025213844-00372.warc.gz\"}"}