Permanently store randomly generated values

I’m doing the Mysterious Organism challenge whereby at the end I must populate an array with randomly generated DNA suquences.
I’ve succesfully completed the requirements however each time I run the program it generates a new array of randomly generated DNA. I’m wondering if it’s possible to permenantly store the same values of the array even after re-running the program? I hope this makes sense.

let uniqueId = () => { return Math.floor(Math.random() * 10000); }; // Returns a random DNA base 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; }; function pAequorFactory(specID, sequence) { this.specimenNum = specID; this.dna = sequence; this.willLikelySurvive = function () { let subArr = this.dna.filter((base) => base == "C" || base == "G").length; let survivability = Math.floor((subArr / 15) * 100); return survivability >= 60; }; this.mutate = function () { let dnaBases = ["A", "T", "C", "G"]; let randomBaseIndex = Math.floor(Math.random() * 15); altDnaBases = dnaBases.filter((base) => base !== this.dna[randomBaseIndex]); this.dna[randomBaseIndex] = `*${ altDnaBases[Math.floor(Math.random() * 3)] }`; return this.dna; }; } pAequorFactory.prototype.compareDNA = function (obj) { let commonDna = 0; for (let i = 0; i < this.dna.length; i++) { if (this.dna[i] === obj.dna[i]) { commonDna++; } } commonDna = Math.floor((commonDna / 15) * 100); console.log( `Specimen #1 and specimen #2 share ${commonDna}% of DNA in common.` ); }; function collection() { let dnaCollection = []; let idCounter = 1; while (dnaCollection.length < 30) { let newDna = new pAequorFactory(uniqueId(), mockUpStrand()); if (newDna.willLikelySurvive()) { dnaCollection.push(newDna); } idCounter++; } return dnaCollection; } const sampleCollection = collection(); console.log(sampleCollection);

To store the data between runs of the program, you’d need a database. To store the data while the program is running, you could use an array.

2 Likes

You could also try the localStorage.
An easy way to store data permanently on your computer without a database. It’s a bit like a cookie – but for client-side reading.

1 Like

Ahh ok. I was thinking I’d probably need a database but I was just curious. Thank you very much.

Oh great! I’ll give that a try and see how it works out. Thanks again for your help mirja_t👍

1 Like