我尝试了以下任务,但我坚持定义一个布尔值:
定义一个函数
接收钱包,一个对象。getWalletFacts
应该返回一个句子,说明钱包的颜色和 现金状态。getWalletFacts
我尝试的代码
const hascash = Boolean();
let wallet ={
color:"",
hascash:true||false,
write: function(sentence){
console.log(sentence);
}
};
function getWalletFacts(wallet){
let sentence= "my wallet is " + wallet.color+ " and " + wallet.hascash;
return sentence;
}
每当我检查我的答案时,它都会告诉我 hascash 是未定义的,即
Expected: "My wallet is Black and has cash"
Received: "my wallet is Black and undefined"
根据我对问题的理解,hascash 接受一个布尔值
举个例子
const wallet = {
color: "Black",
hasCash: true
};
getWalletFacts(wallet); // => 'My wallet is Black and has cash'
const wallet2 = {
color: "Grey",
hasCash: false
};
getWalletFacts(wallet2); // => 'My wallet is Grey and does not have cash'
是
hasCash
,不是hascash
——JavaScript区分大小写。
您还需要一个条件来将
true/false
值转换为正确的英语。
function getWalletFacts(wallet) {
let sentence = "my wallet is " + wallet.color + " and " + (wallet.hasCash ? "has cash" : "does not have cash");
return sentence;
}
const wallet = {
color: "Black",
hasCash: true
};
console.log(getWalletFacts(wallet)); // => 'My wallet is Black and has cash'
const wallet2 = {
color: "Grey",
hasCash: false
};
console.log(getWalletFacts(wallet2)); // => 'My wallet is Grey and does not have cash'
function getWalletFacts(wallet) {
let money = " ";
if (wallet.hasCash === true) {
money = "has cash";
} else {
money = "does not have cash";
}
let sentence = "My wallet is " + wallet.color + " and " + money;
return sentence;
}