Home > database >  Is there an object method I'm missing that tests a new object value against a string?
Is there an object method I'm missing that tests a new object value against a string?

Time:10-13

I'm trying to test if a object has the value for a specific key using a method with an if statement.

function PCParts(motherboard, graphics, cpu, randomAccessMemory) {
  this.motherboard = motherboard;
  this.graphics = graphics;
  this.cpu = cpu;
  this.randomAccessMemory = randomAccessMemory;
  //these methods are called and a result is displayed
  this.caseSize = case_size();
  this.storage = storage_device();
}

//this should check the input and return a proper case size.
//For some reason, in the for-in loop it decides to return "mid-tower" regardless of if the first part of the if statement is true.
function case_size() {
  if (this.motherboard == "MSI B450 Tomahawk") {
    this.caseSize = "full tower";
  } else {
    this.caseSize = "mid tower";

  }
  return this.caseSize;
}

//this is having the same issue as case_size function
//it refuses to display "NVMe-1TB" even if the if statment is true.
//I tried removing "this" to see if that was the issue and it refused to display anything.
function storage_device() {
  if (this.cpu == "AMD Ryzen 5" && this.randomAccessMemory == "Corsair Vengeance 32GB") {
    this.storage = "NVMe 1TB";
  } else {
    this.storage = "HDD 1TB";
  }
  return this.storage;
}



let gaming_computer = new PCParts("MSI B450 Tomahawk", "GTX 3090", "AMD Ryzen 5", "Corsair Vengeance 32GB");

for (let property in gaming_computer) {
  document.write(`${property}: ${gaming_computer[property]} <br>`);
}

The code is commented with the issues I'm having but basically the "if" statement is false for some reason and the "else" statement is what is being displayed.

Is there some object method that tests a new object value against a string? If this has been answered somewhere else, sorry... I've been looking and can't find an answer. Thanks.

CodePudding user response:

in your case_size you have used this keyword . but case_size is simply a function not a class . so this keyword should not be used . and in PCParts() , when you are calling case_size() you have to pass the argument of the motherboard so that in the own function it can access the value of motherboard otherwise it can not take value of motherboard from a another function . that's all . HAPPY CODING !!

function PCParts(motherboard, graphics, cpu, randomAccessMemory) {
    this.motherboard = motherboard;
    this.graphics = graphics;
    this.cpu = cpu;
    this.randomAccessMemory = randomAccessMemory;
    this.caseSize = case_size(motherboard);
    this.storage = storage_device();
  }
  function case_size(motherboard) {
    if (motherboard == "MSI B450 Tomahawk") {
      caseSize = "full tower";
    } else {
      caseSize = "mid tower";
    }
    return caseSize;
  }
  function storage_device() {
    if (this.cpu == "AMD Ryzen 5" && this.randomAccessMemory == "Corsair Vengeance 32GB") {
      this.storage = "NVMe 1TB";
    } else {
      this.storage = "HDD 1TB";
    }
    return this.storage;
  }
  let gaming_computer = new PCParts("MSI B450 Tomahawk", "GTX 3090", "AMD Ryzen 5", "Corsair Vengeance 32GB");
  for (let property in gaming_computer) {
    document.write(`${property}: ${gaming_computer[property]} <br>`);
  }

  • Related