【问题标题】:Write a function, iterate through an object and string, return account balance and account name写一个函数,遍历一个对象和字符串,返回账户余额和账户名
【发布时间】:2019-02-06 22:12:11
【问题描述】:

我卡在这段代码上,因为我能够遍历对象(下面列出了两种方法),但是,如果我需要对象中的第二个键,我不确定如何访问它自动键。

以下是问题的说明:编写一个函数 bankAccountChecker,它接收一个对象,该对象代表某人的整个银行信息,以及一个“帐户”字符串。您的函数应该检查银行信息的输入“帐户”,并返回一个包含余额和帐户检查的字符串(请参阅下面的示例函数调用)

这是我尝试的第一个代码sn-p:

function bankAccountChecker (bankingInfo, account) {
  for(const key in bankingInfo) {
    let value = bankingInfo[key];
    return `You have $ ${bankingInfo[key]} in your ${account} account`
  }
}

var account001 = {
  checking: 1000000,
  savings: 2
};

var account002 = {
  business: 2000000,
  personal: -4
};

var account003 = {
  travel: 534,
  education: 1012
};

bankAccountChecker(account001, 'checking'); //"You have $1000000 in your checking account"
bankAccountChecker(account002, 'personal'); //"You have $-4 in your personal account"
bankAccountChecker(account003, 'travel'); //"You have $534 in your travel account"

这是我尝试过的第二个:

function bankAccountChecker (bankingInfo, account) {
  const values = Object.values(bankingInfo);
    return `You have $ ${values} in your ${account} account`
}

var account001 = {
  checking: 1000000,
  savings: 2
};

var account002 = {
  business: 2000000,
  personal: -4
};

var account003 = {
  travel: 534,
  education: 1012
};

bankAccountChecker(account001, 'checking'); //"You have $1000000 in your checking account"
bankAccountChecker(account002, 'personal'); //"You have $-4 in your personal account"
bankAccountChecker(account003, 'travel'); //"You have $534 in your travel account"

无论如何,我运行的每个代码 sn-p 都是不正确的。如果您有任何反馈,请告诉我。谢谢! (我已经包含了代码应该迭代的测试和对象)。

【问题讨论】:

  • bankingInfo[account]bankAccountChecker ()

标签: javascript object iteration


【解决方案1】:

您可以使用bracket notation 访问对象中的帐户:

function bankAccountChecker(bankingInfo, account) {
  return `You have $ ${bankingInfo[account]} in your ${account} account`
}

var account001 = {
  checking: 1000000,
  savings: 2
};

var account002 = {
  business: 2000000,
  personal: -4
};

var account003 = {
  travel: 534,
  education: 1012
};

console.log( bankAccountChecker(account001, 'checking') ); 
console.log( bankAccountChecker(account002, 'personal') ); 
console.log( bankAccountChecker(account003, 'travel') ); 

【讨论】:

    【解决方案2】:

    你非常接近 :) 你只需要引用对象键。 [帐户] 例如。

    function bankAccountChecker (bankingInfo, account) {
        return`You have $ ${bankingInfo[account]} in your ${account} account`
    }
    
    var account001 = {
      checking: 1000000,
      savings: 2
    };
    
    var account002 = {
      business: 2000000,
      personal: -4
    };
    
    var account003 = {
      travel: 534,
      education: 1012
    };
    
    console.log(bankAccountChecker(account001, 'checking')); //"You have $1000000 in your checking account"
    console.log(bankAccountChecker(account002, 'personal')); //"You have $-4 in your personal account"
    console.log(bankAccountChecker(account003, 'travel')); //"You have $534 in your travel account"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-23
      • 2021-02-16
      • 2011-12-09
      • 2014-12-01
      相关资源
      最近更新 更多