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

150
Vistas
Cannot access 'usagePrice' before initialization

I have a script that I'm using to calculate the price of a selected field

When i run the function 'calculateTotal()' i get this error "Cannot access 'usagePrice' before initialization".

Here is my code:

// 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());

Can anyone explain whats going wrong? Thank you in advance.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Solved now.

The issue was i was naming the variables in calculateTotal() the same as the functions declared before.

// 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 Denunciar

0

Just like the name of the function calculateTotal starts with a verb ("calculate"), it is very common to name functions with a verb at the beginning. In your case, it makes sense to start function names with the "get" verb:

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 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