Home > Enterprise >  How to insert dictionary values as arguments in JavaScript?
How to insert dictionary values as arguments in JavaScript?

Time:09-16

I'm outputting an array that will return the amount owed on a loan based on the loanAmount, interest, and loanTermYears for the dictionary instance1. The dictionary is printed successfully. However, calculatedLoanAmount returns NaN. The function calculateAmount is not properly reading the key-value pairs correctly. How do I properly insert these key-value pairs as arguments so that the function can return the total amount owed?

var info = []
var instance1 = new Object();

var instance1 = {
    Id: 0,
    customerName: "Khawaja Sardar",
    phoneNumber: "745-544-7777",
    address: "New York, NY",
    loanAmount: 5000,
    interest: 6.0,
    loanTermYears: 5,
    loanType: "APR",
    description: "Loan domain for instance1",
    calculatedLoanAmount: calculateAmount(instance1.loanAmount, instance1.interest, instance1.loanTermYears)

    }

function calculateAmount(loanAmount, interest, loanTermYears) {
    var totalMonth = loanTermYears * 12;
    var montlyInterest = interest / 100 / 12;
    var temp = Math.pow(1   montlyInterest, totalMonth);
    var monthlyPayment = loanAmount * montlyInterest * temp / (temp - 1);
    var sum =  monthlyPayment * totalMonth
    var totalAmount = sum

    return totalAmount
    
}

info.push(instance1)

console.log(info)

CodePudding user response:

The problem is the way you declare your object. This code simply creates an object of Instance with all the data from instance1. Try this instead:

var info = [];

function calculateAmount(loanAmount, interest, loanTermYears) {
  var totalMonth = loanTermYears * 12;
  var montlyInterest = interest / 100 / 12;
  var temp = Math.pow(1   montlyInterest, totalMonth);
  var monthlyPayment = (loanAmount * montlyInterest * temp) / (temp - 1);
  var sum =  monthlyPayment * totalMonth;
  var totalAmount = sum;

  return totalAmount;
}

function Instance(instance) {
  this.Id = instance.Id;
  this.customerName = instance.customerName;
  this.phoneNumber = instance.phoneNumber;
  this.address = instance.address;
  this.loanAmount = instance.loanAmount;
  this.interest = instance.interest;
  this.loanTermYears = instance.loanTermYears;
  this.loanType = instance.loanType;
  this.description = instance.description;
  this.calculatedLoanAmount = calculateAmount(
    this.loanAmount,
    this.interest,
    this.loanTermYears
  );
}

var instanceParams = {
  Id: 0,
  customerName: "Khawaja Sardar",
  phoneNumber: "745-544-7777",
  address: "New York, NY",
  loanAmount: 5000,
  interest: 6.0,
  loanTermYears: 5,
  loanType: "APR",
  description: "Loan domain for instance1"
};

const instance1 = new Instance(instanceParams);

info.push(instance1);

console.log(info[0]);

CodePudding user response:

If you removed line two, you would get an error saying something like 'cannot read properties of undefined (reading "loanAmount")' since instance1 would not yet be defined. If you want to set an object's keys based on other keys the object contains, you need to do it after the object is created, e.g

var instance1 = {
...
}
instance1.calculatedLoanAmount = calculateLoanAmount(instance1.loanAmount, instance1.interest, instance1.loanTermYears)
  • Related