Javascript Error: Object Undefined

I’m making a fantasy currency converter, and I thought it was going great, until I tried to actually use it. This is the code:

class Exchange {

  constructor (currencyOne, originalPrice, currencyTwo, amount) {

  this.currencyOne = currencyOne,

  this.usdPrice = usdPrice,

  this.currencyTwo = currencyTwo,

  this.amount = amount

  }

  get currencyOne() {

    return this.currencyOne;

  }

  get usdPrice() {

    return this.usdPrice;

  }

  get currencyTwo() {

    return this.currencyTwo;

  }

  findNewAmount(currencyTwo, amount) {

    if (currencyTwo === 'Credits') {

      //Star Wars

      newPrice = amount * 2.1;

      return newPrice;

    } else if (currencyTwo === 'Altairian dollars') {

      //Hitchhiker's Guide to the Galaxy

      newPrice = amount * (5 / 26);

      return newPrice;

    } else if (currencyTwo === 'Lei') {

      //Resident Evil: Village

      newPrice = amount * (1.1 / 8);

      return newPrice;

    }

    return 'The new price is ' + newPrice + ' in ' + currencyTwo;

  }

}

Whenever I try to run Exchange.findNewAmount('Credits', 22155); (it’s 10,550 credits to buy the kind of landspeeder Luke Skywalker owns, and that should be the output if I input 22,155, the price in USD), it throws the error “Exchange.findNewAmount is not a function at Object.” First off, I don’t know what that means, and secondly, how do I fix it?

Hey @realelfruler

I’m not on that much of an expert, and I’m also on the Objects Lesson right now :+1:
I think that you forgot to put an = sign at the start of your Object. class Exchange = {.
Also a good practice would be to put a comma at the end of each statement/block inside the Objects. As I’ve pasted your code inside VSC and multiple syntax errors were instantly recognised :man_shrugging:

Are you familiar with this keyword? If not you should get familiar with it, because I don’t think your function findNewAmount() can reach those variables inside of the object, and I think that’s where you need to use this.

Hope this was a bit helpful. Good Luck

Cheers

1 Like

Thank you @ktoni73! I was actually creating a class (it’s basically an object that can create other objects, if you haven’t gotten to that lesson yet), but changing it to an object instead made it much easier to work with.

However, now that I’ve changed the code, the new error is a call stack size exceeded on return this.currencyTwo in the getter of the same name. This error only pops up when I try to actually use the code.

const Exchange = {
  currencyOne: 'USD',
  currencyTwo: ' ',
  amount: 0,
  newPrice: 0,

  get currencyOne() {
    return this.currencyOne;
  },

  get currencyTwo() {
    return this.currencyTwo;
  },
  set currencyTwo(currencyName) {
    return this.currencyTwo = currencyName;
  },

  get amount() {
    return this.amount;
  },
  set amount(amount) {
    return this.amount = amount;
  },

  get newPrice() {
    return this.amount;
  },
  set newPrice(newPrice) {
    return this.newPrice = newPrice;
  },

  findNewAmount(currencyTwo, amount) {
    if (this.currencyTwo === 'Credits') {
      //Star Wars
      this.newPrice = this.amount * 2.1;
      return newPrice;
    } else if (this.currencyTwo === 'Altairian dollars') {
      //Hitchhiker's Guide to the Galaxy
      this.newPrice = this.amount * (5 / 26);
      return newPrice;
    } else if (currencyTwo === 'Lei') {
      //Resident Evil: Village
      this.newPrice = this.amount * (1.1 / 8);
      return newPrice;
    };

    return 'The new price is ' + newPrice + ' in ' + currencyTwo;
  }
};

Okay, so I stepped away from this project for a bit, but when I came back I realized that I could easily fix the call stack error by adding underscores to all the properties of Exchange. However, I then realized that, while the code looked cool, it never actually returned a real amount, so I ended up having to rework some things. I deleted the entire object, and was left with the function findNewAmount to work with. This is the new code, which works fine, except it doesn’t print my fancy schmancy statement.

const findNewAmount = (currencyTwo, amount) => {
      let newPrice = 0;
      if (currencyTwo === 'Credits') {
        //Star Wars
        newPrice = amount / 2.1;
        return newPrice;
      } else if (currencyTwo === 'Altairian dollars') {
        //Hitchhiker's Guide to the Galaxy
        newPrice = amount / (5 / 26);
        return newPrice;
      } else if (currencyTwo === 'Lei') {
        //Resident Evil: Village
        newPrice = amount / (1.1 / 8);
        return newPrice;
      };

      return `The new price is ${newPrice} ${currencyTwo}`;
}