Question:
I have this exercise: // invoice information /* the invoices have the name of the company, telephone address, NIF, customer name, address, telephone number and identification number, element, description, price, quantity, total amount, type of VAT, payment method
add method that calculates total amount and updates corresp value. and another method that shows the total amount on the screen */
var facturitas = factura = {
nombre: 'gordonas',
direccion: 'el hatillo',
telefono: 2129633629,
nif: '12jd-3jKu',
producto: 'dona',
descripcion: 'dona rellena de chocolate mas chispitas de chocolate',
precio: 1.23,
cantidad: 2,
importeTotal: importeTotal() ,
tipoIva: 1.21 ,
formaPago: 'tarjeta'
}
function importeTotal(){
var importeTotal = (facturitas.precio * 1) / facturitas.tipoIva
importeTotal = facturitas.importeTotal
}
function mostrarImporteTotal(){
return `el importe total es: ${facturitas.importeTotal} `
}
mostrarImporteTotal()
When I run the code I get the error 'Cannot read properties of undefined (reading 'price'') in the line var total amount = (invoices.price * 1) / invoices.Ivattype and the truth is I don't know why. Before I had the code without assigning the invoice variable to the invoice object and it came out the same, however when I put the mouse over the properties of the object nothing came out, anything came out and now that I assign that variable if it comes out, then I'm a little confused, if someone could help me please
Answer:
Well, the problem that I see is that you are entering into a kind of cyclical redundancy, that is, when you define facturitas
you depend on the function importeTotal()
, but this in turn depends on facturitas
(that is, on itself) and as the object facturitas
"has not finished" being created, you enter into this conflict.
What I suggest is that you do something more generic, a function that accepts the object with the information and adds the additional information.
const facturitas = { nombre: 'gordonas', direccion: 'el hatillo', telefono: 2129633629, nif: '12jd-3jKu', producto: 'dona', descripcion: 'dona rellena de chocolate mas chispitas de chocolate', precio: 1.23, cantidad: 2, tipoIva: 1.21, formaPago: 'tarjeta' } function importeTotal(factura) { factura.importeTotal = (factura.precio * 1) / factura.tipoIva } function mostrarImporteTotal() { importeTotal(facturitas); return `el importe total es: ${facturitas.importeTotal}`; } console.log(mostrarImporteTotal());
This way it will not only work for facturitas
, you could also use it with other objects of type invoice.
Now, what you could also do (because according to me it is the objective of the exercise) is to define the method inside the object.
const facturitas = { nombre: 'gordonas', direccion: 'el hatillo', telefono: 2129633629, nif: '12jd-3jKu', producto: 'dona', descripcion: 'dona rellena de chocolate mas chispitas de chocolate', precio: 1.23, cantidad: 2, tipoIva: 1.21, importeTotal: function() { return (this.precio * 1) / this.tipoIva; }, formaPago: 'tarjeta' } console.log(`El importe total es: ${facturitas.importeTotal()}`);