Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

249
Views
Javascript: Object method - why do I need parentheses?

I am learning javascript and Ive stumbled upon issue that I do not understand. Could somebody explain to me why in method compareDNA I need to use parentheses while using this.dna and in the previous method it works just fine?

// Returns a random DNA base
const returnRandBase = () => {
  const dnaBases = ['A', 'T', 'C', 'G'];
  return dnaBases[Math.floor(Math.random() * 4)];
};

// Returns a random single stand of DNA containing 15 bases
const mockUpStrand = () => {
  const newStrand = [];
  for (let i = 0; i < 15; i++) {
    newStrand.push(returnRandBase());
  }
  return newStrand;
};

function pAequorFactory(specimenNum, dna){
  return {
    specimenNum,
    dna,
    mutate(){
      let i = Math.floor(Math.random() * 15)
      let newGene = returnRandBase()
      while (this.dna[i] === newGene){
        newGene = returnRandBase()
      }
      this.dna[i] = newGene
      return this.dna
    },

    compareDNA(object){
      let counter = 0
      for(let i = 0; i < this.dna().length; i++){
        if(this.dna()[i] === object.dna()[i]){
          counter++
        }
      }
      let percentage = counter / this.dna().length * 100
      return `Specimen number ${this.specimenNum} and specimen number ${object.specimenNum} have ${percentage}% of DNA in common.`
      },
    }
  }

let aligator = pAequorFactory(1, mockUpStrand)
let dog = pAequorFactory(2, mockUpStrand)
console.log(aligator.compareDNA(dog))

console.log(dog.dna().length)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The problem is that the dna that is passed as an argument is a function, so it becomes a method of the returned object, and needs to be called with .dna(). However, this looks like a mistake - actually an array should have been passed:

let aligator = pAequorFactory(1, mockUpStrand())
//                                           ^^
let dog = pAequorFactory(2, mockUpStrand())
//                                      ^^

Then you can access .dna[i] or .dna.length as normal.

If you don't do that, dog.dna() returns a different DNA every time, which doesn't make sense.

using this.dna and in the previous method it works just fine?

Actually, it doesn't. dog.mutate() does return a function with a single integer property. It's supposed to return an array really.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!