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

145
Views
No se puede acceder a 'usagePrice' antes de la inicialización

Tengo un script que estoy usando para calcular el precio de un campo seleccionado

Cuando ejecuto la función 'calculateTotal()' aparece este error "No se puede acceder a 'usagePrice' antes de la inicialización".

Aquí está mi código:

 // Returns total for step 1 const usagePrice = () => { const field = document.querySelector('input[name="usage"]:checked'); const subField = document.querySelector('input[name="varifocal-lenses"]:checked'); let price = field.dataset.price; if (field.value == 'varifocal') { price = subField.dataset.price; } console.log('Usage price: ' + price); return price; } // Adds all totals together const calculateTotal = () => { const usagePrice = usagePrice(); const prescriptionPrice = prescriptionPrice(); const lensPackagePrice = lensPackagePrice(); const lensTypePrice = lensTypePrice(); const total = usagePrice + prescriptionPrice + lensPackagePrice + lensTypePrice; return total; } console.log(calculateTotal());

¿Alguien puede explicar qué está pasando mal? Gracias de antemano.

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

0

Resuelto ahora.

El problema era que estaba nombrando las variables en computeTotal() igual que las funciones declaradas antes.

 // Adds all totals together const calculateTotal = () => { const usagePriceValue = usagePrice(); const prescriptionPriceValue = prescriptionPrice(); const lensPackagePriceValue = lensPackagePrice(); const lensTypePriceValue = lensTypePrice(); const total = usagePriceValue + prescriptionPriceValue + lensPackagePriceValue + lensTypePriceValue; return total; }
about 4 years ago · Juan Pablo Isaza Report

0

Así como el nombre de la función calculateTotal comienza con un verbo ("calcular"), es muy común nombrar funciones con un verbo al principio. En su caso, tiene sentido comenzar los nombres de las funciones con el verbo "obtener":

 const usagePrice = getUsagePrice(); const prescriptionPrice = getPrescriptionPrice(); const lensPackagePrice = getLensPackagePrice(); const lensTypePrice = getLensTypePrice(); const total = usagePrice + prescriptionPrice + lensPackagePrice + lensTypePrice; return total;
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!