简体   繁体   English

object javascript 中未定义的 boolean

[英]Undefined boolean in an object javascript

I attempted the following question but i am stuck on defining a boolean.我尝试了以下问题,但我坚持定义 boolean。 The question is as follows:问题如下:

QUESTION问题

Define a function getWalletFacts that receives wallet, an object.定义一个接收钱包的 function getWalletFacts ,一个 object。

getWalletFacts should return a sentence stating the wallet's color and cash state. getWalletFacts应该返回一个语句,说明钱包的颜色和现金 state。

My attempted code我尝试的代码

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;
}

whenever i check my answer it tells me that hascash is undefined ie每当我检查我的答案时,它都会告诉我 hascash 是未定义的,即

    Expected: "My wallet is Black and has cash"
        Received: "my wallet is Black and  undefined"

from my understanding of the question hascash accepts a boolean根据我对问题 hascash 的理解接受 boolean

Any help would be kindly appreciated!任何帮助将不胜感激!

Given Example给定示例

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'

It's hasCash , not hascash -- JavaScript is case-sensitive.它是hasCash ,而不是hascash - JavaScript 区分大小写。

You also need a conditional to turn the true/false values into proper English.您还需要一个条件来将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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM