Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

251
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda